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

Commit fbe86b9

Browse files
committed
Change setValueFn to not be a transform (now is responsible for updating the value). Also rename valueFn to transformFn.
R=arv BUG= Review URL: https://codereview.appspot.com/33390043
1 parent 90d5bbc commit fbe86b9

File tree

2 files changed

+77
-24
lines changed

2 files changed

+77
-24
lines changed

src/observe.js

+22-19
Original file line numberDiff line numberDiff line change
@@ -662,17 +662,18 @@
662662
}
663663
};
664664

665-
function PathObserver(object, path, callback, target, valueFn, setValueFn) {
665+
function PathObserver(object, path, callback, target, transformFn,
666+
setValueFn) {
666667
var path = path instanceof Path ? path : getPath(path);
667668
if (!path || !path.length || !isObject(object)) {
668669
this.value_ = path ? path.getValueFrom(object) : undefined;
669-
this.value = valueFn ? valueFn(this.value_) : this.value_;
670+
this.value = transformFn ? transformFn(this.value_) : this.value_;
670671
this.closed = true;
671672
return;
672673
}
673674

674675
Observer.call(this, object, callback, target);
675-
this.valueFn = valueFn;
676+
this.transformFn = transformFn;
676677
this.setValueFn = setValueFn;
677678
this.path = path;
678679

@@ -712,7 +713,8 @@
712713
if (areSameValue(this.value_, this.oldValue_))
713714
return false;
714715

715-
this.value = this.valueFn ? this.valueFn(this.value_) : this.value_;
716+
this.value = this.transformFn ? this.transformFn(this.value_)
717+
: this.value_;
716718
this.reportArgs = [this.value, this.oldValue];
717719
return true;
718720
},
@@ -723,7 +725,8 @@
723725
this.observedSet.reset();
724726

725727
this.value_ = this.path.getValueFrom(this.object, this.observedSet);
726-
this.value = this.valueFn ? this.valueFn(this.value_) : this.value_;
728+
this.value = this.transformFn ? this.transformFn(this.value_)
729+
: this.value_;
727730

728731
if (this.observedSet)
729732
this.observedSet.cleanup();
@@ -734,17 +737,17 @@
734737
},
735738

736739
setValue: function(newValue) {
737-
if (!this.path)
738-
return;
739-
if (typeof this.setValueFn === 'function')
740-
newValue = this.setValueFn(newValue);
741-
this.path.setValueFrom(this.object, newValue);
740+
if (this.setValueFn)
741+
this.setValueFn(newValue);
742+
else if (this.path)
743+
this.path.setValueFrom(this.object, newValue);
742744
}
743745
});
744746

745-
function CompoundPathObserver(callback, target, valueFn) {
747+
function CompoundPathObserver(callback, target, transformFn, setValueFn) {
746748
Observer.call(this, undefined, callback, target);
747-
this.valueFn = valueFn;
749+
this.transformFn = transformFn;
750+
this.setValueFn = setValueFn;
748751

749752
this.observed = [];
750753
this.values = [];
@@ -790,7 +793,7 @@
790793
var value = path.getValueFrom(object, this.observedSet);
791794
var oldValue = this.values[i/2];
792795
if (!areSameValue(value, oldValue)) {
793-
if (!anyChanged && !this.valueFn) {
796+
if (!anyChanged && !this.transformFn) {
794797
this.oldValues = this.oldValues || [];
795798
this.changeFlags = this.changeFlags || [];
796799
for (var j = 0; j < this.values.length; j++) {
@@ -799,7 +802,7 @@
799802
}
800803
}
801804

802-
if (!this.valueFn)
805+
if (!this.transformFn)
803806
this.changeFlags[i/2] = true;
804807

805808
this.values[i/2] = value;
@@ -817,8 +820,8 @@
817820
if (!this.getValues())
818821
return;
819822

820-
if (this.valueFn) {
821-
this.value = this.valueFn(this.values);
823+
if (this.transformFn) {
824+
this.value = this.transformFn(this.values);
822825

823826
if (areSameValue(this.value, this.oldValue))
824827
return false;
@@ -835,11 +838,11 @@
835838
sync: function(hard) {
836839
if (hard) {
837840
this.getValues();
838-
if (this.valueFn)
839-
this.value = this.valueFn(this.values);
841+
if (this.transformFn)
842+
this.value = this.transformFn(this.values);
840843
}
841844

842-
if (this.valueFn)
845+
if (this.transformFn)
843846
this.oldValue = this.value;
844847
},
845848

tests/test.js

+55-5
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,14 @@ suite('PathObserver Tests', function() {
343343
});
344344

345345
test('Path setValue', function() {
346-
var arr = {};
346+
var obj = {};
347347

348-
arr.foo = 'bar';
349-
observer = new PathObserver(arr, 'foo', callback);
350-
arr.foo = 'baz';
348+
obj.foo = 'bar';
349+
observer = new PathObserver(obj, 'foo', callback);
350+
obj.foo = 'baz';
351351

352352
observer.setValue('bat');
353-
assert.strictEqual(arr.foo, 'bat');
353+
assert.strictEqual(obj.foo, 'bat');
354354
assertPathChanges('bat', 'bar');
355355

356356
observer.setValue('bot');
@@ -360,6 +360,27 @@ suite('PathObserver Tests', function() {
360360
observer.close();
361361
});
362362

363+
test('Path setValueFn', function() {
364+
var obj = { foo: 1 };
365+
function setValueFn(value) {
366+
obj.foo = 2*value;
367+
}
368+
369+
observer = new PathObserver(obj, 'foo', callback, undefined, undefined,
370+
setValueFn);
371+
obj.foo = 2;
372+
373+
observer.setValue(2);
374+
assert.strictEqual(obj.foo, 4);
375+
assertPathChanges(4, 1);
376+
377+
observer.setValue(8);
378+
observer.reset();
379+
assertNoChanges();
380+
381+
observer.close();
382+
});
383+
363384
test('Degenerate Values', function() {
364385
var emptyPath = Path.get();
365386
observer = new PathObserver(null, '', callback);
@@ -877,6 +898,35 @@ suite('CompoundPathObserver Tests', function() {
877898
observer.close();
878899
});
879900

901+
test('setValueFn', function() {
902+
var obj = { foo: 1, bar: 2 };
903+
904+
function valueFn(values) {
905+
return values[0] + values[1];
906+
}
907+
908+
function setValueFn(value) {
909+
obj.foo = value;
910+
}
911+
912+
observer = new CompoundPathObserver(callback, undefined, valueFn,
913+
setValueFn);
914+
observer.addPath(obj, 'foo');
915+
observer.addPath(obj, 'bar');
916+
observer.start();
917+
918+
observer.setValue(2);
919+
assert.strictEqual(obj.foo, 2);
920+
assertPathChanges(4, 3);
921+
922+
observer.setValue(8);
923+
observer.reset();
924+
assertNoChanges();
925+
926+
observer.close();
927+
});
928+
929+
880930
test('Degenerate Values', function() {
881931
var model = {};
882932

0 commit comments

Comments
 (0)