# CoordinatedLifecycle

`class` in `services` · v0.8.2

<https://docs.seedcord.org/packages/services/0.8.2/classes/coordinated-lifecycle>

Abstract base class for coordinated lifecycle management (startup/shutdown)

```ts
abstract class CoordinatedLifecycle<
    TPhase extends number,
    TEvents extends SEEventMapLike<TEvents>
> extends StrictEventEmitter<TEvents>
```

## Constructors

### constructor

```ts
protected CoordinatedLifecycle(
    loggerName: string,
    phaseOrder: TPhase[],
    phaseEnum: Record<number, string>
)
```

Constructs a new instance of the `CoordinatedLifecycle` class

## Properties

### logger

```ts
protected readonly logger: Logger
```

### phaseEnum

```ts
protected readonly phaseEnum: Record<number, string>
```

### phaseOrder

```ts
protected readonly phaseOrder: TPhase[]
```

### tasksMap

```ts
protected readonly tasksMap: Map<TPhase, LifecycleTask[]>
```

## Methods

### addListener

```ts
addListener<TEventKey>(
    event: TEventKey,
    listener: (...args: TEvents[TEventKey]) => void
): this
```

Alias of [`on`](/packages/services/0.8.2/classes/strict-event-emitter#on) for compatibility with Node.js EventEmitter APIs.

### addTask

```ts
public addTask(
    phase: TPhase,
    taskName: string,
    task: () => Promise<void>,
    timeoutMs: number
): void
```

Adds a lifecycle task to a specific phase.

Tasks are executed in phase order during lifecycle operations. Each task has a timeout to prevent hanging operations.

### canAddTask

```ts
protected abstract canAddTask(): boolean
```

### canRemoveTask

```ts
protected abstract canRemoveTask(): boolean
```

### emit

```ts
emit<TEventKey>(
    event: TEventKey,
    args: TEvents[TEventKey]
): boolean
```

Emits an event with the strictly typed argument tuple for the event name.

### eventNamesTyped

```ts
eventNamesTyped(): SEEventKey<TEvents>[]
```

Returns the list of event names known to the emitter with the mapped key type.

### executeTasksInPhase

```ts
protected abstract async executeTasksInPhase(
    phase: TPhase,
    tasks: LifecycleTask[]
): Promise<PromiseSettledResult<void>[]>
```

### getTaskType

```ts
protected abstract getTaskType(): string
```

### listenerCountTyped

```ts
listenerCountTyped<TEventKey>(event: TEventKey): number
```

Counts listeners for an event without widening the return type of listenerCount.

### listeners

```ts
listeners<TEventKey>(
    event: TEventKey
): ((...args: TEvents[TEventKey]) => void)[]
```

Retrieves the listener list for a given event with the correct tuple signature.

### off

```ts
off<TEventKey>(
    event: TEventKey,
    listener: (...args: TEvents[TEventKey]) => void
): this
```

Removes a previously registered listener for the given event.

### on

```ts
on<TEventKey>(
    event: TEventKey,
    listener: (...args: TEvents[TEventKey]) => void
): this
```

Registers a persistent listener with tuple-safe arguments for the given event.

### once

```ts
once<TEventKey>(
    event: TEventKey,
    listener: (...args: TEvents[TEventKey]) => void
): this
```

Registers a one time listener that is removed after the first invocation.

### removeListener

```ts
removeListener<TEventKey>(
    event: TEventKey,
    listener: (...args: TEvents[TEventKey]) => void
): this
```

Alias of [`off`](/packages/services/0.8.2/classes/strict-event-emitter#off) for compatibility with Node.js EventEmitter APIs.

### removeTask

```ts
public removeTask(
    phase: TPhase,
    taskName: string
): boolean
```

Removes a lifecycle task from a specific phase.

### runPhase

```ts
protected async runPhase(phase: TPhase): Promise<void>
```

Run all tasks in a specific phase

### runTaskWithTimeout

```ts
protected async runTaskWithTimeout(
    phase: TPhase,
    task: LifecycleTask
): Promise<void>
```

Run a single task with timeout

### waitFor

```ts
async waitFor<TEventKey>(
    event: TEventKey,
    opts?: { signal?: AbortSignal; timeoutMs?: number }
): Promise<TEvents[TEventKey]>
```

Waits for an event to be emitted, resolving with the listener arguments tuple once triggered. Supports optional abort signals and timeouts for cancellation semantics.
