Skip to content

Instantly share code, notes, and snippets.

@martypenner
Last active March 19, 2020 21:27
Show Gist options
  • Save martypenner/a63355678a23d7ba1b601afe92b4c751 to your computer and use it in GitHub Desktop.
Save martypenner/a63355678a23d7ba1b601afe92b4c751 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,
},
type: 'parallel',
states: {
health: {
initial: 'healthy',
states: {
healthy: {
id: 'healthy'
},
sick: {
id: 'sick'
},
dead: {
id: 'dead'
},
},
},
polling: {
initial: 'fetching',
states: {
waiting: {
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 + 1 * 1000
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment