Store is a reactive container that holds a collection of reactive items. You can add, remove and access items via methods of this class. It also emits events when items are added, removed or updated.

Example

const store = new Store();
const a = atom(0);
const b = atom(0);

const childStore = new Store();
const sum = computed(() => a.value + b.value);
childStore.addItems({ sum });

store.addItems({ a, b, childStore });

store.subscribe((updates) => {
let updatesArr = Array.from(updates.keys());
console.log("props", updatesArr);
});

// mute updates
store.muteUpdates();
childStore.removeItem("childStore");
a.value = 3;
b.value = 4;
store.unmuteUpdates();
// outputs
// props [ 'a', 'childStore.sum', 'b' ]

// without mute updates

a.value = 1;
// outputs
// props [ 'a' ]
// props [ 'childStore.sum' ]

b.value = 2;
// outputs
//props [ 'b' ]
//props [ 'childStore.sum' ]

// using batch
batch(() => {
a.value = 2;
b.value = 3;
});
// outputs
// props [ 'a' ]
// props [ 'childStore.sum' ]

Constructors

Properties

#childStores: Map<string, Store> = ...
#isDestroyed: boolean = false
#items: Map<string, ReactivePrimitive> = ...
#keys: WeakMap<WeakKey, any> = ...
#subscriber: ((updates, store) => void)

Type declaration

#unsubscribers: Dictionary<(() => void)> = ...

Type declaration

    • (): void
    • Returns void

#updates: Map<string, UpdateDataRecord>
#updatesManager: UpdateDataRecordManager
eventEmitter: EventEmitterExt<"change" | "destroy" | "clear-updates">

Accessors

  • get isDestroyed(): boolean
  • This property is set to true when the store is destroyed, and false otherwise. It is used to prevent further modifications to the store after it has been destroyed.

    Returns boolean

    Example

    const store = new Store();

    const store2 = new Store();
    const a = atom(1);

    store2.addItems({ a });
    store.addItems({ store2 });

    console.log(store.hasItem("store2")); // output: true

    store2.destroy();
    console.log(store2.hasItem("a")); // output: false
    console.log(store.hasItem("store2")); // output: false
    console.log(a.isDestroyed); // output: true

Methods

  • Adds a reactive item to the store with the given key.

    Parameters

    • key: string

      The key to use when adding the item to the store.

    • reactiveItem: ReactivePrimitive

      The reactive item to add to the store.

    Returns void

    Throws

    If an item with the given key already exists in the store.

  • Adds a child store with the given key to this store.

    Parameters

    • storeName: string

      The key to use when adding the child store to this store.

    • store: Store

      The child store to add to this store.

    Returns void

    Throws

    If a child store with the given key already exists in this store.

  • Destroys the child store with the given key. This method is useful for cleaning up after a child store that is no longer needed. It calls destroy on the child store and removes the child store from this store.

    Parameters

    • key: string

      The key of the child store to destroy.

    Returns void

  • Destroys the reactive item with the given key. This method is useful for cleaning up after a reactive item that is no longer needed. It calls destroy on the reactive item and removes the item from the store.

    Parameters

    • key: string

      The key of the item to destroy.

    Returns boolean

    true if the item was destroyed, false otherwise.

  • Retrieves the child store with the given key from the store.

    Parameters

    • key: string

      The key of the child store to retrieve.

    Returns Store

    The child store with the given key, or null if no such child store exists in the store.

  • Retrieves the reactive item with the given key from the store.

    Parameters

    • key: string

      The key of the item to retrieve.

    Returns ReactivePrimitive

    The reactive item with the given key, or null if no such item exists in the store.

  • Removes the child store with the given key from this store.

    Parameters

    • key: string

      The key of the child store to remove.

    Returns void

  • Removes the reactive item with the given key from the store. This method does not call destroy on the item.

    Parameters

    • key: string

      The key of the item to remove.

    Returns void

  • Adds one or more reactive items to the store. If an item is a child store, it will be added to the store.

    Parameters

    • items: {
          [key: string]: ReactivePrimitive | Store;
      }

      An object where the keys are the keys to use when adding the items to the store and the values are the reactive items to add.

    Returns void

    Throws

    If an item with the given key already exists in the store.

    Throws

    If the store is destroyed.

    Example

    const store = new Store();
    const a = atom(1);
    store.addItems({ a });
    console.log(store.hasItem("a")); // output: true
  • Retrieves the value of this Store as a plain object, optionally filtered by a specified filter.

    Parameters

    • Optional filter: "all" | "reactives" | "stores" = "all"

      The filter to apply when retrieving items. Default is "all". Possible values can be "all", "reactives", or "stores" (if applicable).

    Returns any

    A plain object containing the values of the items that match the filter.

    Example

    const store = new Store();
    const a = atom(1);
    const b = atom(2);
    const c = new Store();
    const d = computed(() => a.value + b.value);
    const e = collection([1, 2, 3]);

    store.addItems({ a, b, c });
    c.addItems({ d, e });

    console.log(store.asPlainObject());
    // output: { a: 1, b: 2, c: { d: 3, e: [1, 2, 3] } }

    console.log(store.asPlainObject("all"));
    // output: { a: 1, b: 2, c: { d: 3, e: [1, 2, 3] } }

    console.log(store.asPlainObject("reactives"));
    // output: { a: 1, b: 2 }

    console.log(store.asPlainObject("stores"));
    // output: { c: { d: 3, e: [1, 2, 3] } }

    store.destroy();

    console.log(store.asPlainObject());
    // output: {}
  • Clears all reactive items from the store. This method is useful for resetting a Store to an empty state. It removes all reactive items from the store and clears all child stores. It does not destroy the reactive items.

    Returns void

  • Destroys all reactive items stored in the Store. This method is useful for cleaning up after a Store that is no longer needed. It calls destroy on each reactive item in the store and clears the store of all items.

    Returns void

  • Destroys the item with the given key, whether it's a reactive item or a child store. It first attempts to destroy a reactive item with the specified key, and if not found, attempts to destroy a child store with the same key.

    Parameters

    • key: string

      The key of the item or child store to destroy.

    Returns void

  • Retrieves the item with the given key from the store. This method first looks for a reactive item with the given key, and if no such item exists, looks for a child store with the same key.

    Parameters

    • key: string

      The key of the item to retrieve.

    Returns ReactivePrimitive | Store

    The item with the given key, or null if no such item exists in the store.

  • Retrieves the names of items stored in the Store, optionally filtered by a specified filter.

    Parameters

    • Optional filter: "all" | "reactives" | "stores" = "all"

      The filter to apply when retrieving item names. Default is "all". Possible values can be "all", "reactives", or "stores" (if applicable).

    Returns string[]

    An array containing the names of items that match the filter.

  • Retrieves all items stored in the Store, optionally filtered by a specified filter.

    Parameters

    • Optional filter: "all" | "reactives" | "stores" = "all"

      The filter to apply when retrieving items. Default is "all". Possible values can be "all", "reactives", or "stores" (if applicable).

    Returns Map<string, ReactivePrimitive | Store>

    A Map containing the items that match the filter.

  • Checks if an item with the given key exists in the store.

    Parameters

    • key: string

      The key of the item to check.

    Returns boolean

    true if the item exists, false otherwise.

  • Returns whether the event emitter is currently muted.

    Returns boolean

  • Mutes the event emitter, preventing any updates from being triggered. Any updates that are scheduled while muted will be queued and executed when unmuteUpdates is called.

    Returns void

  • Subscribes a function to be called when this Store is destroyed. The function is called with no arguments.

    Parameters

    • fn: ((store) => void)

      The function to be called.

        • (store): void
        • Parameters

          Returns void

    Returns (() => void)

    A function that unsubscribes the given function.

      • (): void
      • Returns void

  • Removes the reactive item with the given key from the store. This method does not call destroy on the item.

    Parameters

    • key: string

      The key of the item to remove.

    Returns void

  • Subscribes a function to be called whenever the value of this Store changes. The function is called with a Map of updates, where the keys are the names of the items that changed, and the values are UpdateDataRecord objects.

    Parameters

    • fn: ((update, store) => void)

      The function to be called whenever the value of this Atom changes.

    Returns (() => void)

    A function that unsubscribes the given function.

      • (): void
      • Returns void

    Example

    const store = new Store();
    const a = atom(1);
    const b = atom(2);

    const c = new Store();
    const d = new Computed(() => a.value + b.value);
    c.addItems({ d });

    store.addItems({ a, b, c });

    let i = 0;

    store.subscribe((updates) => {
    let updatesArr = Array.from(updates.keys());
    console.log(updatesArr);
    i += 1;
    });

    a.value = 2;
    // output: ["a", "c.d"]

    b.value = 3;
    // output: ["b", "c.d"]

    console.log(i); // output: 4
  • Unmutes the event emitter, allowing updates to be triggered. Any updates that were scheduled while muted will be executed.

    Returns void