-
Notifications
You must be signed in to change notification settings - Fork 34
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
Doing multiple changes inside transaction()
results multiple invocations of observer subscribed via subscribe()
.
#184
Comments
Unfortunately this is the programmed behaviour... I know it's not intuitive though. It comes down to this phrasing: https://github.com/luwes/sinuous/blob/master/packages/sinuous/observable/src/observable.js#L51-L71 /**
* Creates a transaction in which an observable can be set multiple times
* but only trigger a computation once.
* @param {Function} fn
* @return {*}
*/
...
q.forEach(data => {
if (data._pending !== EMPTY_ARR) {
const pending = data._pending;
data._pending = EMPTY_ARR;
data(pending); // This calls all computeds linked to the observable
}
}); Each observable can only trigger a computation once, so I didn't like this behaviour so in my own reactive library (which is on indefinite hiatus/cancelled 😞) I implemented https://github.com/heyheyhello/haptic/blob/main/src/state/index.ts#L288-L317 /**
* Batch signal writes so only the last write per signal is applied. Values are
* committed at the end of the function call. */
...
signals.forEach((signal) => {
// Doesn't run any subscribed wires since `transactionCommit` is set
signal(signal.next); // Does not call computeds.
delete signal.next;
signal.wires.forEach((wire) => transactionWires.add(wire));
});
transactionCommit = false;
_runWires(transactionWires) // Call computeds, once each, in the correct order. |
You could make a PR for @luwes and he might accept it. Just collect computeds instead of running them and then batch call them afterwards. This would be a behavioural change tho...might break some apps. |
@heyheyhello Thanks for your reply. It seems like a bad idea to make a PR if it's designed to work in this way. And I solved my problem by adding a flag. By the way, Haptic is cool. |
Doing multiple changes inside
transaction()
results multiple invocations of observer subscribed viasubscribe()
.How to reproduce:
Link to https://CodePen.io/j-sen/pen/OJjNKVK
The text was updated successfully, but these errors were encountered: