Skip to content

Instantly share code, notes, and snippets.

@brookslybrand
Created October 9, 2019 14:26
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 brookslybrand/bc64403e794d27bfe8843bfa01a32397 to your computer and use it in GitHub Desktop.
Save brookslybrand/bc64403e794d27bfe8843bfa01a32397 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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