tuil
ReferencePackages@mwillbanks/tuil-coreAPI

Lifecycle

class exported by @mwillbanks/tuil-core.

View rawEdit

class

Public class exported by @mwillbanks/tuil-core.

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

MemberTypeRequiredDescriptionRelated types
#stateAppLifecycleStateYesThe #state member uses the AppLifecycleState contract.AppLifecycleState
#observersSet<(state: AppLifecycleState, previous: AppLifecycleState) => void>YesThe #observers member uses the Set<(state: AppLifecycleState, previous: AppLifecycleState) => void> contract.AppLifecycleState
__constructoranyYesThe __constructor member uses the any contract.
stateAppLifecycleStateYesThe state member uses the AppLifecycleState contract.AppLifecycleState
transition(next: AppLifecycleState) => voidYesThe transition member uses the (next: AppLifecycleState) => void contract.AppLifecycleState
observe(observer: (state: AppLifecycleState, previous: AppLifecycleState) => void) => () => voidYesThe observe member uses the (observer: (state: AppLifecycleState, previous: AppLifecycleState) => void) => () => void contract.AppLifecycleState

Parameters

This declaration has no public members.

Returns

This declaration does not return a value.

Throws

No thrown errors are documented for this declaration.

Source

View the secondary source reference

Package

@mwillbanks/tuil-core

On this page