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

Commit

Permalink
CompoundObserver.getValues retrieve present values, but not update la…
Browse files Browse the repository at this point in the history
…st 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
  • Loading branch information
rafaelw committed Dec 16, 2013
1 parent d5e8dc3 commit f4ccab1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -821,7 +831,7 @@
this.depsChanged_.changed = !this.depsChanged_.changed;
},

getValues_: function(sync) {
getValues_: function(values, sync) {
if (this.observedSet_)
this.observedSet_.reset();

Expand All @@ -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_)
Expand All @@ -853,7 +863,7 @@
},

check_: function() {
var oldValues = this.getValues_();
var oldValues = this.getValues_(this.value_);
if (!oldValues)
return;

Expand All @@ -865,7 +875,7 @@

sync_: function(hard) {
if (hard)
this.getValues_(true);
this.getValues_(this.value_, true);
},

close: function() {
Expand Down
20 changes: 20 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down

0 comments on commit f4ccab1

Please sign in to comment.