angular / watchtower.js

ES6 Port of Angular.dart change detection code.
Other
409 stars 30 forks source link

Reaction functions should always get the current value #39

Open tbosch opened 10 years ago

tbosch commented 10 years ago

When doing two watches with a reaction function that updates the value for the other watch, we get an infinite cycle right now:

setup({a:null, b:null});
watchGrp.watch(parse('a'), function(newValue) { context.b = newValue; });
watchGrp.watch(parse('b'), function(newValue) { context.a = newValue; });

context.a = 1;
context.b = 2;
var count;
do (
  count = watchGroup.detectChanged();
} while (count);

The problem here is that the value passed to the reaction function might have been updated by another reaction function and therefore might be stale. The right thing would be to read the current value again just before calling the reaction function.

Note that the people who created Object.observe must have thought about this problem, as the change record that Object.observe returns only contains the old value, but not the new value...

Also talked to @jbdeboer and he wants to change this for AngularDart as well...

tbosch commented 10 years ago

See https://github.com/angular/angular.dart/issues/1113

Nevraeka commented 10 years ago

+1 @tbosch