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

Bug: restoring state from serialized getPersistedSnapshot() breaks history behaviour #5178

Open
seanlennaerts opened this issue Jan 23, 2025 · 0 comments
Labels

Comments

@seanlennaerts
Copy link

seanlennaerts commented Jan 23, 2025

XState version

XState version 5

Description

I think we've discovered unexpected behaviour when restoring from a persisted snapshot that has been serialized and the machine's historyValue not being restored correctly.

Take the following sample checkout machine (link to stately):

Image

Say we go through the following path:

simulated.demo.mov

So far this looks good. The History node successfully remembers the previous "OrderSummary" state by returning to "OrderSummary.Ready" the second time around.

The problem appears to only happen if we persist the state of the machine, serialize it, and restore it.

Steps to reproduce

Start with this minimal reproduction: https://stackblitz.com/github/seanlennaerts/xstate-history-bug?file=README.md&view=editor

If you run it with npm run test you'll see that the snapshots for the original actor and the restored actor do not match:

Image

The first log is the snapshot from the original actor and the historyValue is an instance of StateNode. The second log is a snapshot of a second actor that restored the serialized state of the first actor and the historyValue is a pojo - I'm thinking this may have something to do with the bug.

So what actually happens when the second actor tries to go back to the OrderSummary history node?

Instead of returning to "OrderSummary.Ready" which the historyValue points to, the machine goes all the way to the initial state "ShippingAddress" - this is where the assertion at the end of the file fails.

Interestingly enough if we skip the serialization and restored from snapshot directly then the assertion at the end of the file succeeds.

Expected result

After restoring from serialized persisted snapshot the machine should be able to go back to a history node.

Actual result

The machine could not go to a history node and goes back to the initial state of the machine.

Reproduction

https://stackblitz.com/github/seanlennaerts/xstate-history-bug?file=README.md&view=editor

Additional context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant