Created
April 27, 2020 00:25
-
-
Save ecancino/e03143775ad36fe9bb8d8fefbfcb10a0 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 fetchCuteAnimals = () => { | |
| return fetch('https://www.reddit.com/r/aww.json') | |
| .then(res => res.json()) | |
| .then(data => data.data.children.map(child => child.data)) | |
| } | |
| const cuteAnimalMachine = Machine({ | |
| id: 'cuteAnimals', | |
| initial: 'idle', | |
| context: { | |
| cuteAnimals: null, | |
| error: null, | |
| }, | |
| states: { | |
| idle: { | |
| on: { FETCH: 'loading' }, | |
| }, | |
| loading: { | |
| invoke: { | |
| id: 'fetchCuteAnimals', | |
| src: fetchCuteAnimals, | |
| onDone: { | |
| target: 'success', | |
| actions: [ | |
| assign({ | |
| cuteAnimals: (context, event) => event.data, | |
| }), | |
| ], | |
| }, | |
| onError: { | |
| target: 'failure', | |
| actions: [ | |
| assign({ | |
| error: (context, event) => event.data, | |
| }), | |
| ], | |
| }, | |
| }, | |
| }, | |
| success: { | |
| type: 'final', | |
| }, | |
| failure: { | |
| on: { | |
| RETRY: 'loading', | |
| }, | |
| }, | |
| }, | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment