Created
January 29, 2021 16:15
-
-
Save stefanfrede/1130720edbdec76dbd93f5a36ead44c7 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: { | |
data: null, | |
error: null, | |
}, | |
states: { | |
idle: { | |
on: { | |
FETCH: 'loading', | |
}, | |
}, | |
loading: { | |
invoke: { | |
src: 'fetchData', | |
onDone: { target: 'resolved', actions: ['setdata'] }, | |
onError: { target: 'rejected', actions: ['seterror'] }, | |
}, | |
}, | |
resolved: { | |
initial: 'unknown', | |
on: { | |
FETCH: 'loading', | |
}, | |
states: { | |
unknown: { | |
always: [ | |
{ | |
target: 'withData', | |
cond: 'hasData', | |
}, | |
{ | |
target: 'withoutData', | |
}, | |
], | |
}, | |
withData: {}, | |
withoutData: {}, | |
}, | |
}, | |
rejected: { | |
on: { | |
FETCH: 'loading', | |
}, | |
}, | |
}, | |
}, | |
{ | |
actions: { | |
setdata: assign((_, event) => ({ | |
data: event.data, | |
})), | |
seterror: assign((_, event) => ({ | |
error: event.data, | |
})), | |
}, | |
guards: { | |
hasData: (context) => { | |
return context.data && context.data.length > 0; | |
}, | |
}, | |
}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment