@hyperfrontend/state-machine/events

events

Event-driven state primitives: a small pub/sub registry used as a building block by other state-machine pieces.

Events exposes on over typed event names, and derives each event from the state changes its own dispatch produces, so components can subscribe to lifecycle and state-change notifications without coupling to the dispatcher that publishes them. Used internally by LifecycleAwareComponent and Store to surface state transitions; available on its own for application code that wants the same shape without dragging in a full store.

API Reference§

◇ Classes

§class

Events

Event system that tracks state changes and dispatches events to registered handlers.
Constructor
§new Events()
Methods
§readonly dispatch(action: Action): void
Reduces an action into the internal store and dispatches the events the resulting state change implies.
Handlers run synchronously before this call returns, once for every event whose flag went from off to on, in the order the events are declared and then in registration order. An action that leaves the derived state unchanged fires nothing.

Parameters

NameTypeDescription
§action
ActionThe action to reduce, as produced by the action creators
§readonly on(event: Event, eventHandler: EventHandler): void
Registers a handler for one event name.
The handler runs each time the matching derived-state flag turns on, so it fires on every transition into that state rather than once. Registration is additive: a handler registered twice runs twice, and nothing is deduplicated.

Parameters

NameTypeDescription
§event
EventWhich state transition to listen for, one of the event constants
§eventHandler
EventHandlerCalled with the event name, the state after the transition and the state before it
Example

Registering event handlers

const events = new Events()
events.on(event.Done, () => console.log('Operation completed'))
events.dispatch(success())