State machine and state transition management utility class
| src | ||
| .gitignore | ||
| biome.json | ||
| bun.lock | ||
| LICENSE | ||
| Makefile | ||
| package.json | ||
| README.md | ||
| tsconfig.json | ||
State Machine
State machine and state transition management utility class
Usage
The StateMachine class is generic and takes an enum defining the
possible states:
enum States {
Connecting,
Connected,
Disconnected,
}
const machine = new StateMachine<States>({
// The starting state of the machine
initial: States.Connecting,
// All state transitions.
// States must define an `in` transition for other states to be able to
// change to it.
transitions: {
// An empty object means that no state can transition to this.
// Useful especially for initial states that cannot be returned to.
[States.Connecting]: {},
// For each state, define `in` and optional `out` functions
[States.Connected]: {
in: {
[States.Connecting]: () => {
console.log("Connected!");
},
},
// Specifying a function instead of a map for in/out is a global transition
out: () => {
console.log("Oh, if we're not connected anymore, thats bad!");
},
},
[States.Disconnected]: {
// `in: true` is shorthand for "anything can transition to this"
// Specifying a global transition function for `in` also allows all
in: true,
},
},
});
// Prints "Connected!"
fakeEvents.on("connected", () => machine.change(States.Connected));
// Prints "Oh, if we're not connected anymore, that's bad!"
fakeEvents.on("error", () => machine.change(States.Disconnected));