Created
December 19, 2019 06:50
-
-
Save hellobrian/0e50940d5fcbabbda5661194530517c8 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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