gerich-home / it-depends

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

Postpone subscriptions/unsubscriptions #68

Open gerich-home opened 8 years ago

gerich-home commented 8 years ago

In certain situations there is no point in subscribing or unsubscribing immediately. We should postpone deep subscriptions until a change was made.

For the case of bulkChange subscriptions/unsubscriptions should be postponed until the end of bulk change.

Given the following definitions:

var x = [];
for (var j = 0; j < dependenciesCount; j++) {
    x.push(itDepends.value(-1));
}

var a = itDepends.computed(function() {
    var z = 0;
    for (var j = 0; j < dependenciesCount; j++) {
        z += x[j]();
    }
    return z;
});

var b = itDepends.value(true);

var c = itDepends.computed(function() {
    return b() ? a() : 0;
});

the following scenarios should work with approximately the same performance:

c.onChange(function() {}); // triggers massive subscriptions currently
b.write(false); // but all of them get disabled here
b.write(false);
c.onChange(function() {});

Given the following definitions:

var x = [];
for (var j = 0; j < dependenciesCount; j++) {
    x.push(itDepends.value(-1));
}

var a = itDepends.computed(function() {
    var z = 0;
    for (var j = 0; j < dependenciesCount; j++) {
        z += x[j]();
    }
    return z;
});

var b = itDepends.value(false);

var c = itDepends.computed(function() {
    return b() ? a() : 0;
});

var s = a.onChange(function() {});
x[0].write(1);

the following scenarios should also work with approximately the same performance:

s.disable(); // should not unsubscribe from a and all x[i] immediately!
var s2 = c.onChange(function() {}); // because the replacement subscription is created here
b.write(true); // but becomes active only here
var s2 = c.onChange(function() {});
b.write(true);
s.disable(); // does not trigger massive unsubscriptions currently