From 75c4e837350228bceec5407fd12830c480ff8359 Mon Sep 17 00:00:00 2001 From: Rafael Weinstein Date: Wed, 3 Jul 2013 14:50:39 -0700 Subject: [PATCH] Observers try to call .unobserved on observed objects when they close --- change_summary.js | 21 +++++++++------------ tests/test.js | 27 +++++++++++++++++++++++++++ util/array_reduction.js | 4 ++++ 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/change_summary.js b/change_summary.js index c373733..34ffc36 100644 --- a/change_summary.js +++ b/change_summary.js @@ -281,7 +281,8 @@ return copy; } - function Observer(callback) { + function Observer(object, callback) { + this.object = object; this.callback = callback; this.reporting = true; if (hasObserve) @@ -309,7 +310,11 @@ close: function() { if (!this.valid) return; + if (typeof this.object.unobserved === 'function') + this.object.unobserved(); + this.disconnect(); + this.object = undefined; this.valid = false; }, @@ -427,8 +432,7 @@ } function ObjectObserver(object, callback) { - this.object = object; - Observer.call(this, callback); + Observer.call(this, object, callback); } ObjectObserver.prototype = createObject({ @@ -476,18 +480,13 @@ this.oldObject = undefined; else if (this.object) Object.unobserve(this.object, this.boundInternalCallback); - - this.object = undefined; } }); function ArrayObserver(array, callback) { if (!Array.isArray(array)) throw Error('Provided object is not an Array'); - - this.object = array; - - Observer.call(this, callback); + Observer.call(this, array, callback); } ArrayObserver.prototype = createObject({ @@ -605,9 +604,8 @@ if (!isObject(object)) return; - this.object = object; this.path = path; - Observer.call(this, callback); + Observer.call(this, object, callback); } PathObserver.prototype = createObject({ @@ -619,7 +617,6 @@ }, disconnect: function() { - this.object = undefined; this.value = undefined; if (hasObserve) { this.observedSet.reset(); diff --git a/tests/test.js b/tests/test.js index dbc9b0d..513bbd6 100644 --- a/tests/test.js +++ b/tests/test.js @@ -131,6 +131,14 @@ suite('PathObserver Tests', function() { callbackInvoked = false; } + test('Close Invokes Unobserved', function() { + var called = false; + var obj = { foo: 1, unobserved: function() { called = true }}; + var observer = new PathObserver(obj, 'foo', function() {}); + observer.close(); + assert.isTrue(called); + }); + test('Delivery Until No Changes', function() { var obj = { foo: { bar: 5 }}; var callbackCount = 0; @@ -729,6 +737,15 @@ suite('ArrayObserver Tests', function() { observer.close(); } + test('Close Invokes Unobserved', function() { + var called = false; + var obj = []; + obj.unobserved = function() { called = true }; + var observer = new ArrayObserver(obj, function() {}); + observer.close(); + assert.isTrue(called); + }); + test('Delivery Until No Changes', function() { var arr = [0, 1, 2, 3, 4]; var callbackCount = 0; @@ -1282,6 +1299,16 @@ suite('ObjectObserver Tests', function() { callbackInvoked = false; } + test('Close Invokes Unobserved', function() { + var called = false; + var obj = {}; + obj.unobserved = function() { called = true }; + var observer = new ObjectObserver(obj, function() {}); + observer.close(); + assert.isTrue(called); + }); + + test('Delivery Until No Changes', function() { var obj = { foo: 5 }; var callbackCount = 0; diff --git a/util/array_reduction.js b/util/array_reduction.js index f19c787..709dc83 100644 --- a/util/array_reduction.js +++ b/util/array_reduction.js @@ -74,6 +74,10 @@ function ArrayReduction(array, path, reduceFn, initial) { arrayObserver.close(); }; + this.unobserved = function() { + self.close(); + }; + this.deliver = function() { arrayObserver.deliver(); observers.forEach(function(observer) {