@n7e/state-machine
v0.2.0
Published
A small library for building big state machines.
Maintainers
Readme
State Machine
A small library for building big state machines.
For further insights, read on.
Installation
To install this library use your favorite package manager. No additional steps are required to start using the library.
npm install @n7e/state-machineThis library is implemented in TypeScript but can be used with JavaScript without any additional steps.
Usage
This library's take on state machines differs a bit from traditional approaches. Instead of allowing viewing and manipulation from outside the state machine, it only exposes methods to tick and reset the state machine. Execution logic, state transitions and everything else is hidden. It's up to the state to request a transition if and when appropriate.
import { State } from "@n7e/state-machine";
class SomeState extends State {
public tick(): void {
// ...
if (someCondition) {
this.request(SomeOtherState.name);
return;
}
// ...
}
}Finite State Machine
A finite state machine is a state machine with a fixed and known set of states.
import { FiniteStateMachineFactory } from "@n7e/state-machine";
const stateMachine = new FiniteStateMachineFactorr(InitialState.name).createStateMachineWith([
new InitialState(),
new SomeOtherState()
]);
while (true) {
stateMachine.tick();
}