Skip to content

Instantly share code, notes, and snippets.

@brookslybrand
Created May 4, 2020 22:08
Show Gist options
  • Save brookslybrand/2a8c804455aea23bbe4aa319b2b38baf to your computer and use it in GitHub Desktop.
Save brookslybrand/2a8c804455aea23bbe4aa319b2b38baf to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const autoSaveMachine = Machine(
{
id: 'autoSaveMachine',
initial: 'saved',
states: {
saved: {
on: {
HAS_UNSAVED_DATA: 'waitingToSave'
}
},
waitingToSave: {
after: [
{
delay: 1000,
target: 'readyToSave'
}
]
},
readyToSave: {
on: {
SAVE_DATA: 'saving'
}
},
saving: {
invoke: {
src: 'uploadData',
onDone: 'saved',
onError: 'error'
}
},
error: {
on: {
RETRY: 'readyToSave'
}
}
}
},
{
services: {
uploadData: (ctx, e) => {
return Math.random() < 0.2 ? Promise.reject() : Promise.resolve()
}
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment