Created
July 13, 2021 20:26
-
-
Save schicks/e15d21c44b36667c0dae85b0a8620541 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
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const maxTime = 5000 | |
const randomSleep = () => { | |
return new Promise((resolve, reject) => { | |
const success = maxTime * Math.random() | |
const failure = maxTime * Math.random() + 100 | |
setTimeout(resolve, success) | |
setTimeout(reject, failure) | |
}) | |
} | |
const fetchMachine = Machine({ | |
id: 'fetch', | |
initial: 'loading', | |
context: { | |
retries: 0 | |
}, | |
states: { | |
loading: { | |
invoke: { | |
id: 'getResults', | |
src: randomSleep, | |
onDone: 'success', | |
onError: 'failure' | |
}, | |
after: { | |
[maxTime]: 'failure' | |
} | |
}, | |
success: { | |
type: 'final' | |
}, | |
failure: { | |
type: 'final' | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment