From 957e1776ea266093045d39fece308926088d2d69 Mon Sep 17 00:00:00 2001 From: John Messerly Date: Wed, 24 Sep 2014 21:19:01 -0700 Subject: [PATCH] fix bug with rootObj optimization --- src/observe.js | 21 +++++++++++---------- tests/test.js | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/observe.js b/src/observe.js index 147f68a..42b41cd 100644 --- a/src/observe.js +++ b/src/observe.js @@ -390,7 +390,7 @@ obj = obj[this[i - 1]]; if (!isObject(obj)) return; - observe(obj, this[0]); + observe(obj, this[i]); } }, @@ -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); @@ -691,7 +690,9 @@ rootObj = undefined; rootObjProps = undefined; observedSetCache.push(this); - } + if (lastObservedSet === this) + lastObservedSet = null; + }, }; return record; @@ -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; diff --git a/tests/test.js b/tests/test.js index 9bc594f..213a278 100644 --- a/tests/test.js +++ b/tests/test.js @@ -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(); + }); + }); + });