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

fix bug with rootObj optimization in observed sets #73

Merged
merged 1 commit into from
Oct 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = 0;
new PathObserver(model, 'a.b.c').open(function() {
called++;
});

// 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.equal(called, 1);
done();
});
});

});


Expand Down