Last active
March 19, 2020 21:27
-
-
Save martypenner/a63355678a23d7ba1b601afe92b4c751 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains 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, | |
}, | |
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