Last active
May 20, 2020 21:12
-
-
Save andrewgordstewart/507bec08cfc4986328430d7ef23c678d 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 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