import type { StoreReducer } from './store-base'; /** * What a store's `persist` reads and writes: `localStorage`'s two methods. Each * may answer a promise, as `indexedDbStorage()` does: the store then reads the * saved state when it lands, and a rejection warns as a throw does. */ export type PersistStorage = { getItem(key: string): string | null | Promise; setItem(key: string, value: string): void | Promise; }; /** What `saveTo` takes. */ export type SaveToOptions = { /** Storage key. Use a unique prefix per feature: `'vc:cart'`. */ key: string; /** Default: `globalThis.localStorage`, looked up at each read and save. `indexedDbStorage()` for IndexedDB. */ storage?: PersistStorage; /** Default: `JSON.stringify`. */ serialize?: (state: S) => string; /** Default: `JSON.parse`, a parse failure read as nothing saved. Return null to start from `state()`. */ deserialize?: (raw: string) => S | null; /** Accept a read state, or false to start from `state()` with a warning: a check of its shape. */ validate?: (state: S) => boolean; }; /** @internal A store's write: its reducer applied, as a ring step when the store keeps undo. */ export type StoreWrite = (cmd: { target: any; payload?: any; }, f: StoreReducer) => S; /** @internal What the store does with one opened persistence. */ export type PersistHooks = { /** On the store's watcher: schedules the save. */ save: () => void; /** While an async read is pending: a handler that keeps its write for the replay. Null otherwise. */ keep: ((f: StoreReducer) => (cmd: { target: any; payload?: any; }) => S) | null; /** While an async read is pending: keeps a server's state for the replay. Null otherwise. */ held: ((cmd: object, next: S) => void) | null; /** Resolves once an async read has landed and its replay written. Null for a sync read. */ ready: Promise | null; }; /** The value of a store's `persist`, made by `saveTo`. */ export type StorePersist = (init: () => S) => { /** The store's first state: the saved one, or `init()`. */ first: S; /** Wires the store's cell, its lifetime and its write. */ attach(cell: { value: S; }, live: () => boolean, write: StoreWrite): PersistHooks; }; /** * saveTo - a store's saved state, the value of its `persist` option: read when * the store is created, saved once per microtask after any of its writes, * whatever wrote it. An async storage is read when it answers; the writes made * before then are replayed on the saved state by the undo ring's rule (a * reducer runs again, a state set whatever came before is kept), and nothing is * saved before it lands. A missing `key` throws here. * * @example * defineChamberStore('cart', { state, reducers, persist: saveTo({ key: 'vc:cart' }) }); */ export declare function saveTo(options: SaveToOptions): StorePersist; //# sourceMappingURL=store-persist.d.ts.map