@hyperfrontend/state-machine/lifecycle-aware-componentlifecycle-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
Constructor
new LifecycleAwareComponent( )Properties
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( ): voidThe 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): LifecycleAwareComponentIf the component is already running, the callback is invoked at once with
true. A callback already registered is not added a second time.Parameters
| Name | Type | Description |
|---|---|---|
§callback | ActiveChangeCallback | Called with the flag's new value on every change |
Returns
LifecycleAwareComponentonInitializingStatusChange( callback: InitializingChangeCallback): LifecycleAwareComponentIf 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
| Name | Type | Description |
|---|---|---|
§callback | InitializingChangeCallback | Called with the flag's new value on every change |
Returns
LifecycleAwareComponentonReadyStatusChange( callback: ReadyChangeCallback): LifecycleAwareComponentIf the component is already ready, the callback is invoked at once with
true. A callback already registered is not added a second time.Parameters
| Name | Type | Description |
|---|---|---|
§callback | ReadyChangeCallback | Called with the flag's new value on every change |
Returns
LifecycleAwareComponentonStartStatusChange( callback: StartingChangeCallback): LifecycleAwareComponentIf 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
| Name | Type | Description |
|---|---|---|
§callback | StartingChangeCallback | Called with the flag's new value on every change |
Returns
LifecycleAwareComponentonStopStatusChange( callback: StoppingChangeCallback): LifecycleAwareComponentIf 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
| Name | Type | Description |
|---|---|---|
§callback | StoppingChangeCallback | Called with the flag's new value on every change |
Returns
LifecycleAwareComponentprotected setInitializing( initializing: boolean): voidSetting the value the flag already holds changes nothing and notifies nobody, so the callbacks fire only on a real transition.
Parameters
| Name | Type | Description |
|---|---|---|
§initializing | boolean | The 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 = ( active: boolean) => voidtype InitializingChangeCallback = ( initializing: boolean) => voidtype ReadyChangeCallback = ( ready: boolean) => voidtype StartingChangeCallback = ( starting: boolean) => voidtype StoppingChangeCallback = ( stopping: boolean) => void