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

Commit

Permalink
Add optional valueFn to PathObserver
Browse files Browse the repository at this point in the history
This is the first step towards adding a CompoundPathObserver to observe-js (and removing CompoundBinding from TemplateBind). The motivation for this is to reduce allocation and total number of observers when CompoundObservation is needed.

This step allows for code to special case when there is a conversion function, but only one dependent path.

R=arv
BUG=

Review URL: https://codereview.appspot.com/13208043
  • Loading branch information
rafaelw committed Aug 24, 2013
1 parent fae14aa commit d2b20c2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/observe.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,25 +587,28 @@
}
};

function PathObserver(object, pathString, callback, target, token) {
this.value = undefined;
function PathObserver(object, pathString, callback, target, token, valueFn) {
this.valueFn = valueFn;

var path = getPath(pathString);
if (!path) {
// Invalid path.
this.closed = true;
this.value = undefined;
this.value = this.valueFn ? this.valueFn() : undefined;
return;
}

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

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

Expand All @@ -631,7 +634,10 @@
},

check: function() {
this.value = this.path.getValueFrom(this.object, this.observedSet);
// Note: Extracting this to a member function for use here and below
// regresses dirty-checking path perf by about 25% =-(.
var newValue = this.path.getValueFrom(this.object, this.observedSet)
this.value = this.valueFn ? this.valueFn(newValue) : newValue;

if (areSameValue(this.value, this.oldValue))
return false;
Expand All @@ -641,8 +647,10 @@
},

sync: function(hard) {
if (hard)
this.value = this.path.getValueFrom(this.object, this.observedSet);
if (hard) {
var newValue = this.path.getValueFrom(this.object, this.observedSet)
this.value = this.valueFn ? this.valueFn(newValue) : newValue;
}

this.oldValue = this.value;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,28 @@ suite('PathObserver Tests', function() {
observer.close();
});

test('valueFn', function() {
var model = { };

function valueFn(value) {
return isNaN(value) ? value : value * 3;
}

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

model.foo = 1;
assertPathChanges(3, undefined);

model.foo = 2;
assertPathChanges(6, 3);

delete model.foo;
assertPathChanges(undefined, 6);

observer.close();
});

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

Expand Down

0 comments on commit d2b20c2

Please sign in to comment.