Created
December 10, 2019 18:34
-
-
Save isaacplmann/860f97dcf3792c75d97b9728131444e3 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: [], | |
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: { | |
on: { | |
FETCH: 'pending' | |
} | |
} | |
} | |
}, | |
{ | |
actions: { | |
setResults: assign((ctx, event) => ({ | |
results: event.data | |
})), | |
setMessage: assign((ctx, event) => ({ | |
message: event.data | |
})) | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment