Created
March 11, 2020 15:03
-
-
Save avanslaars/a3a96812e289a2aba4854a7223ec6f11 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetchMachine = Machine({ | |
id: 'fetch', | |
initial: 'idle', | |
context: { | |
results: [], | |
retries: 0, | |
}, | |
states: { | |
idle: { | |
on: { | |
FETCH: 'loading', | |
}, | |
}, | |
loading: { | |
invoke: { | |
src: 'loadData', | |
onDone: { target: 'success', actions: 'handleData' }, | |
onError: 'failure', | |
}, | |
}, | |
success: {}, | |
terminated: {}, | |
awaitingRetry: { | |
after: { | |
FETCH_DELAY: 'loading', | |
}, | |
}, | |
failure: { | |
on: { | |
'': [ | |
{ | |
target: 'awaitingRetry', | |
actions: 'increment', | |
cond: 'withinLimit', | |
}, | |
{ target: 'terminated' }, | |
], | |
// FETCH: { target: 'loading', actions: 'increment' }, | |
}, | |
}, | |
}, | |
}, | |
{ | |
services: { | |
loadData: () => Promise.reject('Boom') | |
}, | |
actions: { | |
handleData: assign({ results: (_, event) => event.data }), | |
increment: assign({ retries: context => context.retries + 1 }), | |
}, | |
delays: { | |
FETCH_DELAY: context => context.retries * 500, | |
}, | |
guards: { | |
withinLimit: (context) => context.retries < 4 | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment