Skip to content

Instantly share code, notes, and snippets.

@massimoselvi
Last active December 20, 2020 08:10
Show Gist options
  • Save massimoselvi/0291cafd5f583c165a3ef1dd35bf02af to your computer and use it in GitHub Desktop.
Save massimoselvi/0291cafd5f583c165a3ef1dd35bf02af to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
/*
* FETCH MACHINE (work in progress)
* preview: https://xstate.js.org/viz/?gist=0291cafd5f583c165a3ef1dd35bf02af
*/
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const sellMachine = Machine({
id: 'sellVehicle',
initial: 'idle',
context: {
busy: undefined,
attempts: 0,
retries: 0
},
states: {
idle: {
on: {
FETCH_LIST: {
target: 'loading',
actions: assign({
fetchingType: (ctx, event) => ctx.busy = 'LIST'
}),
cond: ctx => {
ctx.busy = 'fetchingList'
return ctx.attempts < 3
}
},
FETCH_ITEM: {
target: 'loading',
actions: assign({
fetchingType: (ctx, event) => ctx.busy = 'ITEM'
}),
cond: ctx => {
ctx.busy = 'fetchingItem'
return ctx.attempts < 3
}
}
}
},
loading: {
entry: assign({
attempts: ctx => ctx.attempts + 1
}),
after: {
TIMEOUT: 'failure'
},
on: {
RESOLVE: 'success',
REJECT: 'failure'
}
},
success: {
// type: 'final'
initial: 'first',
states: {
first: {
on: {
'': {
target: 'second',
cond: 'maxAttempts'
},
NEXT: 'second'
}
},
second: {
on: {
NEXT: 'third'
}
},
third: {
type: 'final'
}
}
},
failure: {
on: {
RETRY: {
target: 'loading',
actions: assign({
retries: (context, event) => context.retries + 1
})
}
}
}
}
},
{
guards: {
maxAttempts: ctx => ctx.attempts >= 3,
listLoaded: ctx => ctx.busy === 'fetchingList',
itemLoaded: ctx => ctx.busy === 'fetchingItem',
},
delays: {
TIMEOUT: 2000
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment