UNPKG

@stainless-code/persist

Version:

Hydration-aware persistence middleware for reactive stores (storage × codec seams, TanStack Store adapters, React hydration hook)

33 lines (32 loc) 1.36 kB
import { t as HydrationSignal } from "./hydration-B12PkrNq.mjs"; //#region src/use-hydrated.d.ts interface UseHydratedResult { /** * Gates UI flash only, never the state read — `useSelector` reads the * pre-hydration (initial) state unchanged until hydration lands. */ hydrated: boolean; } /** * Mount a `HydrationSignal` into the React lifecycle via `useSyncExternalStore`. * Returns ONLY `hydrated` — state reads go through `useSelector`. Null signal → * `hydrated: true` (store stays the same with or without persistence). Server * snapshot is always `true` (no storage server-side, nothing to gate) — the * SSR policy every framework adapter must implement per the * `HydrationSignal` adapter contract; this hook is the reference. * * @example * ```ts * // store module — hydration signal as a persist sidekick * const persist = persistStore(store, { name: "app:prefs:v1" }); * export const prefsHydration = toHydrationSignal(persist); * * // component — gate the hydrate flash, read state via useSelector as usual * const { hydrated } = useHydrated(prefsHydration); * const prefs = useSelector(store, (s) => s.prefs); * if (!hydrated) return <Skeleton />; * ``` */ declare function useHydrated(signal: HydrationSignal | null | undefined): UseHydratedResult; //#endregion export { UseHydratedResult, useHydrated };