Persisting a state *after* machine is settled #1265
Replies: 3 comments 7 replies
-
Actions are considered "fire & forget" so from our perspective they take near-zero time and are always considered as done after we fire them. I would recommend using let newState
interpreter.onTransition(state => (newState = state))
interpreter.send(event);
await this.storageAdapter.updateMachine(
id,
{ state: JSON.stringify(newState.toJSON()) }
); This would "resolve" all sync transitions etc but if you have any async work that you need to await then you need to track it within your machine and only store the state when you know that all async work has been finished. |
Beta Was this translation helpful? Give feedback.
-
We are currently in the process of designing some services that will use xstate for state management. It has occurred to me that for this use case, it might actually make sense to write our own custom interpreter tailored for the necessity to persist and rehydrate and the different flavor of listening for events in that context. Having run across this thread, and noticing the similarity of your send function to the bare-bones custom interpreter in the docs, I just thought I might suggest that as one possibility (albiet, requiring more work). |
Beta Was this translation helpful? Give feedback.
-
@ayashjorden Did you ever get this on-demand interpreter working? I have a similar usecase |
Beta Was this translation helpful? Give feedback.
-
Hi,
As a followup to the discussion on Gitter, it is still unclear how to persist a state of a machine after the machine has settled (finished processing all of the transitions/activities).
The Persisting State documentation provides the basic example but also includes a call to
resolveState
which I don't understand the explanation comment.In my scenario, I run machines in an on-demand:
A machine can run through several transitions before it finish processing all transitions and activities.
The following snippet is an excerpt of that flow:
Getting the updated state from
interpreter.send(event)
may not result in the most recent state if the machine has still some processing to do (transitions and activities, actions or anything else from MachineOptions that runs longer).In my use-case, I had to add a
sleep
to let the machine settle and only then get the state:The ugly solution I found is:
I'd appreciate any ideas/feedback on the above,
Yarden
Beta Was this translation helpful? Give feedback.
All reactions