Xstate: Rehydrating from persisted state breaks delayed transitions

Created on 27 Jul 2020  路  11Comments  路  Source: davidkpiano/xstate

Description
According to this section https://xstate.js.org/docs/guides/states.html#persisting-state in the documentation the state of running machines can be persisted and used to rehydrate the machine to this exact state later. This is a super useful feature in many ways and it worked great so far.
However if I add delayed transitions (https://xstate.js.org/docs/guides/delays.html#delayed-transitions) to my machine I run into problems. On a fresh machine without any previous state the delayed transitions work perfectly fine but as soon as the machine gets initialised with state that was previously persisted, the delayed transitions will not work anymore and the machine gets stuck.

Expected Result
Delayed transitions should also work if the machine was started using a previously persisted state.

Actual Result
Delayed transitions only work on machines started without an initial state.

Reproduction
Code Sandbox which shows the problem: https://codesandbox.io/s/dry-sound-q3fxm

Additional context
Can be reproduced with current xstate version 4.11.0

bug

Most helpful comment

I've identified the issue and am working on a fix.

All 11 comments

Your CodeSandbox seems to work fine. When I start the delayed transition, and then reload, the machine:

  1. successfully starts in that 'doingStuff' state
  2. successfully transitions to 'idle' after 2 seconds, as expected.

Am I missing something?

hmm that is strange. I uploaded a recording of what happens for me here: https://gfycat.com/distortedcrazyduck
For me with this sandbox as soon as the machine gets rehydrated it will get stuck in the 'doingStuff' state and not transition after the 2 second period.
I also tried the sandbox in a few different browsers with the same problem in all of them.

Aaah I think we are doing different things...
When reloading the window while in the 'doingStuff' state (which is what you did I guess) everything works fine in the rehydrated machine.
But if the delayed transition is allowed to transition back to the 'idle' state before reloading the window (which is what the recording posted above shows) the delayed transition will not be fired in the rehydrated machine when manually transitioning to 'doingStuff'.

I've identified the issue and am working on a fix.

Hi @davidkpiano, thanks a lot for fixing this! Any timeline on when the @xstate/react package with the fix will be released?

@mircostraessle Planning a release today!

Awesome 馃檹

@davidkpiano sorry to bother you again but as of now you released xstate 4.12 but no new version of @xstate/react which would contain the fix for this issue. It would really help to have this fix released so we are able to use delayed transitions again.
Thanks for your work!

Thanks for the reminder; will release a new RC today or tomorrow.

@davidkpiano wanted to check in to see if you are still planning to do a bugfix release for this @xstate/react or if there is something blocking that? If it is blocked until v1 or something like that I would have to look into fixing it temporarily in my project.

@mircostraessle Waiting on https://github.com/davidkpiano/xstate/pull/1472 to release.

Was this page helpful?
0 / 5 - 0 ratings