@stainless-code/persist
Version:
Hydration-aware persistence middleware for reactive stores (storage × codec seams, TanStack Store adapters, React hydration hook)
38 lines (37 loc) • 1.6 kB
text/typescript
import { a as PersistApi, o as PersistOptions } from "./persist-core-DUCh-1pn.mjs";
import { Atom, Store, StoreActionMap } from "@tanstack/store";
//#region src/persist-tanstack.d.ts
/**
* Persist a `@tanstack/store` `Store` (action-bearing stores included).
*
* @example
* ```ts
* const store = new Store({ filters: [] as string[] });
* const persist = persistStore(store, {
* name: "app:filters:v1",
* version: 1,
* skipPersist: (s) => s.filters.length === 0, // no key when default
* });
* // hydrates automatically on create; subscribe-writes on every setState
* await persist.rehydrate(); // optional manual re-read, awaitable
* ```
*/
declare function persistStore<TState, TPersistedState = TState>(store: Store<TState, StoreActionMap>, options: PersistOptions<TState, TPersistedState>): PersistApi<TState, TPersistedState>;
/**
* Persist a writable `@tanstack/store` `Atom`. Default `merge` REPLACES
* instead of shallow-spreading — atoms commonly hold primitives, which a
* spread would corrupt. Pass `merge` to override.
*
* @throws Error when given a readonly (computed) atom — there is no `set` to
* hydrate into.
*
* @example
* ```ts
* const theme = createAtom<"light" | "dark">("light");
* const persist = persistAtom(theme, { name: "app:theme:v1" });
* // hydrate REPLACES the primitive; theme.set() writes through
* ```
*/
declare function persistAtom<TState, TPersistedState = TState>(atom: Atom<TState>, options: PersistOptions<TState, TPersistedState>): PersistApi<TState, TPersistedState>;
//#endregion
export { persistAtom, persistStore };