Skip to content

Instantly share code, notes, and snippets.

@pke
Last active December 15, 2020 17:10
Show Gist options
  • Save pke/9ce4bb4840c35b4c3567adf98dacf8fb to your computer and use it in GitHub Desktop.
Save pke/9ce4bb4840c35b4c3567adf98dacf8fb to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const guards = {
canCountDown: context => !guards.countedDown(context),
countedDown: context => context.count === 0
}
const countdownMachine = Machine({
id: "countdown",
initial: "idle",
context: {
count: 10
},
states: {
idle: {
on: {
START: {
actions: "startCounting",
target: "counting"
}
}
},
counting: {
after: {
1000: {
target: "counting",
actions: "countdown",
cond: "canCountDown"
}
},
on: {
"": {
target: "done",
cond: "countedDown"
},
STOP: {
target: "idle",
actions: "reset"
}
}
},
done: {
type: "final",
on: {
RESTART: {
target: "counting",
actions: "reset"
}
}
}
}
}, {
guards,
actions: {
startCounting: assign({ startCount: context => context.count }),
reset: assign({ count: context => context.startCount }),
countdown: assign({ count: context => context.count - 1 })
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment