StreetStrider / fluh

simple & easy functional reactive library with atomic push strategy
Other
17 stars 1 forks source link

Stateless architecture #3

Open StreetStrider opened 1 year ago

StreetStrider commented 1 year ago

State management libraries usually feature something like createStore method. The purpose of it is to create state root for the current state management instance. This approach is correct, but I dislike it for additional level of instancing. But the alternative is worse: you need to make your module stateful by creating a single state instance inside of the imported module. It is not a big deal. Some packages have their own global state, (like fastdom and flyd itself), but it's not a perfect architecture (however, the real issues are small).

Right now fluh is stateful module. It has global Realm object designated for state. This approach does not break anything, since you can store multiple graphs in one realm, however there are small downsides. For instance, current cache system would invalidate all caches on every graph if one of the graphs changes.

I wonder, is there a way to solve all statefulness issues without introducing explicit «createStore»?

Solution might be to make graphs self-organize. Each graph should create single realm object and connect all Buds to it. In case of extension all newly created Buds (all of them are descendants) would be also connected to the same realm. In case of graph split, which essentially happens when partial ending graph, realm should also split.

Right now Buds have both direct and reverse references for finalization and memory cleaning purposes, so Buds in one graph can reach each other freely. It would not be a problem even to designate leader Bud if it would be required by new design.