Last active
March 19, 2020 21:22
-
-
Save martypenner/f83e5af9dae0e7969a4cabef05927e0f to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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