Skip to content

Instantly share code, notes, and snippets.

@brookslybrand
Created February 11, 2021 19:49
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/4de204088198de791d6a6b3ba2f3d387 to your computer and use it in GitHub Desktop.
Save brookslybrand/4de204088198de791d6a6b3ba2f3d387 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
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