Last active
July 9, 2020 11:30
-
-
Save mrsln/63e1e5ae003af9343f3bde4c0b9b4739 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 playerMachine = Machine({ | |
initial: "init", | |
context: { | |
pin: "", | |
lastError: "", | |
}, | |
states: { | |
init: { | |
invoke: { | |
id: "getPin", | |
src: () => getPinFromLocalPlayerStore(), | |
onDone: [ | |
{ target: "fetchPin", cond: (_context, event) => !event.data }, | |
{ target: "playback", cond: (_context, event) => event.data }, | |
], | |
}, | |
}, | |
fetchPin: { | |
invoke: { | |
id: "fetchPin", | |
src: fetchPin, | |
onDone: { | |
target: "playback", | |
actions: assign({ pin: (_context, event) => event.data }), | |
}, | |
}, | |
}, | |
playback: { | |
initial: "idle", | |
states: { | |
idle: {}, | |
} | |
}, | |
}, | |
}); | |
async function fetchPin() { | |
const fetch = await fetchWithTimeout("https://www.random.org/integers/?num=1&min=1000&max=10000&col=5&base=10&format=plain&rnd=new"); | |
return await fetch.text(); | |
} | |
function fetchWithTimeout(url, options = {}, timeout = 7000) { | |
return Promise.race([ | |
fetch(url, options), | |
new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), timeout)), | |
]); | |
} | |
function getPinFromLocalPlayerStore() { | |
return Promise.resolve() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment