Skip to content

Instantly share code, notes, and snippets.

@gsasouza
Last active October 4, 2021 20:13
Show Gist options
  • Save gsasouza/225e37b525d98f8bb3f684c6c0e52ea2 to your computer and use it in GitHub Desktop.
Save gsasouza/225e37b525d98f8bb3f684c6c0e52ea2 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const isWaiting = ({ status }) => status === 'WAITING'
const isError = ({ status }) => status === 'ERROR'
const isDone = ({ status }) => status === 'DONE'
const shouldRetry = ({ retryCount }) => retryCount <= 3
const creatingAccountMachine = Machine(
{
id: 'creatingAccountMachine',
initial: 'fetchStatus',
context: { retryCount: 0, status: 'WAITING'},
states: {
fetchStatus: {
invoke: {
src: 'fetchStatus',
onDone: {
target: 'statusTick',
actions: assign({
status: (context, event) => 'DONE',
}),
},
onError: {
target: 'statusTick',
},
},
},
statusTick: {
entry: send({ type: 'NEXT' }, { delay: () => 2000 }),
on: {
NEXT: [
{ target: 'fetchStatus', cond: isWaiting },
{ target: 'createOnSupplier', cond: isDone },
{ target: 'errorService', cond: isError },
],
},
},
createOnSupplier: {
invoke: {
src: 'createBuyerOrgSupplier',
onDone: {
target: 'done',
},
onError: {
actions: assign({ retryCount: ({ retryCount }) => retryCount + 1 }),
target: 'retryCreateOnSupplier',
},
},
},
retryCreateOnSupplier: {
entry: send({ type: 'NEXT' }),
on: {
NEXT: [
{ target: 'createOnSupplier', cond: shouldRetry },
{ target: 'errorSupplier' },
],
},
},
errorService: {
type: "final"
},
errorSupplier: {
type: 'final',
},
done: {
type: 'final',
},
},
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment