DataStoreclass

new DataStore<T>()
Return
DataStore<T>
Store a data object, with helpers to read and update individual props.
PropertyType
.dataT
Get the current data of this store.
- Supports suspense-like reads: throws a Promise while loading or the error reason on failure (inherited from Store.value). required

Store a data object, with helpers to read and update individual props.

  • update(), set() replace the stored object with an immutable updated copy.

A Store for a plain object value. DataStore<T> adds object-aware helpers on top of the base store: read the whole object with .data, or update it without replacing the reference yourself.

OptionalDataStore<T> is the variant whose value may be undefined — it adds .exists, .require(), and .delete() for the absent case.

Usage

DataStore

ts
import { DataStore } from "shelving/store";

const state = new DataStore<{ name: string; count: number }>({ name: "Dave", count: 0 });

state.update({ count: 1 });   // Partial update — merges into the current value.
state.set("count", 2);        // Set a single named prop.
console.log(state.get("name")); // "Dave"
console.log(state.data);        // { name: "Dave", count: 2 }

OptionalDataStore

ts
import { OptionalDataStore, NONE } from "shelving/store";

const profile = new OptionalDataStore<{ name: string }>(NONE);

profile.value = { name: "Dave" };
console.log(profile.exists);        // true
console.log(profile.require().name); // "Dave" — throws RequiredError if absent
profile.delete();                    // value is now undefined

Examples

const store = new DataStore({ name: "Dave", age: 40 });
store.set("age", 41);
store.get("name"); // "Dave"

Methods

Go

DataStore.update()method

Update several props in this data.

update(updates: Updates<T>): void
Go

DataStore.get()method

Get a single named prop from this data.

get(name: K): T[K]
Go

DataStore.set()method

Set a single named prop in this data.

set(name: K, value: T[K]): void