# lifecycle-aware-component A component base that exposes [`initializing`](https://www.hyperfrontend.dev/docs/libraries/state-machine/lifecycle-aware-component/#api-LifecycleAwareComponent), [`ready`](https://www.hyperfrontend.dev/docs/libraries/state-machine/lifecycle-aware-component/#api-LifecycleAwareComponent), [`starting`](https://www.hyperfrontend.dev/docs/libraries/state-machine/lifecycle-aware-component/#api-LifecycleAwareComponent), [`stopping`](https://www.hyperfrontend.dev/docs/libraries/state-machine/lifecycle-aware-component/#api-LifecycleAwareComponent), and [`active`](https://www.hyperfrontend.dev/docs/libraries/state-machine/lifecycle-aware-component/#api-LifecycleAwareComponent) state phases plus per-phase change callbacks. [`LifecycleAwareComponent`](https://www.hyperfrontend.dev/docs/libraries/state-machine/lifecycle-aware-component/#api-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`](https://www.hyperfrontend.dev/docs/libraries/state-machine/lifecycle-aware-component/#api-LifecycleAwareComponent) or a service that needs to drain in-flight work on [`stopping`](https://www.hyperfrontend.dev/docs/libraries/state-machine/lifecycle-aware-component/#api-LifecycleAwareComponent) before becoming inactive. Each phase exposes a typed change callback ([`InitializingChangeCallback`](https://www.hyperfrontend.dev/docs/libraries/state-machine/lifecycle-aware-component/#api-InitializingChangeCallback), [`ReadyChangeCallback`](https://www.hyperfrontend.dev/docs/libraries/state-machine/lifecycle-aware-component/#api-ReadyChangeCallback), [`StartingChangeCallback`](https://www.hyperfrontend.dev/docs/libraries/state-machine/lifecycle-aware-component/#api-StartingChangeCallback), [`StoppingChangeCallback`](https://www.hyperfrontend.dev/docs/libraries/state-machine/lifecycle-aware-component/#api-StoppingChangeCallback), [`ActiveChangeCallback`](https://www.hyperfrontend.dev/docs/libraries/state-machine/lifecycle-aware-component/#api-ActiveChangeCallback)) so subclasses or wrappers can react when the component crosses a boundary. The component composes the lower-level [`Events`](https://www.hyperfrontend.dev/docs/libraries/state-machine/events/#api-Events) registry under the hood; reach for it when you need a structured lifecycle rather than raw pub/sub. ## API Reference ### Classes #### LifecycleAwareComponent Abstract base class for components that need lifecycle state management. Tracks initializing, ready, starting, stopping, and active states. ##### Constructor ```typescript new LifecycleAwareComponent() ``` **Properties** | Name | Type | Description | | --- | --- | --- | | `get initializing` | `boolean` | Whether the component is still being initialized. | | `get ready` | `boolean` | Whether the component has finished initializing and can be started. | | `get starting` | `boolean` | Whether the component is in the middle of starting. | | `get stopping` | `boolean` | Whether the component is in the middle of stopping. | | `get active` | `boolean` | Whether the component has started and is running. | | `protected abstract init` | `Process` | 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` | 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` | 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. | ##### clear ```typescript 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 ```typescript 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** | Name | Type | Description | | --- | --- | --- | | `callback` | `ActiveChangeCallback` | Called with the flag's new value on every change | **Returns** `LifecycleAwareComponent`: This component, so registrations can be chained ##### onInitializingStatusChange ```typescript 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** | Name | Type | Description | | --- | --- | --- | | `callback` | `InitializingChangeCallback` | Called with the flag's new value on every change | **Returns** `LifecycleAwareComponent`: This component, so registrations can be chained ##### onReadyStatusChange ```typescript 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** | Name | Type | Description | | --- | --- | --- | | `callback` | `ReadyChangeCallback` | Called with the flag's new value on every change | **Returns** `LifecycleAwareComponent`: This component, so registrations can be chained ##### onStartStatusChange ```typescript 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** | Name | Type | Description | | --- | --- | --- | | `callback` | `StartingChangeCallback` | Called with the flag's new value on every change | **Returns** `LifecycleAwareComponent`: This component, so registrations can be chained ##### onStopStatusChange ```typescript 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** | Name | Type | Description | | --- | --- | --- | | `callback` | `StoppingChangeCallback` | Called with the flag's new value on every change | **Returns** `LifecycleAwareComponent`: This component, so registrations can be chained ##### setActive ```typescript 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** | Name | Type | Description | | --- | --- | --- | | `active` | `boolean` | The new value of the flag | ##### setInitializing ```typescript 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** | Name | Type | Description | | --- | --- | --- | | `initializing` | `boolean` | The new value of the flag | ##### setReady ```typescript 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** | Name | Type | Description | | --- | --- | --- | | `ready` | `boolean` | The new value of the flag | ##### setStarting ```typescript 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** | Name | Type | Description | | --- | --- | --- | | `starting` | `boolean` | The new value of the flag | ##### setStopping ```typescript 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** | Name | Type | Description | | --- | --- | --- | | `stopping` | `boolean` | The new value of the flag | **Example: Extending LifecycleAwareComponent** ```typescript class MyService extends LifecycleAwareComponent { async doInit() { this.setInitializing(true) } async doStart() { this.setActive(true) } } ``` ### Types #### ActiveChangeCallback Callback invoked when active state changes ```typescript type ActiveChangeCallback = (active: boolean) => void ``` #### InitializingChangeCallback Callback invoked when initializing state changes ```typescript type InitializingChangeCallback = (initializing: boolean) => void ``` #### ReadyChangeCallback Callback invoked when ready state changes ```typescript type ReadyChangeCallback = (ready: boolean) => void ``` #### StartingChangeCallback Callback invoked when starting state changes ```typescript type StartingChangeCallback = (starting: boolean) => void ``` #### StoppingChangeCallback Callback invoked when stopping state changes ```typescript type StoppingChangeCallback = (stopping: boolean) => void ``` --- Canonical page: https://www.hyperfrontend.dev/docs/libraries/state-machine/lifecycle-aware-component/ This file: https://www.hyperfrontend.dev/docs/libraries/state-machine/lifecycle-aware-component.md Documentation index: https://www.hyperfrontend.dev/llms.txt