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

Commit

Permalink
Allow CompoundObserver to report changes in value from added to open
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelw committed Apr 16, 2014
1 parent fdc0da7 commit 2e3aaec
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
41 changes: 25 additions & 16 deletions src/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -875,9 +875,10 @@
}
});

function CompoundObserver() {
function CompoundObserver(reportChangesOnOpen) {
Observer.call(this);

this.reportChangesOnOpen_ = reportChangesOnOpen;
this.value_ = [];
this.directObserver_ = undefined;
this.observed_ = [];
Expand All @@ -889,23 +890,22 @@
__proto__: Observer.prototype,

connect_: function() {
this.check_(undefined, true);

if (!hasObserve)
return;

var object;
var needsDirectObserver = false;
for (var i = 0; i < this.observed_.length; i += 2) {
object = this.observed_[i]
if (object !== observerSentinel) {
needsDirectObserver = true;
break;
if (hasObserve) {
var object;
var needsDirectObserver = false;
for (var i = 0; i < this.observed_.length; i += 2) {
object = this.observed_[i]
if (object !== observerSentinel) {
needsDirectObserver = true;
break;
}
}

if (needsDirectObserver)
this.directObserver_ = getObservedSet(this, object);
}

if (needsDirectObserver)
this.directObserver_ = getObservedSet(this, object);
this.check_(undefined, !this.reportChangesOnOpen_);
},

disconnect_: function() {
Expand All @@ -926,14 +926,23 @@
if (this.state_ != UNOPENED && this.state_ != RESETTING)
throw Error('Cannot add paths once started.');

this.observed_.push(object, getPath(path));
var path = getPath(path);
this.observed_.push(object, path);
if (!this.reportChangesOnOpen_)
return;
var index = this.observed_.length / 2 - 1;
this.value_[index] = path.getValueFrom(object);
},

addObserver: function(observer) {
if (this.state_ != UNOPENED && this.state_ != RESETTING)
throw Error('Cannot add observers once started.');

this.observed_.push(observerSentinel, observer);
if (!this.reportChangesOnOpen_)
return;
var index = this.observed_.length / 2 - 1;
this.value_[index] = observer.open(this.deliver, this);
},

startReset: function() {
Expand Down
31 changes: 27 additions & 4 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ function assertPathChanges(expectNewValue, expectOldValue, dontDeliver) {
}

function assertCompoundPathChanges(expectNewValues, expectOldValues,
expectObserved) {
observer.deliver();
expectObserved, dontDeliver) {
if (!dontDeliver)
observer.deliver();

assert.isTrue(callbackInvoked);

Expand All @@ -84,8 +85,10 @@ function assertCompoundPathChanges(expectNewValues, expectOldValues,
assert.deepEqual(expectOldValues, oldValues);
assert.deepEqual(expectObserved, observed);

assert.isTrue(window.dirtyCheckCycleCount === undefined ||
window.dirtyCheckCycleCount === 1);
if (!dontDeliver) {
assert.isTrue(window.dirtyCheckCycleCount === undefined ||
window.dirtyCheckCycleCount === 1);
}

callbackArgs = undefined;
callbackInvoked = false;
Expand Down Expand Up @@ -1066,6 +1069,7 @@ suite('CompoundObserver Tests', function() {
observer.addPath(model, 'b');
observer.addPath(model, Path.get('c'));
observer.open(callback);
assertNoChanges();

var observerCallbackArg = [model, Path.get('a'),
model, Path.get('b'),
Expand Down Expand Up @@ -1102,6 +1106,25 @@ suite('CompoundObserver Tests', function() {
observer.close();
});

test('reportChangesOnOpen', function() {
var model = { a: 1, b: 2, c: 3 };

observer = new CompoundObserver(true);
observer.addPath(model, 'a');
observer.addPath(model, 'b');
observer.addPath(model, Path.get('c'));

model.a = -10;
model.b = 20;
observer.open(callback);
var observerCallbackArg = [model, Path.get('a'),
model, Path.get('b'),
model, Path.get('c')];
assertCompoundPathChanges([-10, 20, 3], [1, 2, ],
observerCallbackArg, true);
observer.close();
});

test('All Observers', function() {
function ident(value) { return value; }

Expand Down

0 comments on commit 2e3aaec

Please sign in to comment.