@studnicky/strata-store-kit
Compose state stores into an ordered flow that propagates a source update through every higher store.
Install
pnpm add @studnicky/strata-store-kit @studnicky/storeLayer order
StrataStore<TState> accepts stores from source to target. Consumer writes enter the first store; every update then propagates to the next store before the write resolves. Reads and subscriptions observe the final store.
setState / update
│
▼
MemoryPersistence ───► LocalStorage ───► consumer snapshothydrate() restores the final durable store, then seeds the source store with that value. The source update propagates through the full chain, leaving the cache and durable state aligned for the next update. clear() removes the named value from every layer. Call dispose() when the composition no longer owns its subscriptions.
Try it
The complete example seeds a local-storage counter as if it came from an earlier browser session, creates an in-memory cache plus durable store, hydrates the composition, applies an update, and prints the cache, durable, and consumer snapshots. It clears the demo state and disposes its subscriptions after the run.
Usage
const key = 'demo:layered-counter';
const cache = Store.create({ 'initialState': 0, 'key': key, 'persistence': MemoryPersistence.create<number>() });
const durable = Store.create({
'initialState': 0,
'key': key,
'persistence': BrowserPersistence.create({ 'codec': NUMBER_CODEC, 'storageTarget': StorageTarget.LocalStorage })
});
await durable.setState(7);
const counter = StrataStore.create({ 'layers': [cache, durable] });
counter.subscribe((snapshot): void => {
console.log(`durable subscriber received ${snapshot}`);
});
await counter.hydrate();
await counter.update((snapshot): number => {
const result = snapshot + 1;
return result;
});
console.log({
'cache': cache.getSnapshot(),
'consumer': counter.getSnapshot(),
'durable': durable.getSnapshot()
});
await counter.clear();
counter.dispose();Exports
| Symbol | Purpose | Import path |
|---|---|---|
StrataStore | Ordered store composition that implements StoreInterface<TState>. | @studnicky/strata-store-kit |
StrataStoreOptionsInterface<TState> | Construction options containing source-to-target layers. | @studnicky/strata-store-kit |