Skip to content

Instantly share code, notes, and snippets.

@martypenner
Last active March 19, 2020 21:22
Show Gist options
  • Save martypenner/f83e5af9dae0e7969a4cabef05927e0f to your computer and use it in GitHub Desktop.
Save martypenner/f83e5af9dae0e7969a4cabef05927e0f to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const machine = Machine({
id: 'backoffFetch',
context: {
status: 'green',
retryCount: 0,
},
initial: 'fetching',
states: {
waiting: {
initial: 'success',
states: {
healthy: {},
sick: {},
dead: {},
},
after: {
FETCH_INTERVAL: 'fetching'
}
},
fetching: {
invoke: {
src: 'fetch',
onDone: 'waiting.healthy',
onError: [
{
target: 'waiting.dead',
cond: 'networkIsDead',
actions: 'incrementRetryCount'
},
{
target: 'waiting.sick',
actions: 'incrementRetryCount'
}
],
}
}
}
}, {
guards: {
networkIsDead: (ctx) => ctx.retryCount > 3,
incrementRetryCount: assign({
retryCount: (ctx) => ctx.retryCount + 1
})
},
delays: {
FETCH_INTERVAL: (ctx, event) => ctx.retryCount * 1000
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment