paldepind / flyd

The minimalistic but powerful, modular, functional reactive programming library in JavaScript.
MIT License
1.56k stars 85 forks source link

Atomic updates #7

Closed iofjuupasli closed 9 years ago

iofjuupasli commented 9 years ago

Dependency graph:

a < b
^   ^
c   |
^   |
d < result
var a = flyd.stream();
var b = flyd.stream([a], function(){return a() + 1});
var c = flyd.stream([a], function(){return a() + 100});
var d = flyd.stream([c], function(){return c() + 1000});

var result = flyd.stream([b, d], function(){
    console.log(b(), d());
    return b() + d();
});

a(1);
a(5);
a(11);

Output:

2 1101
6 1101
6 1105
12 1105
12 1111
paldepind commented 9 years ago

Yes! That's a good catch! Thanks a lot for this. That's going straight to the test suite :+1:

It appears that the approach I've used in Flyd is too primitive. Moving the definition of b to after the definition of d fixes the issue. But obviously Flyd should do the correct thing in all cases.

From the top of my head I think a breath first graph search will be adequate. I'll think more about it.

paldepind commented 9 years ago

Nope, simple breath first is definitely not going to cut it. We'll have to determine a proper evaluation order for the dependency graph.

paldepind commented 9 years ago

This should be fixed by 555cfac5aa4. The atomic updates should now be completely bulletproof. Unless you can find another breaking case I will close this issue :)