it-fsm/examples/turnstile.ts

31 lines
681 B
TypeScript
Raw Normal View History

2021-08-23 11:33:20 +03:00
import { StateMachineBuilder } from "../fsm.ts";
const [locked, unlocked] = ["locked", "unlocked"] as const;
const smbTurnstile = new StateMachineBuilder()
.withStates([locked, unlocked])
.withTransitions([
[locked, { coin: unlocked }],
[unlocked, { push: locked }],
]);
async function main() {
const sm = smbTurnstile.build(locked);
function logCurrentState() {
2021-08-23 11:40:13 +03:00
console.log("current state", JSON.stringify(sm.currentState.name));
2021-08-23 11:33:20 +03:00
}
2021-08-23 11:40:13 +03:00
logCurrentState();
await sm.trigger("coin", {});
logCurrentState();
await sm.trigger("push", {});
logCurrentState();
await sm.trigger("push", {});
logCurrentState();
2021-08-23 11:33:20 +03:00
}
if (import.meta.main) {
main();
}