@atomic-design/di
Version: 
Lightweight, type-safe dependency inversion utilities for Atomic Design architectures in React
109 lines (108 loc) • 2.94 kB
JavaScript
import { jsx as g } from "react/jsx-runtime";
import { createContext as k, use as v, Suspense as y, useContext as h, useMemo as C } from "react";
const H = {};
function I(o, e) {
  const n = $(o, e), s = n.getInitVariants(), t = k(s), { Provider: u } = t, p = () => {
    const r = v(t);
    if (r === s)
      throw new Error("Need to wrap into Provider");
    return r.context;
  };
  n.setDepsHook(() => p());
  const f = ({ children: r, variants: m, space: w, variant: i }) => {
    const { variants: d } = h(t), c = C(
      () => P(d, m, w, i),
      [d, w, i, m]
    ), j = C(async () => {
      try {
        return await n.getVariant(c);
      } catch (b) {
        throw console.error(b), b;
      }
    }, [c]), V = v(j), x = C(
      () => ({ variants: c, context: V }),
      [c, V]
    );
    return /* @__PURE__ */ g(u, { value: x, children: r });
  }, a = (r) => /* @__PURE__ */ g(y, { fallback: null, children: /* @__PURE__ */ g(f, { ...r }) });
  function l(r, m) {
    return (i) => {
      const d = p()[r][m];
      return /* @__PURE__ */ g(d, { ...i || {} });
    };
  }
  return [p, a, l];
}
function P(o, e, n, s) {
  const t = { ...o };
  return e && Object.assign(t, e), n && n && Object.assign(t, { [n]: s }), t;
}
function $(o, e) {
  let n;
  const s = () => n(), t = /* @__PURE__ */ new Map();
  return {
    getVariant: async (a) => {
      const l = M(a);
      let r = t.get(l);
      if (r)
        return r;
      const m = Object.entries(o).map(
        async ([i, d]) => {
          const c = a[i];
          if (c) {
            const V = e[i];
            if (V) {
              const x = V[c];
              if (x) {
                const b = await x();
                return b.setDepsHook(s), [i, b.output];
              }
            }
          }
          const j = await d();
          return j.setDepsHook(s), [i, j.output];
        }
      ), w = await Promise.all(m);
      return r = Object.fromEntries(w), t.set(l, r), r;
    },
    getInitVariants: () => H,
    setDepsHook: (a) => {
      n = a;
    }
  };
}
function M(o) {
  return Object.entries(o).filter(([, e]) => e !== void 0).sort(([e], [n]) => e.localeCompare(n)).map(([e, n]) => `${e}=${String(n)}`).join("&");
}
let D = 1;
function W(o) {
  let e;
  return {
    output: Object.fromEntries(Object.entries(o).map(([s, t]) => {
      const u = (...p) => {
        const [f] = p, a = e(), l = O(f) ? {
          ...f,
          deps: a
        } : { deps: a };
        return /* @__PURE__ */ g(t, { ...l });
      };
      return "displayName" in t ? u.displayName = `${t.displayName}_${D++}` : "name" in t && (u.displayName = `${t.name}_${D++}`), [s, u];
    })),
    setDepsHook: (s) => {
      e = s;
    }
  };
}
const A = (o) => ({
  output: o,
  setDepsHook: () => {
  }
});
function O(o) {
  return typeof o == "object" && o !== null;
}
export {
  A as blankDeps,
  I as createVariantsContext,
  W as extractComponentDeps
};