statelyai / xstate

Actor-based state management & orchestration for complex app logic.
https://stately.ai/docs
MIT License
26.51k stars 1.22k forks source link

Bug: History state in parallel machines is changing states unexpectedly #4900

Closed ccapndave closed 1 month ago

ccapndave commented 1 month ago

XState version

XState version 5

Description

image

Given the above machine, send the following sequence of events in the simulator:

  1. child1.next
  2. child2.next
  3. child1.history
  4. child1.history
  5. child1.history ...

Expected result

Whenever history is called, both machines stay in their previous state as (I think?) is expected. I use this pattern in my application to intercept events that need to do actions without changing any states.

Actual result

Although we get this result in the simulator, in real XState code it behaves differently. If both machines are in their Next states and child1.history is sent, one of the machines changes back to its initial state.

This can be reproduced by clicking on the buttons in the codesandbox. In this case when both machines are in Next and child1.history is sent, Child1 goes back to its initial state.

Reproduction

https://codesandbox.io/p/sandbox/jolly-meadow-5sxz66

Additional context

Note that in my actual app in fact it is Child2 (i.e. the machine without the history state) which goes back to its initial state and Child1 stays where it is.

davidkpiano commented 1 month ago

It says "Sandbox not found" - can you please make it available?

ccapndave commented 1 month ago

Sorry! Have a go now.

davidkpiano commented 1 month ago

Workaround (as discussed on Discord):