UNPKG

@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

48 lines (47 loc) • 1.21 kB
//#region src/adapters/frameworks/lit.ts /** * Lit `ReactiveController` over `HydrationSignal` — gate with `hydrated`. * Construct on the host (`addController`); subscribe on connect, tear down on * disconnect. Null signal → `hydrated` stays `true`. * * @example * ```ts * import { LitElement, html } from "lit"; * import { HydrationController } from "@stainless-code/persist/frameworks/lit"; * * class PrefsEl extends LitElement { * #hydration = new HydrationController(this, prefsHydration); * render() { * return this.#hydration.hydrated * ? html`<prefs-panel></prefs-panel>` * : html`<skeleton-el></skeleton-el>`; * } * } * ``` */ var HydrationController = class { #host; #signal; #unsubscribe; constructor(host, signal) { this.#host = host; this.#signal = signal; host.addController(this); } get hydrated() { return this.#signal?.isHydrated() ?? true; } hostConnected() { if (!this.#signal) return; this.#unsubscribe = this.#signal.subscribeHydrated(() => { this.#host.requestUpdate(); }); this.#host.requestUpdate(); } hostDisconnected() { this.#unsubscribe?.(); this.#unsubscribe = void 0; } }; //#endregion export { HydrationController };