Last active
March 5, 2020 19:41
-
-
Save avanslaars/23b6a145af0e075ea2502eb8db89e9c1 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: [], | |
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