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
Reaction order flipped between v4 and v5 #1916
Comments
In general you shouldn't rely on reactions execution order. |
The order is deterministic though, and even if it may be difficult to predict, that does not make it unpredictable, as the example above shows. It seems as though v5 uses some sort of queue whereas v4 uses a stack, but this change was not documented. |
therefore the term "practically"
https://github.com/mobxjs/mobx/blob/master/CHANGELOG.md#breaking-changes |
The idea of a side effect in general is that it happens outside the system itself; in other words, it's effects are not programmatically observable. If the order does matter, that is usually a code smell, indicating that there is some shared state, which should be a computed instead of a reaction, or that the reactions should simply be merged. Sure, the order is deterministic though, there is no Math.random() in there or something. But MobX doesn't give any guarantees or rules for what the ordering will be. You might want to share your actual problem; as there might be unidiomatic use of reactions, and people could suggest a more idiomatic usage pattern. Otherwise this issue can be closed. |
I've refactored so that this is no longer an issue, but what I was doing was setting observable values to true in response to async tasks/requests, and expecting that I could use when statements like promises (where handlers are called in the order they are attached). This is the case in mobx 5, but not in 4. |
You can sort of chain when(cond, () => {
when(cond, () => {
when(cond, () => {
/* ... */
}
})
}) However I don't think it's practically usable anywhere. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs or questions. |
It seems like reactions are run in the order they are created in v5, but in the opposite order in v4.
See https://stackblitz.com/edit/typescript-mjc2uo?file=index.ts and change the mobx version to see this behaviour.
Is there a way to get v5 execution order in v4 (I'm currently trying to downgrade, and was expecting it to remain the same).
The text was updated successfully, but these errors were encountered: