Skip to content

Instantly share code, notes, and snippets.

@arekmaz
Last active March 8, 2020 11:15
Show Gist options
  • Save arekmaz/17dd1cf51479aeee821ccfe89a138f3c to your computer and use it in GitHub Desktop.
Save arekmaz/17dd1cf51479aeee821ccfe89a138f3c 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 fetchMachine = Machine({
id: 'fetch',
initial: 'gettingFromCache',
context: {
fetchTimes: 0,
fetchRetryCount: 4
},
states: {
gettingFromCache: {
on: {
cacheReadError: [
{
target: 'error',
cond: context => context.fetchTimes > 0
},
{
target: 'fetching'
}
],
cacheReadSuccess: 'success'
}
},
fetching: {
on: {
fetchSuccess: {
target: 'writingToCache',
actions: assign({
fetchTimes: (context, event) => context.fetchTimes + 1
})
},
fetchError: [
{
target: 'error',
cond: context => context.fetchTimes >= context.fetchRetryCount,
actions: assign({
fetchTimes: (context, event) => context.fetchTimes + 1
})
},
{
target: 'fetching',
actions: assign({
fetchTimes: (context, event) => context.fetchTimes + 1
})
}
]
}
},
writingToCache: {
on: {
cacheWritingSuccess: 'gettingFromCache',
cacheWritingError: 'error'
}
},
success: {
type: 'final'
},
error: {
type: 'final'
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment