Persisting state with actors #1515
-
Hi all, What is the recommended way to persists the state of a machine that uses actors? I've made a codesandbox that demonstrates my problem. When saving the state to localstorage, the actors get reduced to an object with only a id key. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
If you want services/actors to be restarted, the best thing to do would be to try an event sourcing approach instead: https://codesandbox.io/s/event-sourcing-example-xe952?file=/src/index.js // Example: you can use localStorage instead
const persistedEvents = localStorage.getItem('events');
const service = interpret(machine)
.onTransition(state => {
if (state.changed !== undefined) {
// push event to local storage
}
});
service.start();
// replay events
persistedEvents.forEach(event => {
service.send(event);
}); |
Beta Was this translation helpful? Give feedback.
-
When replying the events, what happens to those states that instantiate a service that makes a POST request? Is it executed again or it's cancelled because it's immediately recives the |
Beta Was this translation helpful? Give feedback.
If you want services/actors to be restarted, the best thing to do would be to try an event sourcing approach instead: https://codesandbox.io/s/event-sourcing-example-xe952?file=/src/index.js