Replies: 4 comments
-
The restored state contains all the information that the machine needs to execute actions. So to prevent actions from being executed, delete them: // Use machine.resolveState() to resolve the state definition to a new State instance relative to the machine
const resolvedState = toggleMachine.resolveState(previousState);
+delete resolvedState.actions;
const toggleService2 = interpret(toggleMachine)
.onTransition((state) => console.log(state.value))
.start(resolvedState); |
Beta Was this translation helpful? Give feedback.
-
But I don't want to delete them. Future transitions will need them. I think it's a common use case for serverless architecture or if you want to be resilient when server restarts. There is no point to send that email twice. |
Beta Was this translation helpful? Give feedback.
-
Future transitions will still execute actions as in the proposed solution you don't always delete actions (that is actually our of your control anyway when using the builtin interpreter) but only delete the actions from a single state pre hydration.
I, personally, agree with that - I believe that as we treat actions as fire and forget we shouldn't persist them by default. This would be a breaking change though so changing this will have to wait for v5 - and, of course, @davidkpiano would have to be on board with that too. |
Beta Was this translation helpful? Give feedback.
-
Yes, and the way this works in serverless architectures is by keeping a log of actions already executed in storage, so that when e.g., a serverless function replays events to get to its current state, it checks storage to see if the actions are already executed and doesn't re-execute them. This is how AWS Step Functions, Azure Durable Functions, etc. work. Given a
This makes sense, but yeah, would be a breaking change. |
Beta Was this translation helpful? Give feedback.
-
Hi
I'm trying to persist the current state and restore it later (from a database).
Here are the steps :
Go to state 'A' to 'B'. Executing action 'C' due to this transition.
Save the current state (Set to 'B').
When restoring the state action 'C' is executed again even if the transition has already happened.
Is it the expected result ?
If yes how can I avoid that ?
Thanks
https://codesandbox.io/s/exciting-sun-qt92p
Beta Was this translation helpful? Give feedback.
All reactions