[]previous and next page

SetStore

Updates a store.

Updates a store. Every form is one batched, fine-grained transaction:

  • setStore((draft) => { draft.items.push(x) }) mutates a draft; returning a value replaces (reconciles) instead.
  • setStore({ title: "x" }) merges keys into an object; setStore([...]) replaces an array.
  • setStore(reconcile(next)) diffs next into the store.
  • setStore("todos", 2, "done", true) writes at a path of up to four steps, fully type-checked; a step may be a key, several keys, an array filter (item) => item.done, or a range { from, to, by }; the last argument may be a value, an updater, or a partial object that is merged. Deeper or dynamic paths go through a draft function.

Import#

ts
import type { SetStore } from "@shadow-js/core";

Signature#

ts
interface SetStore<T> {
  /**
   * Draft form: `update` mutates a draft of the whole store. Returning a value instead replaces
   * the store with it (an array is reconciled, a plain object is merged).
   */
  (update: (draft: T) => void | T | Partial<T>): void;
  /** Reconcile form: diffs the data given to {@link reconcile} into the store. */
  (update: Reconciler<T>): void;
  /** Value form: an array replaces an array store (diffed in place); an object merges its keys. */
  (value: T extends readonly unknown[] ? T : Partial<T>): void;
  /** Path form with one step: `a` selects the keys or items, `value` is the leaf update. */
  <A extends Step<T>>(a: A, value: Leaf<Next<T, A>>): void;
  /** Path form with two steps. */
  <A extends Step<T>, B extends Step<Next<T, A>>>(
    a: A,
    b: B,
    value: Leaf<Next<Next<T, A>, B>>,
  ): void;
  /** Path form with three steps. */
  <A extends Step<T>, B extends Step<Next<T, A>>, C extends Step<Next<Next<T, A>, B>>>(
    a: A,
    b: B,
    c: C,
    value: Leaf<Next<Next<Next<T, A>, B>, C>>,
  ): void;
  /** Path form with four steps, the deepest typed path; deeper updates use the draft form. */
  <
    A extends Step<T>,
    B extends Step<Next<T, A>>,
    C extends Step<Next<Next<T, A>, B>>,
    D extends Step<Next<Next<Next<T, A>, B>, C>>,
  >(
    a: A,
    b: B,
    c: C,
    d: D,
    value: Leaf<Next<Next<Next<Next<T, A>, B>, C>, D>>,
  ): void;
}

Members#

Name Type Description
T T