Last active
April 8, 2021 17:39
-
-
Save conorhastings/dbd1a33d13497ac79f74e9ad948628ee 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 UserMachine = () => {}; | |
Machine( | |
{ | |
id: 'Get UserI', | |
initial: 'idle', | |
context: { | |
user: null, | |
retries: 0, | |
maxRetries: 3, | |
}, | |
states: { | |
idle: { | |
on: { | |
FETCH: 'loading', | |
}, | |
}, | |
loading: { | |
invoke: { | |
id: 'fetchUser', | |
src: (context, event) => | |
fetch('https://reqres.in/api/users/2').then((data) => data.json()), | |
onDone: { | |
target: 'resolved', | |
actions: assign({ | |
user: (_, event) => event, | |
}), | |
}, | |
onError: 'rejected', | |
}, | |
on: { | |
CANCEL: 'idle', | |
}, | |
}, | |
resolved: { | |
type: 'final', | |
}, | |
rejected: { | |
RETRY: { | |
target: 'loading', | |
actions: assign({ | |
retries: (context, event) => context.retries + 1, | |
}), | |
cond: 'canRetry', | |
}, | |
}, | |
}, | |
}, | |
{ | |
guards: { | |
canRetry: (context) => context.retries >= maxRetries | |
} | |
}, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment