Skip to content

Instantly share code, notes, and snippets.

@abruzzi
Last active December 20, 2022 00:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Generated by XState Viz: https://xstate.js.org/viz
// 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