Created
October 9, 2019 14:26
-
-
Save brookslybrand/bc64403e794d27bfe8843bfa01a32397 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 incrementTime = assign({ | |
time: (context, e) => context.time + 1 | |
}); | |
// Guard to check if the max count was reached | |
function maxCountReached(context, event) { | |
return context.time >= 3; | |
} | |
const timerMachine = Machine( | |
{ | |
id: "timer", | |
initial: "paused", | |
context: { | |
time: 0 | |
}, | |
states: { | |
paused: { | |
on: { | |
TOGGLE: [ | |
{ | |
target: "playing", | |
actions: "resetTime", | |
cond: "maxCountReached" | |
}, | |
{ target: "playing" } | |
] | |
} | |
}, | |
playing: { | |
on: { | |
"": { | |
target: "paused", | |
cond: "maxCountReached" | |
}, | |
TOGGLE: "paused", | |
INCREMENT: { | |
target: "playing", | |
actions: "incrementTime" | |
} | |
} | |
} | |
}, | |
on: { | |
RESET: { | |
target: ".paused", | |
actions: "resetTime" | |
} | |
} | |
}, | |
{ | |
actions: { | |
incrementTime, | |
resetTime: assign({ | |
time: 0 | |
}) | |
}, | |
guards: { maxCountReached } | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment