UNPKG

@unleash/proxy-client-react

Version:
141 lines (140 loc) 4.6 kB
import { UnleashClient as k } from "unleash-proxy-client"; import { InMemoryStorageProvider as Q, LocalStorageProvider as W, UnleashClient as X } from "unleash-proxy-client"; import g, { useState as b, useEffect as y, useMemo as A, useContext as p, useRef as H } from "react"; const d = g.createContext(null), P = { bootstrap: [], disableRefresh: !0, disableMetrics: !0, url: "http://localhost", appName: "offline", clientKey: "not-used" }, M = "startTransition", _ = g[M] || ((e) => e()), z = ({ config: e, children: t, unleashClient: r, startClient: s = !0, stopClient: u = !0, startTransition: c = _ }) => { var F, h, S; const l = e || P, o = g.useRef( r || new k(l) ), [n, x] = g.useState( !!(r ? e != null && e.bootstrap && (e == null ? void 0 : e.bootstrapOverride) !== !1 || (F = r.isReady) != null && F.call(r) : l.bootstrap && l.bootstrapOverride !== !1) ), [R, v] = b(((S = (h = o.current).getError) == null ? void 0 : S.call(h)) || null); y(() => { !l && !r && console.error( `You must provide either a config or an unleash client to the flag provider. If you are initializing the client in useEffect, you can avoid this warning by checking if the client exists before rendering.` ); const a = (E) => { c(() => { v((U) => U || E); }); }, f = (E) => { c(() => { v(null); }); }; let i = null; const m = () => { i = setTimeout(() => { c(() => { x(!0); }); }, 0); }; return o.current.on("ready", m), o.current.on("error", a), o.current.on("recovered", f), s && (o.current.stop(), o.current.start()), function() { o.current && (o.current.off("error", a), o.current.off("ready", m), o.current.off("recovered", f), u && o.current.stop()), i && clearTimeout(i); }; }, []); const T = A( () => ({ on: (a, f, i) => o.current.on(a, f, i), off: (a, f) => o.current.off(a, f), updateContext: async (a) => await o.current.updateContext(a), isEnabled: (a) => o.current.isEnabled(a), getVariant: (a) => o.current.getVariant(a), client: o.current, flagsReady: n, flagsError: R, setFlagsReady: x, setFlagsError: v }), [n, R] ); return /* @__PURE__ */ g.createElement(d.Provider, { value: T }, t); }, B = (e) => { const { isEnabled: t, client: r } = p(d), [s, u] = b(!!t(e)), c = H(); return c.current = s, y(() => { if (!r) return; const l = () => { const n = t(e); n !== c.current && (c.current = n, u(!!n)); }, o = () => { const n = t(e); c.current = n, u(n); }; return r.on("update", l), r.on("ready", o), () => { r.off("update", l), r.off("ready", o); }; }, [r]), s; }, K = () => { const { client: e } = p(d), [t, r] = b(e.getAllToggles()); return y(() => { const s = () => { r(e.getAllToggles()); }; return e.on("update", s), () => { e.off("update", s); }; }, []), t; }, L = () => { const { flagsReady: e, flagsError: t } = p(d); return { flagsReady: e, flagsError: t }; }, q = (e, t) => { var s, u, c, l; return !(e.name === (t == null ? void 0 : t.name) && e.enabled === (t == null ? void 0 : t.enabled) && e.feature_enabled === (t == null ? void 0 : t.feature_enabled) && ((s = e.payload) == null ? void 0 : s.type) === ((u = t == null ? void 0 : t.payload) == null ? void 0 : u.type) && ((c = e.payload) == null ? void 0 : c.value) === ((l = t == null ? void 0 : t.payload) == null ? void 0 : l.value)); }, Y = (e) => { const { getVariant: t, client: r } = p(d), [s, u] = b(t(e)), c = H({ name: s.name, enabled: s.enabled }); return c.current = s, y(() => { if (!r) return; const l = () => { const n = t(e); q(c.current, n) && (u(n), c.current = n); }, o = () => { const n = t(e); c.current.name = n == null ? void 0 : n.name, c.current.enabled = n == null ? void 0 : n.enabled, u(n); }; return r.on("update", l), r.on("ready", o), () => { r.off("update", l), r.off("ready", o); }; }, [r]), s || {}; }, j = () => { const { updateContext: e } = p(d); return e; }, D = () => { const { client: e } = p(d); return e; }; export { d as FlagContext, z as FlagProvider, Q as InMemoryStorageProvider, W as LocalStorageProvider, X as UnleashClient, z as default, B as useFlag, K as useFlags, L as useFlagsStatus, D as useUnleashClient, j as useUnleashContext, Y as useVariant }; //# sourceMappingURL=unleash-react.js.map