Skip to content

Instantly share code, notes, and snippets.

@Andarist
Created December 30, 2021 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Andarist/812dc699e7161ba31887af2d19e7b997 to your computer and use it in GitHub Desktop.
Save Andarist/812dc699e7161ba31887af2d19e7b997 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const createSingleState = () => ({
initial: "fetch",
states: {
fetch: {
invoke: {
src: "fetchSmth",
onDone: {
target: "done",
actions: "notifySingleSuccess"
}
}
},
done: {
type: "final"
}
}
});
const testMachine = Machine(
{
id: "test",
type: "parallel",
onDone: {
actions: "notifyBothSuccess"
},
states: {
first: createSingleState(),
second: createSingleState()
}
},
{
actions: {
notifySingleSuccess: () => console.log(Date.now(), "single success"),
notifyBothSuccess: () => console.log(Date.now(), "both success")
},
services: {
fetchSmth: () => {
const ms = Math.floor(Math.random() * 1000);
console.log("gonna resolve in", ms);
return new Promise(resolve =>
setTimeout(() => {
console.log(Date.now(), "resolving single");
resolve();
}, ms)
);
}
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment