UNPKG

@blac/react

Version:

Demo [https://blac-react-demo.vercel.app/](https://blac-react-demo.vercel.app/)

82 lines (81 loc) 2.33 kB
var v = Object.defineProperty; var x = (s, e, t) => e in s ? v(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t; var i = (s, e, t) => x(s, typeof e != "symbol" ? e + "" : e, t); import { Blac as h } from "blac"; import { useId as E, useMemo as U, useSyncExternalStore as j, useLayoutEffect as w } from "react"; const k = (s, e) => ({ subscribe: (t) => { const n = s.addSubscriber({ fn: () => { try { t(s.state); } catch (a) { console.error({ e: a, bloc: s, dependencyArray: e }); } }, dependencyArray: e }); return () => { n(); }; }, getSnapshot: () => s.state, getServerSnapshot: () => s.state }), c = class c { static useBloc(e, t) { const n = E(), a = c.getUsedKeys(n); let f = t != null && t.inferStateUsage ? (r) => { if (typeof r != "object") return [r]; const d = []; for (const K of a) K in r && d.push(r[K]); return d; } : (r) => [r], y, g; typeof t == "object" && (f = t.dependencySelector ?? f, y = t.id, g = t.props ?? g), e.isolated && (y = n); const u = h.getInstance().getBloc(e, { id: y, props: g, reconnect: !1 }); if (!u) throw new Error(`useBloc: could not resolve: ${e.name || e}`); const { subscribe: b, getSnapshot: m, getServerSnapshot: B } = U( () => k(u, f), [u.createdAt] ), o = j( b, m, B ), l = /* @__PURE__ */ new Set(), I = U(() => { try { if (t != null && t.inferStateUsage && typeof o == "object") return new Proxy(o, { get(r, d) { return a.add(d), l.add(d), Reflect.get(r, d); } }); } catch (r) { h.instance.log("useBloc Error", r); } return o; }, [o]); return w(() => { c.getUsedKeys(n).clear(); for (const r of l) c.getUsedKeys(n).add(r); }, [l, n]), [I, u]; } }; i(c, "ridUsedKeys", /* @__PURE__ */ new Map()), i(c, "getUsedKeys", (e) => (c.ridUsedKeys.has(e) || c.ridUsedKeys.set(e, /* @__PURE__ */ new Set()), c.ridUsedKeys.get(e))); let S = c; const $ = S.useBloc; export { k as externalBlocStore, $ as useBloc }; //# sourceMappingURL=index.es.js.map