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

Commit

Permalink
Dirty-check underlying value -- not computed value
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelw committed Aug 30, 2013
1 parent 2dd6f8a commit 6c7b0cf
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 36 deletions.
26 changes: 18 additions & 8 deletions src/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@
observer.report();
cycles++;
}
if (global.testingExposeCycleCount)
global.dirtyCheckCycleCount = cycles;
}

function objectIsEmpty(object) {
Expand Down Expand Up @@ -417,6 +419,9 @@
}
} while (cycles < MAX_DIRTY_CHECK_CYCLES && results.anyChanged);

if (global.testingExposeCycleCount)
global.dirtyCheckCycleCount = cycles;

Observer._allObserversCount = allObservers.length;
runningMicrotaskCheckpoint = false;
};
Expand Down Expand Up @@ -589,21 +594,24 @@
if (!path) {
// Invalid path.
this.closed = true;
this.value = valueFn ? valueFn() : undefined;
this.value_ = undefined;
this.value = valueFn ? valueFn(this.value_) : this.value_;
return;
}

if (!path.length) {
// 0-length path.
this.closed = true;
this.value = valueFn ? valueFn(object) : object;
this.value_ = object;
this.value = valueFn ? valueFn(this.value_) : this.value_;
return;
}

if (!isObject(object)) {
// non-object & non-0-length path.
this.closed = true;
this.value = valueFn ? valueFn() : undefined;
this.value_ = undefined;
this.value = valueFn ? valueFn(this.value_) : this.value_;
return;
}

Expand All @@ -626,6 +634,7 @@

disconnect: function() {
this.value = undefined;
this.value_ = undefined;
if (this.observedSet) {
this.observedSet.reset();
this.observedSet.cleanup();
Expand All @@ -639,15 +648,15 @@
if (this.observedSet)
this.observedSet.reset();

var newValue = this.path.getValueFrom(this.object, this.observedSet)
this.value = this.valueFn ? this.valueFn(newValue) : newValue;
this.value_ = this.path.getValueFrom(this.object, this.observedSet);

if (this.observedSet)
this.observedSet.cleanup();

if (areSameValue(this.value, this.oldValue))
if (areSameValue(this.value_, this.oldValue_))
return false;

this.value = this.valueFn ? this.valueFn(this.value_) : this.value_;
this.reportArgs = [this.value, this.oldValue];
return true;
},
Expand All @@ -657,13 +666,14 @@
if (this.observedSet)
this.observedSet.reset();

var newValue = this.path.getValueFrom(this.object, this.observedSet)
this.value = this.valueFn ? this.valueFn(newValue) : newValue;
this.value_ = this.path.getValueFrom(this.object, this.observedSet);
this.value = this.valueFn ? this.valueFn(this.value_) : this.value_;

if (this.observedSet)
this.observedSet.cleanup();
}

this.oldValue_ = this.value_;
this.oldValue = this.value;
},

Expand Down
67 changes: 39 additions & 28 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ var observer;
var callbackArgs = undefined;
var callbackInvoked = false;

window.testingExposeCycleCount = true;

function callback() {
callbackArgs = Array.prototype.slice.apply(arguments);
callbackInvoked = true;
Expand All @@ -34,6 +36,23 @@ function assertNoChanges() {
assert.isUndefined(callbackArgs);
}

function assertPathChanges(expectNewValue, expectOldValue) {
observer.deliver();

assert.isTrue(callbackInvoked);

var newValue = callbackArgs[0];
var oldValue = callbackArgs[1];
assert.deepEqual(expectNewValue, newValue);
assert.deepEqual(expectOldValue, oldValue);

assert.isTrue(window.dirtyCheckCycleCount === undefined ||
window.dirtyCheckCycleCount === 1);

callbackArgs = undefined;
callbackInvoked = false;
}

var createObject = ('__proto__' in {}) ?
function(obj) { return obj; } :
function(obj) {
Expand Down Expand Up @@ -117,20 +136,6 @@ suite('PathObserver Tests', function() {

teardown(doTeardown);

function assertPathChanges(expectNewValue, expectOldValue) {
observer.deliver();

assert.isTrue(callbackInvoked);

var newValue = callbackArgs[0];
var oldValue = callbackArgs[1];
assert.deepEqual(expectNewValue, newValue);
assert.deepEqual(expectOldValue, oldValue);

callbackArgs = undefined;
callbackInvoked = false;
}

test('Close Invokes Close', function() {
var called = false;
var obj = { foo: 1, close: function() { called = true }};
Expand Down Expand Up @@ -385,6 +390,26 @@ suite('PathObserver Tests', function() {
observer.close();
});

test('valueFn - return object literal', function() {
var model = { };

function valueFn(value) {
return isNaN(value) ? value : [ value ];
}

observer = new PathObserver(model, 'foo', callback, undefined, undefined,
valueFn);

model.foo = 1;
assertPathChanges([1], undefined);

model.foo = 3;
assertPathChanges([3], [1]);

observer.close();
});


test('Path With Indices', function() {
var model = [];

Expand Down Expand Up @@ -714,20 +739,6 @@ suite('CompoundPathObserver Tests', function() {

teardown(doTeardown);

function assertPathChanges(expectNewValue, expectOldValue) {
observer.deliver();

assert.isTrue(callbackInvoked);

var newValue = callbackArgs[0];
var oldValue = callbackArgs[1];
assert.deepEqual(expectNewValue, newValue);
assert.deepEqual(expectOldValue, oldValue);

callbackArgs = undefined;
callbackInvoked = false;
}

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

Expand Down

0 comments on commit 6c7b0cf

Please sign in to comment.