@hyperfrontend/state-machine/state-changestate-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
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.Methods
§
readonly addItem( newState: DerivedState): StateChangeRecords a state as the current one, demoting the one before it to
Only the two most recent states are kept; anything older is dropped. Callbacks run synchronously, in registration order, before this returns.
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
| Name | Type | Description |
|---|---|---|
§newState | DerivedState | The derived state that has just come into effect |
Returns
StateChangeThis tracker, so items and registrations can be chained
§
readonly registerCallback( callback: StateChangeHandler): StateChangeRegisters a callback for every transition that follows.
The callback is not invoked for the states already recorded; its first call comes with the next
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
| Name | Type | Description |
|---|---|---|
§callback | StateChangeHandler | Called with the previous and the current snapshot after each addItem |
Returns
StateChangeThis 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)