Skip to content

Instantly share code, notes, and snippets.

@conorhastings
Last active April 8, 2021 17:39
Show Gist options
  • Save conorhastings/dbd1a33d13497ac79f74e9ad948628ee to your computer and use it in GitHub Desktop.
Save conorhastings/dbd1a33d13497ac79f74e9ad948628ee to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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