Palette
    Preparing search index...

    Class State<Shape>

    Powerful and flexible state management with deep reactivity, transaction, and locking support.

    The State class wraps stateful data and provides an API for mutating the data while listeners subscribe to meaningful updates.

    Type Parameters

    • Shape extends object
    Index

    Constructors

    Accessors

    • get live(): Shape

      Live reactive accessor for state data. Deeply nested changes trigger automatic updates, including array, Map, and Set mutations.

      Returns Shape

      For deeply nested values, the .live accessor may add performance overhead compared to directly accessing the value via .current

    • get raw(): Readonly<Shape>

      The current state

      Returns Readonly<Shape>

      This is a direct reference to the internal state. It is marked as Readonly to help prevent accidental changes. Never edit this value directly.

    Methods

    • Add a handler function for when this state changes.

      Listeners are invoked in the order they are registered and are passed a reference to the internally managed state object as a readonly object.

      Do not modify state values directly. Instead, use set or patch to make immutable updates.

      Parameters

      Returns void

    • Get a single value from the state object

      The value is returned as Readonly to prevent accidental state mutations. To mutate stateful properties, use set or patch instead.

      Type Parameters

      • K extends string | number | symbol

      Parameters

      • key: K

      Returns Readonly<Shape[K]>

    • Apply complex updates to the state using a mutator function.

      The mutator function takes one parameter which is a structuredClone copy of the current state object. Whatever is returned by the mutator is then patched in to the state.

      Parameters

      Returns State<Shape>

    • Set multiple stateful properties at once, leaving omitted properties unchanged.

      Parameters

      Returns State<Shape>

    • Remove a previously registered state handler

      Parameters

      Returns void

    • Fully replace the current state data and force an update

      Parameters

      Returns State<Shape>

    • Set a single stateful property while leaving other properties unchanged.

      Triggers an update to all listeners if the value is different from before when compared using shallow equality.

      Type Parameters

      • K extends string | number | symbol

      Parameters

      Returns State<Shape>

      const state = new State({ count: 1, color: "red" });
      state.set("count", 2); // State is now { count: 2, color: "red" }
    • Returns a deep clone of the current state data using structuredClone() to make the copy.

      The object returned from this function can be edited without modifying the actual internal state.

      
      

      Returns Shape

    • Type Parameters

      • T extends object

      Parameters

      • value: unknown

      Returns value is State<T>