@hyperfrontend/state-machine/state-change

state-change

State-transition tracking primitive: pairs the previous and current state of a slice for change-driven side effects.

StateChange represents the diff between two state snapshots so subscribers (UI render hooks, side-effect runners, lifecycle callbacks) can react to specific transitions rather than every dispatch. The class is consumed internally by LifecycleAwareComponent and exposed for application code that wants to build its own transition-driven behavior on top of the store.

API Reference§

◇ Classes

§class

StateChange

Tracks state changes and notifies registered callbacks when state transitions occur.
Constructor
§new StateChange()
Properties
§get previous:DerivedStateThe state before the most recent one, or null until two states have been recorded.
§get current:DerivedStateThe most recently recorded state, or null until the first addItem.
Methods
§readonly addItem(newState: DerivedState): StateChange
Records a state as the current one, demoting the one before it to previous, and notifies every registered callback.
Only the two most recent states are kept; anything older is dropped. Callbacks run synchronously, in registration order, before this returns.

Parameters

NameTypeDescription
§newState
DerivedStateThe derived state that has just come into effect

Returns

StateChange
This tracker, so items and registrations can be chained
§readonly registerCallback(callback: StateChangeHandler): StateChange
Registers a callback for every transition that follows.
The callback is not invoked for the states already recorded; its first call comes with the next addItem. Registering the same callback twice runs it twice.

Parameters

NameTypeDescription
§callback
StateChangeHandlerCalled with the previous and the current snapshot after each addItem

Returns

StateChange
This tracker, so registrations can be chained
Example

Tracking state transitions

const tracker = new StateChange()
tracker.registerCallback((prev, curr) => console.log('Changed:', prev, '->', curr))
tracker.addItem(newState)