Private
#childPrivate
#isPrivate
#itemsPrivate
#keysPrivate
#subscriberPrivate
#unsubscribersPrivate
#updatesPrivate
#updatesThis 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.
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
Private
#addAdds a reactive item to the store with the given key.
The key to use when adding the item to the store.
The reactive item to add to the store.
If an item with the given key already exists in the store.
Private
#addAdds a child store with the given key to this store.
The key to use when adding the child store to this store.
The child store to add to this store.
If a child store with the given key already exists in this store.
Private
#childPrivate
#destroyDestroys 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.
The key of the child store to destroy.
Private
#destroyDestroys 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.
The key of the item to destroy.
true if the item was destroyed, false otherwise.
Private
#getPrivate
#getRetrieves the reactive item with the given key from the store.
The key of the item to retrieve.
The reactive item with the given key, or null if no such item exists in the store.
Private
#itemsPrivate
#notifyPrivate
#removePrivate
#removeAdds one or more reactive items to the store. If an item is a child store, it will be added to the 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.
If an item with the given key already exists in the store.
If the store is destroyed.
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.
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).
A plain object containing the values of the items that match the filter.
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: {}
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.
The key of the item or child store to destroy.
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.
The key of the item to retrieve.
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.
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).
An array containing the names of items that match the filter.
Retrieves all items stored in the Store, optionally filtered by a specified filter.
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).
A Map containing the items that match the filter.
Subscribes a function to be called when this Store is destroyed. The function is called with no arguments.
The function to be called.
A function that unsubscribes the given function.
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.
The function to be called whenever the value of this Atom changes.
A function that unsubscribes the given function.
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
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