Skip to content

Instantly share code, notes, and snippets.

@brookslybrand
Created March 11, 2021 20:38
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/14c8bbdc48a29ea005cb3781b8e919ef to your computer and use it in GitHub Desktop.
Save brookslybrand/14c8bbdc48a29ea005cb3781b8e919ef to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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