@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
29 lines (28 loc) • 886 B
JavaScript
import { from } from "solid-js";
//#region src/adapters/frameworks/solid.ts
const alwaysTrue = () => true;
/**
* Mount a `HydrationSignal` into Solid's reactivity via `from`. Returns an
* `Accessor<boolean>` — read it in a reactive scope (`createEffect`/component/JSX).
* Null/undefined signal → always `true`; the subscription is owned by the
* reactive scope and cleaned up on scope dispose. Renders `true` on the server.
*
* @example
* ```ts
* import { createEffect } from "solid-js";
* const hydrated = useHydrated(prefsHydration);
* createEffect(() => { if (hydrated()) renderPrefs(); });
* ```
*/
function useHydrated(signal) {
if (!signal) return alwaysTrue;
return from((set) => {
const unsubscribe = signal.subscribeHydrated(() => {
set(signal.isHydrated());
});
set(signal.isHydrated());
return unsubscribe;
}, false);
}
//#endregion
export { useHydrated };