VictorTaelin / PureState

The stupidest state management library that works.
MIT License
310 stars 18 forks source link

Branching breaks the dependency detector #4

Open VictorTaelin opened 8 years ago

VictorTaelin commented 8 years ago

Seems like I completely missed an obvious point. As commented by on Reddit,

var x = state(true);
var y = state(2);
var z = state(() => x() ? 1 : y());
x(false);
console.log(z()); // yields `2` (correct)
y(3);
console.log(z()); // yields `2` (incorrect)

The dependency detector will never realize the presence of y() since the branch isn't executed. I see no obvious way to fix this issue.

VictorTaelin commented 8 years ago

There is a workaround, though, which is listing the dependencies with commas, like:

state(() => x(), y(), (x() ? 1 : y()))

But that is very demotivating, IMO. Maybe a explicit list of dependencies is the only way to do it.

darkelyte commented 8 years ago

Would it be possible to re-evaluate the dependencies each time the state is updated? I believe this is the approach Knockout.js takes for its computed observables.

AutoSponge commented 7 years ago

Using default params you could write it as:

state((a = x(), b = y()) => a ? 1 : b)