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

Commit

Permalink
Observers try to call .unobserved on observed objects when they close
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelw committed Jul 3, 2013
1 parent 0d5a438 commit 75c4e83
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
21 changes: 9 additions & 12 deletions change_summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@
return copy;
}

function Observer(callback) {
function Observer(object, callback) {
this.object = object;
this.callback = callback;
this.reporting = true;
if (hasObserve)
Expand Down Expand Up @@ -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;
},

Expand Down Expand Up @@ -427,8 +432,7 @@
}

function ObjectObserver(object, callback) {
this.object = object;
Observer.call(this, callback);
Observer.call(this, object, callback);
}

ObjectObserver.prototype = createObject({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand All @@ -619,7 +617,6 @@
},

disconnect: function() {
this.object = undefined;
this.value = undefined;
if (hasObserve) {
this.observedSet.reset();
Expand Down
27 changes: 27 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions util/array_reduction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 75c4e83

Please sign in to comment.