Skip to content

@studnicky/store

Keep application state observable while selecting an in-memory or browser-native persistence target.

Install

bash
pnpm add @studnicky/store

Core store

Store<TState> owns one named state value. setState and update persist before notifying subscribers; hydrate restores the named value; and clear removes it before publishing the initial state.

ts
const persistence = MemoryPersistence.create<number>();
const store = Store.create({ 'initialState': 0, 'key': 'demo:memory-counter', 'persistence': persistence });

store.subscribe((snapshot): void => {
  console.log(`subscriber received ${snapshot}`);
});

await store.update((snapshot): number => {
  const result = snapshot + 1;
  return result;
});

const hydrated = Store.create({ 'initialState': 0, 'key': 'demo:memory-counter', 'persistence': persistence });

await hydrated.hydrate();

console.log({
  'hydrated': hydrated.getSnapshot(),
  'snapshot': store.getSnapshot()
});

Try it

Memory state

This example updates a state value, observes the notification, then creates a second Store with the same persistence to hydrate the value.

Loading example…

Browser persistence targets

BrowserPersistence has the same StatePersistenceInterface<TState> contract as MemoryPersistence. Select Memory, LocalStorage, SessionStorage, or IndexedDb; the browser adapter uses the corresponding native API directly.

Loading example…

The runnable sample writes, hydrates, reports, and clears one counter for every target, so it leaves the browser storage area clean.

Composition seams

SurfaceConsumer use
StoreInterface<TState>Depend on a state container without coupling to a concrete implementation.
StatePersistenceInterface<TState>Supply a persistence adapter for another environment or backing service.
StateCodecInterface<TState>Validate and serialize persisted values at the storage boundary.
MemoryPersistence<TState>Keep transient state in process memory.
BrowserPersistence<TState>Persist state through a browser-native target.
StorageTargetSelect Memory, LocalStorage, SessionStorage, or IndexedDb.

Exports

SymbolPurposeImport path
StoreObservable state container with serialized writes.@studnicky/store
MemoryPersistenceIn-memory persistence adapter.@studnicky/store
JsonStateCodecJSON serialization and caller-provided decoded-value validation.@studnicky/store
StoreInterfaceStore contract for consumer dependencies and composition.@studnicky/store
StoreListenerInterfaceSubscriber callback contract for store state updates.@studnicky/store
StatePersistenceInterfacePersistence port implemented by storage adapters.@studnicky/store
StateCodecInterfaceCodec contract for persisted values.@studnicky/store
BrowserPersistenceBrowser-native persistence adapter.@studnicky/store/browser
StorageTargetBrowser persistence target selector.@studnicky/store/browser

Layered state

Use @studnicky/strata-store-kit to connect a fast in-memory store to a durable browser store while retaining the StoreInterface<TState> API.

Source on GitHub