From 2e3aaece33154de8e56cd0992743ecb59c1508c9 Mon Sep 17 00:00:00 2001 From: Rafael Weinstein Date: Wed, 16 Apr 2014 16:27:22 -0700 Subject: [PATCH] Allow CompoundObserver to report changes in value from added to open R=arv BUG= Review URL: https://codereview.appspot.com/88160051 --- src/observe.js | 41 +++++++++++++++++++++++++---------------- tests/test.js | 31 +++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/src/observe.js b/src/observe.js index a7282a3..5e41206 100644 --- a/src/observe.js +++ b/src/observe.js @@ -875,9 +875,10 @@ } }); - function CompoundObserver() { + function CompoundObserver(reportChangesOnOpen) { Observer.call(this); + this.reportChangesOnOpen_ = reportChangesOnOpen; this.value_ = []; this.directObserver_ = undefined; this.observed_ = []; @@ -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() { @@ -926,7 +926,12 @@ 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) { @@ -934,6 +939,10 @@ 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() { diff --git a/tests/test.js b/tests/test.js index f90542b..3924921 100644 --- a/tests/test.js +++ b/tests/test.js @@ -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); @@ -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; @@ -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'), @@ -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; }