Class State<T>

State is a utility class that allows you to hold a value and notify listeners when the value changes.

Type Parameters

  • T

Hierarchy

  • State

Constructors

  • Type Parameters

    • T

    Parameters

    • defaultState: T
    • Optional options: {
          queueEvents: undefined | boolean;
      } = {}
      • queueEvents: undefined | boolean

        queue events to be dispatched in a microtask. If the state changes multiple times in the same microtask, only the last change will be dispatched. If the most recent value is the same as the previous value, no event will be dispatched. Set this to false to dispatch events immediately upon state changes.

    Returns State<T>

Accessors

  • get changeListenerCount(): number
  • Returns number

  • get state(): T
  • Returns T

  • set state(newState): void
  • Parameters

    • newState: T

    Returns void

Methods

  • Returns void

  • Parameters

    • callback: ((state, lastValue?) => void)
        • (state, lastValue?): void
        • Parameters

          • state: T
          • Optional lastValue: T

          Returns void

    • callInstantlyWithCurrentState: boolean = false

    Returns StateChangeListenerHandle

  • Create a listener that listens to multiple states and calls the callback when any of the states change.

    For example,

    import {State} from "lifecycle-utils";

    const valueState1 = new State<number>(6);
    const valueState2 = new State<string>("hello");
    const valueState3 = new State<boolean>(true);

    const eventHandle = State.createCombinedChangeListener([valueState1, valueState2, valueState3], (newValues, previousValues) => {
    console.log("new values:", newValues);
    console.log("previous values:", previousValues);
    });

    valueState1.state = 7;
    valueState2.state = "world";
    valueState3.state = false;

    // after a microtask, the listener will be called
    // to make event fire immediately upon change, disable the `queueEvents` option on the constructor
    await new Promise(resolve => setTimeout(resolve, 0));
    // will print:
    // new values: [7, "world", false]
    // previous values: [6, "hello", true]

    eventHandle.dispose();

    Type Parameters

    • const StatesObjects extends readonly State<any>[]

    • const StateTypes = {
          -readonly [Index in string | number | symbol]: TypeOfState<StatesObjects[Index]>
      }

    Parameters

    • states: StatesObjects
    • callback: ((state, previousState) => void)
        • (state, previousState): void
        • Parameters

          • state: StateTypes
          • previousState: StateTypes | {
                -readonly [Index in string | number | symbol]: undefined
            }

          Returns void

    • Optional options: {
          callInstantlyWithCurrentState?: boolean;
          queueEvents?: boolean;
      } = {}
      • Optional callInstantlyWithCurrentState?: boolean
      • Optional queueEvents?: boolean

        queue events to be dispatched in a microtask. If the state changes multiple times in the same microtask, only the last change will be dispatched. If the most recent value is the same as the previous value, no event will be dispatched. Set this to false to dispatch events immediately upon state changes.

    Returns StateChangeListenerHandle