Last active
December 20, 2022 00:37
-
-
Save abruzzi/99cc6141c8f25e5f79be0dfc98070e06 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const shouldRetry = (context, event) => { | |
return context.retries < 3; | |
}; | |
const fetchMachine = Machine({ | |
id: 'Order Status', | |
initial: 'idle', | |
context: { | |
retries: 0 | |
}, | |
states: { | |
idle: { | |
on: { | |
FETCH: 'loading' | |
} | |
}, | |
loading: { | |
on: { | |
RESOLVE: 'ready', | |
NOT_READY: [ | |
{ | |
target: 'failure', | |
cond: shouldRetry | |
}, | |
{ | |
target: 'error' | |
} | |
] | |
} | |
}, | |
ready: { | |
on: { | |
NOTIFY: 'notified', | |
ERROR: 'error' | |
} | |
}, | |
success: { | |
type: 'final' | |
}, | |
notified: { | |
type: 'final' | |
}, | |
error: { | |
type: 'final' | |
}, | |
failure: { | |
on: { | |
RETRY: { | |
target: 'loading', | |
actions: assign({ | |
retries: (context, event) => context.retries + 1 | |
}) | |
} | |
} | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment