Skip to content

Instantly share code, notes, and snippets.

@avanslaars
Last active March 5, 2020 19:41
Show Gist options
  • Save avanslaars/23b6a145af0e075ea2502eb8db89e9c1 to your computer and use it in GitHub Desktop.
Save avanslaars/23b6a145af0e075ea2502eb8db89e9c1 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const fetchMachine = Machine(
{
id: 'fetch',
initial: 'idle',
context: {
results: [],
retryCount: 0,
},
states: {
idle: {
on: {
FETCH: 'loading',
},
},
loading: {
invoke: {
src: 'loadData',
onDone: { target: 'success', actions: 'handleData' },
onError: 'failure',
},
on: {
RESOLVE: 'success',
REJECT: 'failure',
},
},
success: {},
failure: {
after: {
FETCH_DELAY: [
{ target: 'loading', actions: 'increment', cond: 'withinLimit' },
{ target: 'terminated' },
],
},
},
terminated: {
type: 'final',
},
},
},
{
guards: {
withinLimit: () => true
},
actions: {
handleData: assign({ results: (_, event) => event.data }),
increment: assign({ retryCount: context => context.retryCount + 1 }),
},
delays: {
FETCH_DELAY: context => context.retryCount * 500,
},
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment