@hyperfrontend/state-machine/lifecycle-aware-component

lifecycle-aware-component

A component base that exposes initializing, ready, starting, stopping, and active state phases plus per-phase change callbacks.

LifecycleAwareComponent models a stateful component whose visibility to consumers depends on which phase it has reached: for example, a UI element that should only render after ready or a service that needs to drain in-flight work on stopping before becoming inactive. Each phase exposes a typed change callback (InitializingChangeCallback, ReadyChangeCallback, StartingChangeCallback, StoppingChangeCallback, ActiveChangeCallback) so subclasses or wrappers can react when the component crosses a boundary. The component composes the lower-level Events registry under the hood; reach for it when you need a structured lifecycle rather than raw pub/sub.

API Reference§

◇ Classes

§class

LifecycleAwareComponent

Abstract base class for components that need lifecycle state management. Tracks initializing, ready, starting, stopping, and active states.
Constructor
§new LifecycleAwareComponent()
Properties
§get initializing:booleanWhether the component is still being initialized.
§get ready:booleanWhether the component has finished initializing and can be started.
§get starting:booleanWhether the component is in the middle of starting.
§get stopping:booleanWhether the component is in the middle of stopping.
§get active:booleanWhether the component has started and is running.
§protected abstract init:Process<any>Brings the component to the ready phase.
An implementation raises initializing while it works and ready when it finishes, and resolves to the status the run ended with: success, fail or skipped. The base class never calls it, so the subclass decides when initialization happens.
§abstract start:Process<any>Starts the component once it is ready.
An implementation raises starting while it works and active once running, and resolves to the status the run ended with: success, fail or skipped. The base class only tracks the flags.
§abstract stop:Process<any>Stops a running component.
An implementation raises stopping while it works and lowers active once stopped, and resolves to the status the run ended with: success, fail or skipped. The base class only tracks the flags.
Methods
§clear(): void
Removes every registered status callback across all five flags.
The flags themselves keep their values; only the subscriptions go, so a component can be reused without carrying listeners from its previous owner.
§onActiveStatusChange(callback: ActiveChangeCallback): LifecycleAwareComponent
Registers a callback for changes to the active flag.
If the component is already running, the callback is invoked at once with true. A callback already registered is not added a second time.

Parameters

NameTypeDescription
§callback
ActiveChangeCallbackCalled with the flag's new value on every change

Returns

LifecycleAwareComponent
This component, so registrations can be chained
§onInitializingStatusChange(callback: InitializingChangeCallback): LifecycleAwareComponent
Registers a callback for changes to the initializing flag.
If the component is initializing at the time, the callback is invoked at once with true, so a late subscriber learns the current phase instead of waiting for the next change. A callback already registered is not added a second time.

Parameters

NameTypeDescription
§callback
InitializingChangeCallbackCalled with the flag's new value on every change

Returns

LifecycleAwareComponent
This component, so registrations can be chained
§onReadyStatusChange(callback: ReadyChangeCallback): LifecycleAwareComponent
Registers a callback for changes to the ready flag.
If the component is already ready, the callback is invoked at once with true. A callback already registered is not added a second time.

Parameters

NameTypeDescription
§callback
ReadyChangeCallbackCalled with the flag's new value on every change

Returns

LifecycleAwareComponent
This component, so registrations can be chained
§onStartStatusChange(callback: StartingChangeCallback): LifecycleAwareComponent
Registers a callback for changes to the starting flag.
If a start is under way at the time, the callback is invoked at once with true. A callback already registered is not added a second time.

Parameters

NameTypeDescription
§callback
StartingChangeCallbackCalled with the flag's new value on every change

Returns

LifecycleAwareComponent
This component, so registrations can be chained
§onStopStatusChange(callback: StoppingChangeCallback): LifecycleAwareComponent
Registers a callback for changes to the stopping flag.
If a stop is under way at the time, the callback is invoked at once with true. A callback already registered is not added a second time.

Parameters

NameTypeDescription
§callback
StoppingChangeCallbackCalled with the flag's new value on every change

Returns

LifecycleAwareComponent
This component, so registrations can be chained
§protected setActive(active: boolean): void
Records whether the component is running and notifies the active callbacks.
Setting the value the flag already holds changes nothing and notifies nobody.

Parameters

NameTypeDescription
§active
booleanThe new value of the flag
§protected setInitializing(initializing: boolean): void
Records whether initialization is under way and notifies the initializing callbacks.
Setting the value the flag already holds changes nothing and notifies nobody, so the callbacks fire only on a real transition.

Parameters

NameTypeDescription
§initializing
booleanThe new value of the flag
§protected setReady(ready: boolean): void
Records whether the component is ready to start and notifies the ready callbacks.
Setting the value the flag already holds changes nothing and notifies nobody.

Parameters

NameTypeDescription
§ready
booleanThe new value of the flag
§protected setStarting(starting: boolean): void
Records whether a start is under way and notifies the starting callbacks.
Setting the value the flag already holds changes nothing and notifies nobody.

Parameters

NameTypeDescription
§starting
booleanThe new value of the flag
§protected setStopping(stopping: boolean): void
Records whether a stop is under way and notifies the stopping callbacks.
Setting the value the flag already holds changes nothing and notifies nobody.

Parameters

NameTypeDescription
§stopping
booleanThe new value of the flag
Example

Extending LifecycleAwareComponent

class MyService extends LifecycleAwareComponent {
  async doInit() { this.setInitializing(true) }
  async doStart() { this.setActive(true) }
}

◆ Types

§type

ActiveChangeCallback

Callback invoked when active state changes
type ActiveChangeCallback = (active: boolean) => void
§type

InitializingChangeCallback

Callback invoked when initializing state changes
type InitializingChangeCallback = (initializing: boolean) => void
§type

ReadyChangeCallback

Callback invoked when ready state changes
type ReadyChangeCallback = (ready: boolean) => void
§type

StartingChangeCallback

Callback invoked when starting state changes
type StartingChangeCallback = (starting: boolean) => void
§type

StoppingChangeCallback

Callback invoked when stopping state changes
type StoppingChangeCallback = (stopping: boolean) => void