simple and typesafe finite-state machines. Inspired by zustand and xstate.
Background
I like automata-based programming. Certain problems are solved elegantly when modelled as finite-state machines, particularly
those related to the logic of user interfaces. It's a great tool in a programmer's toolbox.
The most popular js/ts fsm library is XState. It's pretty good but verbose and difficult to learn, it's a big investment to drop in a team setting. Furthermore, type-safety in XState seems like an afterthought.
For me one of the biggest benefits of finite-state machines is that they enable you to make illegal states unrepresentable, and a large chunk of that is missing from xstate. So that's why I built this library
Features
tyfsm's state machines are centered around leveraging the type-narrowing features of Typescript's discriminated unions.
The core of the state machine is a discriminated union, where each state is a single variant of the union. This gives us neat type-safety narrowing features.
Example
You define your state machine in Typescript's type system:
type WebsocketMachine = StateMachine< // First define your states and the data each state carries { idle: { addr: string; }; connecting: { socket: WebSocket; addr: string; }; connected: { socket: WebSocket; addr: string; }; error: { errorMessage: string; }; }, // Now define the valid transitions between states {
Once you've modelled your state machine, you can create a store in a similar way like in zustand:
// Create the initial stateconst initial: State<"idle"> = { kind: "idle", addr: "ws://localhost:8302",};export const useWebsocketStore = create<WebsocketMachine, Actions>( initial, // Create the machine's actions: // // `get` is a function that returns the current state of the machine // `transition` is a function that transitions the machine, with the following parameters: // * the current state // * the next state // * the data for the next state (get, transition) => ({ connect(idleState) { const socket = new WebSocket(idleState.addr);
Note that all the actions are type-safe. You can only call actions.connect(state) when state is in the idle state. Similarly, the errorMessage property is only available on the
state object when the machine is in the error state.
todo
explore hierarchical designs
transaction functions for async actions so state doesn't unexpectedly change
idle: ["connecting"];
connecting: ["error", "connected"];
connected: ["idle", "error"];
error: ["idle"];
}
>;
// Write a utility to make selecting states easy
type State<K extends WebsocketMachine["allStates"]> = SelectStates<
WebsocketMachine,
K
>;
// Now define actions for the machine
type Actions = {
// This action can only be called in the `idle` state and transitions