Skip to content
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

Closed
SableS opened this issue Feb 28, 2019 · 7 comments
Closed

Reaction order flipped between v4 and v5 #1916

SableS opened this issue Feb 28, 2019 · 7 comments

Comments

@SableS
Copy link

SableS commented Feb 28, 2019

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).

@urugator
Copy link
Collaborator

In general you shouldn't rely on reactions execution order.
Flipped or not, they don't run in creation order, but in invalidation order, which is dynamic - depends on the order in which state is mutated and on the order the subscriptions are created - which is practically unpredictable.
In other words - reactions can't be used as synchronous event listeners. Reactions should be discrete.

@SableS
Copy link
Author

SableS commented Mar 1, 2019

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.

@urugator
Copy link
Collaborator

urugator commented Mar 1, 2019

that does not make it unpredictable

therefore the term "practically"

this change was not documented

https://github.com/mobxjs/mobx/blob/master/CHANGELOG.md#breaking-changes
last point

@mweststrate
Copy link
Member

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.

@SableS
Copy link
Author

SableS commented Mar 4, 2019

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.

@urugator
Copy link
Collaborator

urugator commented Mar 4, 2019

I could use when statements like promises

You can sort of chain when with actually guaranteed execution order, but in has to be done in callback style:

when(cond, () => {
  when(cond, () => {
    when(cond, () => {
      /* ... */
    }
  })
})

However I don't think it's practically usable anywhere.

@SableS SableS closed this as completed Mar 7, 2019
@lock
Copy link

lock bot commented Jul 21, 2019

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.

@lock lock bot locked as resolved and limited conversation to collaborators Jul 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants