@stainless-code/persist
Version:
Hydration-aware persistence for any reactive store — zero-dep persistSource core; codecs, backends, cross-tab transport, source + framework hydration adapters ship as opt-in recipes
27 lines (26 loc) • 836 B
JavaScript
import { readable } from "svelte/store";
//#region src/adapters/frameworks/svelte-store.ts
/**
* Mount a `HydrationSignal` into a Svelte `readable` store. Auto-subscribe
* with `$hydratedStore`. Null/undefined signal → a store that stays `true`;
* the subscription is tied to the store's subscriber lifecycle (start on first
* subscriber, unsubscribe on the last). Yields `true` on the server.
*
* @example
* ```ts
* const hydrated = hydratedStore(prefsHydration);
* // {#if !$hydrated}<Skeleton />{:else}<Prefs />{/if}
* ```
*/
function hydratedStore(signal) {
if (!signal) return readable(true);
return readable(signal.isHydrated(), (set) => {
const unsubscribe = signal.subscribeHydrated(() => {
set(signal.isHydrated());
});
set(signal.isHydrated());
return unsubscribe;
});
}
//#endregion
export { hydratedStore };