From f4ccab114a3cb015dba912847b2fb90f7f5028a2 Mon Sep 17 00:00:00 2001 From: Rafael Weinstein Date: Mon, 16 Dec 2013 14:33:12 -0800 Subject: [PATCH] CompoundObserver.getValues retrieve present values, but not update last reported values. Note that this adds a test for the above and also tests for discardChanges & setValue. R=arv BUG= Review URL: https://codereview.appspot.com/42990043 --- src/observe.js | 24 +++++++++++++++++------- tests/test.js | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/observe.js b/src/observe.js index 68c124f..b0bdc1a 100644 --- a/src/observe.js +++ b/src/observe.js @@ -774,6 +774,16 @@ CompoundObserver.prototype = createObject({ __proto__: PathObserver.prototype, + getValue: function() { + var values = []; + this.getValues_(values, true); + return values; + }, + + setValue: function() { + console.warn('Set to CompoundObserver ignored.'); + }, + // TODO(rafaelw): Consider special-casing when |object| is a PathObserver // and path 'value' to avoid explicit observation. addPath: function(object, path) { @@ -821,7 +831,7 @@ this.depsChanged_.changed = !this.depsChanged_.changed; }, - getValues_: function(sync) { + getValues_: function(values, sync) { if (this.observedSet_) this.observedSet_.reset(); @@ -834,16 +844,16 @@ pathOrObserver.getValueFrom(object, this.observedSet_) if (sync) { - this.value_[i / 2] = value; + values[i / 2] = value; continue; } - if (areSameValue(value, this.value_[i / 2])) + if (areSameValue(value, values[i / 2])) continue; oldValues = oldValues || []; - oldValues[i / 2] = this.value_[i / 2]; - this.value_[i / 2] = value; + oldValues[i / 2] = values[i / 2]; + values[i / 2] = value; } if (this.observedSet_) @@ -853,7 +863,7 @@ }, check_: function() { - var oldValues = this.getValues_(); + var oldValues = this.getValues_(this.value_); if (!oldValues) return; @@ -865,7 +875,7 @@ sync_: function(hard) { if (hard) - this.getValues_(true); + this.getValues_(this.value_, true); }, close: function() { diff --git a/tests/test.js b/tests/test.js index b96d9f3..3382856 100644 --- a/tests/test.js +++ b/tests/test.js @@ -952,6 +952,26 @@ suite('CompoundObserver Tests', function() { assertCompoundPathChanges(['a', 20, 'c'], [-10,, 30], observerCallbackArg); + model.a = 2; + model.b = 3; + model.c = 4; + + assert.deepEqual([2, 3, 4], observer.getValue()); + assertCompoundPathChanges([2, 3, 4], ['a', 20, 'c'], + observerCallbackArg); + + model.a = 'z'; + model.b = 'y'; + model.c = 'x'; + assert.deepEqual(['z', 'y', 'x'], observer.discardChanges()); + assertNoChanges(); + + observer.setValue('blarg!'); + assert.strictEqual('z', model.a); + assert.strictEqual('y', model.b); + assert.strictEqual('x', model.c); + assertNoChanges(); + observer.close(); });