Skip to content

Instantly share code, notes, and snippets.

@JayBee007
Last active September 11, 2021 06:54
Show Gist options
  • Save JayBee007/422e0625c0d8b0a5cda75581dda7b3d3 to your computer and use it in GitHub Desktop.
Save JayBee007/422e0625c0d8b0a5cda75581dda7b3d3 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const timerMachine = Machine({
id: 'timerMachine',
initial: "paused",
states: {
paused: {
on: {
START: {
target: 'running'
}
}
},
running: {
initial: 'round',
invoke: {
src: () => (cb) => {
let timerId = setInterval(() => {
cb('TICK')
}, 1000)
return () => clearInterval(timerId)
},
},
on: {
TICK: {
actions: assign({
secondsLeft: (ctx) => ctx.secondsLeft - 1
})
}
},
states: {
round: {
on: {
'': {
target: 'interval',
cond: (ctx) => ctx.secondsLeft < 0
}
}
},
interval: {}
},
on: {
PAUSED: {
target: 'paused'
}
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment