-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add State.update #80
Comments
Isn't this supposed to be bad practice tho? Why would you use this instead of the normal auto-observable? |
Updating states in a binding is generally a bad idea, yes. I'm working on the premise that people reach for it when other options are exhausted, an when they do, an awkard situation doesn't have to be made any worse. The above example is really bad, actually. Since autorun may fire more often than necessarily expected, updates should be idempotent (and just setting a state to a specific value is, while incrementing it definitely isn't). This closer to a valid use case: autorun(() -> {
currentWindow.update(cur -> switch cur {
case Offline if (!selectedOpponentIsOffline.value): None;
case Reward if (currentReward.ends.passed): RewardExpired;
default: cur;
});
}); Perhaps still not a great example, but for the same inputs, this will produce the same effect. With public function update(computeNext:T->T) {
var currentValue = Observable.untracked(() -> value);
var nextValue = computeNext(currentValue);
if (!this.getComparator().eq(nextValue, computeNext(nextValue))) throw 'update is not idempotent';
return nextValue;
} |
Should add an update function to
State
that can access the current value without creating dependencies. Particularly useful forautorun
, e.g.:The text was updated successfully, but these errors were encountered: