Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
fix bug with rootObj optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
John Messerly committed Sep 25, 2014
1 parent b17a7aa commit 957e177
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@
obj = obj[this[i - 1]];
if (!isObject(obj))
return;
observe(obj, this[0]);
observe(obj, this[i]);
}
},

Expand Down Expand Up @@ -663,14 +663,13 @@
}

var record = {
object: undefined,
objects: objects,
get rootObject() { return rootObj; },
set rootObject(value) {
rootObj = value;
rootObjProps = {};
},
open: function(obs, object) {
if (!rootObj) {
rootObj = object;
rootObjProps = {};
}

observers.push(obs);
observerCount++;
obs.iterateObjects_(observe);
Expand All @@ -691,7 +690,9 @@
rootObj = undefined;
rootObjProps = undefined;
observedSetCache.push(this);
}
if (lastObservedSet === this)
lastObservedSet = null;
},
};

return record;
Expand All @@ -700,9 +701,9 @@
var lastObservedSet;

function getObservedSet(observer, obj) {
if (!lastObservedSet || lastObservedSet.object !== obj) {
if (!lastObservedSet || lastObservedSet.rootObject !== obj) {
lastObservedSet = observedSetCache.pop() || newObservedSet();
lastObservedSet.object = obj;
lastObservedSet.rootObject = obj;
}
lastObservedSet.open(observer, obj);
return lastObservedSet;
Expand Down
20 changes: 20 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,26 @@ suite('PathObserver Tests', function() {
done();
});
});

test('object cycle', function(done) {
var model = { a: {}, c: 1 };
model.a.b = model;

var called = false;
new PathObserver(model, 'a.b.c').open(function() {
called = true;
});

// This change should be detected, even though it's a change to the root
// object and isn't a change to `a`.
model.c = 42;

then(function() {
assert.strictEqual(called, true);
done();
});
});

});


Expand Down

0 comments on commit 957e177

Please sign in to comment.