Created
March 11, 2021 20:38
-
-
Save brookslybrand/14c8bbdc48a29ea005cb3781b8e919ef 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 timer = Machine( | |
{ | |
id: "timer", | |
initial: "idle", | |
context: { | |
timer: 0 | |
}, | |
states: { | |
idle: { | |
on: { | |
START: "running" | |
} | |
}, | |
running: { | |
invoke: { | |
id: "counting", | |
src: (context) => (callback) => { | |
const intervalId = setInterval(() => callback("COUNT"), 1000); | |
return () => clearInterval(intervalId); | |
} | |
}, | |
on: { | |
STOP: "idle", | |
COUNT: { | |
actions: assign({ | |
timer: (context) => context.timer + 1 | |
}) | |
} | |
} | |
} | |
}, | |
on: { | |
RESET: { | |
target: "idle", | |
actions: ["resetTimer"] | |
} | |
} | |
}, | |
{ | |
actions: { | |
resetTimer:assign({ | |
timer: 0 | |
}) | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment