Skip to content

Instantly share code, notes, and snippets.

@hellobrian
Created December 19, 2019 06:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hellobrian/0e50940d5fcbabbda5661194530517c8 to your computer and use it in GitHub Desktop.
Save hellobrian/0e50940d5fcbabbda5661194530517c8 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const repCounterMachine = Machine(
{
id: "repCounter",
initial: "counting",
context: {
goal: 10,
count: 10,
doneMessage: null
},
states: {
restarting: {
entry: ["setGoal"],
on: {
"": "counting"
}
},
counting: {
on: {
increase: {
actions: ["increase"],
cond: "countLessThanGoal"
},
decrease: {
actions: ["decrease"],
cond: "countGreaterThanZero"
},
set_count_to_goal: {
actions: ["setGoal"],
cond: "countLessThanGoal"
},
set_count_to_zero: {
actions: ["setZero"],
cond: "countNotZero"
},
finish: "done"
}
},
done: {
entry: ["doneMessage"],
on: {
retry: "counting"
}
}
}
},
{
actions: {
increase: assign({ count: context => context.count + 1 }),
decrease: assign({ count: context => context.count - 1 }),
setGoal: assign({
count: context => context.goal
}),
setZero: assign({
count: 0
}),
doneMessage: assign({
doneMessage: context =>
`You did ${context.count} rep${context.count === 1 ? "" : "s"}`.trim()
})
},
guards: {
countGreaterThanZero: context => context.count > 0,
countNotZero: context => context.count !== 0,
countLessThanGoal: context => context.count < context.goal
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment