Skip to content

Instantly share code, notes, and snippets.

@peterfarrell
Created July 19, 2022 15:36
Show Gist options
  • Save peterfarrell/4a76327f6cbfca5d79c87235adf0067a to your computer and use it in GitHub Desktop.
Save peterfarrell/4a76327f6cbfca5d79c87235adf0067a to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const saveMachine = Machine({
initial: 'idle',
context: {
data: undefined,
error: undefined,
},
states: {
idle: {
on: { SAVE: 'saving' },
},
saving: {
invoke: {
src: 'saveData',
onDone: {
target: 'success',
actions: assign({ data: (_context, event) => event.data }),
},
onError: {
target: 'failure',
actions: assign({ error: (_context, event) => event.data }),
},
},
on: { RESET: 'idle' },
},
success: {
on: {
RESET: 'idle',
SAVE: 'saving',
},
after: {
2000: 'idle',
},
exit: context => {
context.data = undefined
},
},
failure: {
entry: context => {
context.data = undefined
},
on: {
RESET: 'idle',
SAVE: 'saving',
},
exit: context => {
context.error = undefined
},
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment