Last active
May 5, 2020 07:31
-
-
Save afbroman/ca2441986f4f64b582acde7b721031e8 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains 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: [], | |
message: '', | |
}, | |
states: { | |
idle: { | |
on: { | |
FETCH: 'pending', | |
}, | |
}, | |
pending: { | |
invoke: { | |
src: 'fetchData', | |
onDone: { target: 'successful', actions: ['setResults'] }, | |
onError: { target: 'failed', actions: ['setMessage'] }, | |
}, | |
}, | |
failed: { | |
on: { | |
FETCH: 'pending', | |
}, | |
}, | |
successful: { | |
initial: 'unknown', | |
on: { | |
FETCH: 'pending', | |
}, | |
states: { | |
unknown: { | |
on: { | |
'': [ | |
{ | |
target: 'withData', | |
cond: 'hasData', | |
}, | |
{ target: 'withoutData' }, | |
], | |
}, | |
}, | |
withData: {}, | |
withoutData: {}, | |
}, | |
}, | |
}, | |
}, | |
{ | |
actions: { | |
setResults: assign((ctx, event) => ({ | |
results: event.data, | |
})), | |
setMessage: assign((ctx, event) => ({ | |
message: event.data, | |
})), | |
}, | |
guards: { | |
hasData: (ctx, event) => { | |
return false; | |
}, | |
}, | |
}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment