@evanhahn/functional-state-machine
v0.2.0
Published
very simple state machine
Downloads
122
Readme
functional finite state machine for the javascript
If I'm hungry and I eat a salad, I get full. If I eat a burrito, I get too full. As time passes, I get hungrier and hungrier.
We can model this in JavaScript:
const myStates = {
hungry: {
eatSalad: "full",
eatBurrito: "tooFull",
timePasses: "hungry",
},
full: {
timePasses: "hungry",
},
tooFull: {
timePasses: "full",
},
};Now we can transition between them:
import { functionalStateMachine } from "@evanhahn/functional-state-machine";
// returns "hungry"
functionalStateMachine({
states: myStates,
initial: "full",
apply: ["timePasses"],
});
// returns "hungry"
functionalStateMachine({
states: myStates,
initial: "full",
apply: ["timePasses", "timePasses"],
});
// returns "tooFull"
functionalStateMachine({
states: myStates,
initial: "hungry",
apply: ["eatBurrito"],
});
// returns "full"
functionalStateMachine({
states: myStates,
initial: "hungry",
apply: ["eatBurrito", "timePasses"],
});