Skip to content

Instantly share code, notes, and snippets.

@pke
Last active November 19, 2019 22:35
Show Gist options
  • Save pke/fc4b217d61ff725ce08f0d42744ee951 to your computer and use it in GitHub Desktop.
Save pke/fc4b217d61ff725ce08f0d42744ee951 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const countdownMachine = Machine({
id: "countdown",
initial: "idle",
context: {
count: 10
},
states: {
idle: {
on: {
START: "counting"
}
},
counting: {
after: {
1000: [
{
target: "done",
cond: "countedDown"
},
{
target: "counting",
actions: "countdown"
}
]
},
on: {
STOP: {
target: "idle",
actions: "reset"
}
}
},
done: {
type: "final"
}
}
}, {
guards: {
countedDown: context => context.count === 1
},
actions: {
reset: assign({ count: 10 }),
countdown: assign({ count: context => context.count - 1 })
},
delays: {
countdown: (context, event) => {
return context.count * 1000
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment