gerich-home / it-depends

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

General change callback #5

Closed gerich-home closed 8 years ago

gerich-home commented 8 years ago

Need to have a way to know that any observable value just changed (in general) Simalar to #3, but it will give you the way to do not subscribe to the concrete observable value, but subscribe just to the general event.

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

var subscription = itDepends.onChange(function(value, from, to) {
    console.log('a: ' + (value === a) + ' (' + from+ ' -> ' + to + ')');
});

a(20); // outputs a: true (10 -> 20)
a(30); // outputs a: true (20 -> 30)
a(30); // outputs nothing
c(20); // outputs a: false (10 -> 20)
a(40); // outputs a: true (30 -> 40)

subscription.disable();

a(50); // outputs nothing

subscription.enable();

a(60); // outputs a: true (50 -> 60)
Thoughts:

disable should not keep the registration in the library, so when you disable and throw away the reference to subscription it gets garbage collected

gerich-home commented 8 years ago

Probably onChangeCallback should accept an array of changes because it can be put in bulk update block #4

gerich-home commented 8 years ago

Implemented. Decision made that array will be passed to onChanges (bulk changes callback). Readme needs to be updated with docs.

gerich-home commented 8 years ago

Docs added