Use when managing objects that change over time through named transitions - like a counter
with increment and decrement operations, a shopping cart adding and removing items, or
a toggle managing on/off state with enable and disable transitions.
Concepts
State: An interface defining version data properties and transition methods
Version: The data properties of a State, excluding transition methods and
observers
Transition: A method that takes inputs and returns a new State with updated
version data
Update: A function that accesses current version data via this and returns partial
version data to be merged into the state
Observer: A function called asynchronously when state transitions occur
Manager: Housekeeping operations (snapshots, observers) accessed via manageState(state),
kept separate from State to avoid polluting user-defined interfaces
States are created by the State factory from a Seed where transition
methods are implemented as Update functions that return partial updates. These
updates are merged into a new immutable state, and transition methods return the
new state. Eliminates manual state spreading and ensures type-safe updates.
Basic Usage
Define a state interface with data properties and transition methods, then create a state
by providing initial values and update functions:
toggle .toggle("apple") // items is ["apple"] .toggle("banana") // items is ["apple", "banana"] .toggle("apple"); // items is ["banana"]
Observers
States support the observer pattern through manager metadata accessed via manageState(state).
The manager's attach(observer) and detach(observer) methods create and return
a new state with the observer attached or detached, preserving immutability.
Observers are called asynchronously when transitions occur:
Observers are compared by reference equality (===), which means the same function
reference can only be attached once and must be used to detach.
Snapshots
States support creating and restoring version snapshots for implementing features
like undo/redo, checkpointing, or time-travel debugging through manager metadata
accessed via manageState(state). Snapshots capture the current version data but not observers.
Call the manager's capture() method to create a snapshot:
Type-safe immutable state management.
Use when managing objects that change over time through named transitions - like a counter with increment and decrement operations, a shopping cart adding and removing items, or a toggle managing on/off state with enable and disable transitions.
Concepts
thisand returns partial version data to be merged into the state(state), kept separate from State to avoid polluting user-defined interfacesStates are created by the State factory from a Seed where transition methods are implemented as Update functions that return partial updates. These updates are merged into a new immutable state, and transition methods return the new state. Eliminates manual state spreading and ensures type-safe updates.
Basic Usage
Define a state interface with data properties and transition methods, then create a state by providing initial values and update functions:
Chaining Transitions
Transitions return new immutable states:
Parameterized Transitions
Transitions can accept parameters, which are passed as individual arguments to the update function:
Observers
States support the observer pattern through manager metadata accessed via manageState
(state). The manager'sattach(observer)anddetach(observer)methods create and return a new state with the observer attached or detached, preserving immutability. Observers are called asynchronously when transitions occur:Observers are compared by reference equality (
===), which means the same function reference can only be attached once and must be used to detach.Snapshots
States support creating and restoring version snapshots for implementing features like undo/redo, checkpointing, or time-travel debugging through manager metadata accessed via manageState
(state). Snapshots capture the current version data but not observers.Call the manager's
capture()method to create a snapshot:Restore state from a snapshot by calling the manager's
restore(snapshot)method:Snapshots enable, for instance, undo/redo patterns by storing version history: