ds300 / derivablejs

Functional Reactive State for JavaScript and TypeScript
Apache License 2.0
515 stars 23 forks source link

Do cycles properly #8

Closed ds300 closed 9 years ago

ds300 commented 9 years ago

The current way to do cyclical graph structures is broken. It works by setting the value of atoms within reactions. This violates Havelock's three central tenets:

Using a reaction to propagate state ruins all the of these. The laziness goes away because reactions are eager by definition. The consistency goes away inside transactions because reactions don't run inside transactions. There are other very subtle consistency problems outside of transactions which aren't worth enumerating here. Finally, the automatic memory management doesn't apply for reactions so that goes out the window too.

I think I figured out how to fix this. It involves creating a new derivation type which I'll call loop nodes for the time being. I've got some of the details written down in a notebook, I'll try to get around to the typing them up later.

The API would look a bit like this:

const a = atom("");
const as = a.derive(a => a + "a").loop(a).protect(5)
as.get();
; => "aaaaa"
ds300 commented 9 years ago

After thinking about this a bit more I can't seem to reconcile consistency with automatic memory management for cycles. I can get one or the other, but never both.

Bummer.

ggoodman commented 9 years ago

:+1: