# Lifecycle

Source: /tuil/docs/reference/packages/core/api/lifecycle
Locale: en

class exported by @mwillbanks/tuil-core.



{/* Generated by tooling/docs/generate-reference.ts. */}

## class [#class]

Public class exported by `@mwillbanks/tuil-core`.

```ts
export class Lifecycle {
  #state: AppLifecycleState;
  readonly #observers: Set<
    (state: AppLifecycleState, previous: AppLifecycleState) => void
  >;

  constructor() {
    this.#state = "created";
    this.#observers = new Set();
  }

  get state(): AppLifecycleState {
    return this.#state;
  }

  transition(next: AppLifecycleState): void {
    if (!transitions[this.#state].includes(next)) {
      throw new Error(`Invalid lifecycle transition: ${this.#state} → ${next}`);
    }
    const previous = this.#state;
    this.#state = next;
    for (const observer of this.#observers) {
      observer(next, previous);
    }
  }

  observe(
    observer: (state: AppLifecycleState, previous: AppLifecycleState) => void,
  ): () => void {
    this.#observers.add(observer);
    return deleteOnDispose(this.#observers, observer);
  }
}
```

## Members [#members]

| Member          | Type                                                                                        | Required | Description                                                                                                                         | Related types                                                                |
| --------------- | ------------------------------------------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| `#state`        | `AppLifecycleState`                                                                         | Yes      | The `#state` member uses the `AppLifecycleState` contract.                                                                          | [`AppLifecycleState`](/tuil/docs/reference/packages/core/api/app-lifecycle-state) |
| `#observers`    | `Set<(state: AppLifecycleState, previous: AppLifecycleState) => void>`                      | Yes      | The `#observers` member uses the `Set<(state: AppLifecycleState, previous: AppLifecycleState) => void>` contract.                   | [`AppLifecycleState`](/tuil/docs/reference/packages/core/api/app-lifecycle-state) |
| `__constructor` | `any`                                                                                       | Yes      | The `__constructor` member uses the `any` contract.                                                                                 | —                                                                            |
| `state`         | `AppLifecycleState`                                                                         | Yes      | The `state` member uses the `AppLifecycleState` contract.                                                                           | [`AppLifecycleState`](/tuil/docs/reference/packages/core/api/app-lifecycle-state) |
| `transition`    | `(next: AppLifecycleState) => void`                                                         | Yes      | The `transition` member uses the `(next: AppLifecycleState) => void` contract.                                                      | [`AppLifecycleState`](/tuil/docs/reference/packages/core/api/app-lifecycle-state) |
| `observe`       | `(observer: (state: AppLifecycleState, previous: AppLifecycleState) => void) => () => void` | Yes      | The `observe` member uses the `(observer: (state: AppLifecycleState, previous: AppLifecycleState) => void) => () => void` contract. | [`AppLifecycleState`](/tuil/docs/reference/packages/core/api/app-lifecycle-state) |

## Parameters [#parameters]

This declaration has no public members.

## Returns [#returns]

This declaration does not return a value.

## Throws [#throws]

No thrown errors are documented for this declaration.

## Related types [#related-types]

* [`AppLifecycleState`](/tuil/docs/reference/packages/core/api/app-lifecycle-state)

## Source [#source]

[View the secondary source reference](https://github.com/mwillbanks/tuil/blob/main/packages/core/src/lifecycle.ts)

## Package [#package]

[@mwillbanks/tuil-core](/tuil/docs/reference/packages/core)
