Skip to content

Persisting state with actors #1515

Answered by davidkpiano
DirkWolthuis asked this question in Q&A
Discussion options

You must be logged in to vote

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);
});

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
4 replies
@DirkWolthuis
Comment options

@davidkpiano
Comment options

@Koleok
Comment options

@nirname
Comment options

Answer selected by DirkWolthuis
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
5 participants