Skip to content

Instantly share code, notes, and snippets.

@andrewgordstewart
Last active May 20, 2020 21:12
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 andrewgordstewart/507bec08cfc4986328430d7ef23c678d to your computer and use it in GitHub Desktop.
Save andrewgordstewart/507bec08cfc4986328430d7ef23c678d to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const taskHandler = () => new Promise(resolve => setTimeout(() => {
console.log("Done task")
resolve()
}, 1000))
let taskCounter = 0
const taskManager = Machine({
context: {tasks: {}},
on: {
RUN_TASK: {
actions: assign({
tasks: (context, event) => ({
// In practice, I wouldn't overwrite an existing task id
...context.tasks,
[++taskCounter]: spawn(taskHandler, `task-${taskCounter}`, {sync: true})
})
})
}
// ...
},
initial: 'running',
states: {
running: {}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment