Created
February 11, 2021 19:49
-
-
Save brookslybrand/4de204088198de791d6a6b3ba2f3d387 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 timeLimit = 10; | |
const timerMachine = Machine( | |
{ | |
id: "timer", | |
initial: "reset", | |
context: { | |
t: 0 | |
}, | |
states: { | |
playing: { | |
invoke: { | |
id: "incInterval", | |
src: (context, event) => (callback, onReceive) => { | |
// This will send the 'INC' event to the parent every second | |
const id = setInterval(() => callback("INC"), 1000); | |
// Perform cleanup | |
return () => clearInterval(id); | |
} | |
}, | |
on: { | |
PAUSE: "paused", | |
RESET: { | |
target: "reset", | |
actions: "reset" | |
}, | |
INC: { | |
actions: assign({ t: (context) => context.t + 1 }) | |
} | |
} | |
}, | |
paused: { | |
on: { | |
PLAY: { | |
target: "playing", | |
actions: assign({ | |
t: (context) => (context.t === timeLimit ? 0 : context.t) | |
}) | |
}, | |
RESET: { | |
target: "reset", | |
actions: "reset" | |
} | |
} | |
}, | |
reset: { | |
on: { | |
PLAY: { | |
target: "playing" | |
} | |
} | |
} | |
} | |
}, | |
{ | |
actions: { | |
reset: assign({ | |
t: () => 0 | |
}) | |
} | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment