@sandstack/neuron
Version:
Neuron is a lightweight framework agnostic global state manager for Javascript apps, with React support
154 lines (153 loc) • 4.33 kB
JavaScript
var D = Object.defineProperty;
var E = (s, t, e) => t in s ? D(s, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : s[t] = e;
var m = (s, t, e) => (E(s, typeof t != "symbol" ? t + "" : t, e), e);
import { Neuron as I, NeuronClient as j } from "../core";
import { useState as C, useEffect as x, createContext as _, useContext as $ } from "react";
import { jsx as q } from "react/jsx-runtime";
function z(s, t) {
const [e, r] = C(
t ? t(s.getRef()) : s.getRef()
), n = (a) => s.set(a), c = (a) => {
var u;
if (t) {
const d = (u = t == null ? void 0 : t.toString().match(/\(\w+\) =>\s*(\S+)/)) == null ? void 0 : u[1], g = typeof d == "string" ? d == null ? void 0 : d.split(".").slice(1) : [], S = s.getRef(), R = g.reduce(
(i, f) => i[f],
S
);
let v;
typeof a == "function" ? v = a == null ? void 0 : a(R) : v = a;
const O = ((i, f, h) => {
const p = f.pop(), A = f.reduce((b, l) => b[l] = b[l] || {}, i);
return A[p] = h, i;
})(
S,
g,
v
);
s.set({ ...O });
}
}, o = t ? {
...s.getActions(),
set: n,
setSlice: c
} : { ...s.getActions(), set: n };
return x(() => {
s.effect((a) => {
r(t ? t(a.state) : a.state);
});
}, []), [e, o];
}
const P = (s, t, e) => {
const r = (e == null ? void 0 : e(s, t)) ?? new I(s, t);
return (n) => z(r, n);
}, B = (s, t) => {
const [e, r] = C(t.getRef(s)), n = (c) => {
t.dispatch(s, (o) => {
o.state = typeof c == "function" ? c == null ? void 0 : c(o.prevState) : c;
});
};
return x(() => {
r(t.getRef(s));
}, [s]), x(() => {
t.listen((c) => {
c.key === s && r(c.state);
});
}, [s]), [e, n];
};
class F {
constructor(t) {
m(this, "neuron");
m(this, "useNeuron", (t) => B(t, this.client));
m(this, "name");
m(this, "client");
const e = new j(t);
this.name = t == null ? void 0 : t.name;
const r = t == null ? void 0 : t.modules;
this.neuron = (n, c) => P(n, { modules: r, ...c }, e.neuron), this.client = {
name: e.name,
has: e.has,
getRef: e.getRef,
getSnapshot: e.getSnapshot,
listen: e.listen,
dispatch: e.dispatch,
neuron: e.neuron,
getActions: e.getActions,
remove: e.remove
};
}
}
function G(s, t, e) {
const [r, n] = C(
e ? e(s.getRef(t)) : s.getRef(t)
), c = (u) => s.dispatch(t, (d) => d.state = u), o = (u) => {
var d;
if (e) {
const g = (d = e == null ? void 0 : e.toString().match(/\(\w+\) =>\s*(\S+)/)) == null ? void 0 : d[1], S = typeof g == "string" ? g == null ? void 0 : g.split(".").slice(1) : [], R = s.getRef(t), v = S.reduce(
(f, h) => f[h],
R
);
let N;
typeof u == "function" ? N = u == null ? void 0 : u(v) : N = u;
const i = ((f, h, p) => {
const A = h.pop(), b = h.reduce((l, U) => l[U] = l[U] || {}, f);
return b[A] = p, f;
})(
R,
S,
N
);
s.dispatch(
t,
(f) => f.state = { ...i }
);
}
}, a = e ? {
...s.getActions(t),
set: c,
setSlice: o
} : { ...s.getActions(t), set: c };
return x(() => {
s.listen((u) => {
u.key === t && n(e ? e(u.state) : u.state);
});
}, []), [r, a];
}
class H {
constructor(t) {
m(this, "clientContext");
m(this, "modules");
m(this, "privateNeuron", (t, e) => {
const r = (e == null ? void 0 : e.key) ?? crypto.randomUUID();
return [(o) => {
o.neuron(t, { ...e, key: r });
}, (o) => {
const a = $(this.clientContext);
return G(a, r, o);
}];
});
m(this, "useNeuronClient", (t) => {
const e = new j({
name: t == null ? void 0 : t.name,
modules: this.modules
});
return { client: e, Private: ({ children: n }) => {
const c = this.clientContext;
return /* @__PURE__ */ q(c.Provider, { value: e, children: n });
} };
});
this.modules = (t == null ? void 0 : t.modules) ?? [], this.clientContext = _(
null
);
}
}
globalThis.ReactNeuron = {
neuron: P,
NeuronClient: F,
PrivateNeuronClient: H
};
export {
F as NeuronClient,
H as PrivateNeuronClient,
P as neuron
};
//# sourceMappingURL=react.js.map