gerich-home / it-depends

Lightweight dependency tracking library for JavaScript
http://it-depends-js.github.io/
Other
1 stars 2 forks source link

optional onChange callbacks #3

Closed gerich-home closed 8 years ago

gerich-home commented 8 years ago

There should be an ability to write:

var a = itDepends.value(10);
var b = itDepends.computed(function() {
    return a() > 30 ? 30 : a();
});

var subscription = b.onChange(function(newValue, oldValue) {
    console.log(oldValue + ' -> ' + newValue);
});

a(20); // outputs 10 -> 20
a(30); // outputs 20 -> 30
a(40); // outputs nothing

subscription.disable();

a(20); // outputs nothing

subscription.enable();

a(10); // outputs 20 -> 10