@sandstack/neuron
Version:
Neuron is a lightweight framework agnostic global state manager for Javascript apps, with React support
243 lines (242 loc) • 8.56 kB
JavaScript
var E = Object.defineProperty;
var w = (c, s, t) => s in c ? E(c, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[s] = t;
var i = (c, s, t) => (w(c, typeof s != "symbol" ? s + "" : s, t), t), S = (c, s, t) => {
if (!s.has(c))
throw TypeError("Cannot " + t);
};
var y = (c, s, t) => (S(c, s, "read from private field"), t ? t.call(c) : s.get(c)), v = (c, s, t) => {
if (s.has(c))
throw TypeError("Cannot add the same private member more than once");
s instanceof WeakSet ? s.add(c) : s.set(c, t);
}, C = (c, s, t, e) => (S(c, s, "write to private field"), e ? e.call(c, t) : s.set(c, t), t);
class d {
constructor() {
i(this, "eventEmitters", /* @__PURE__ */ new Map());
i(this, "payload");
}
/**
* Registers a callback to listen to changes associated with the given key.
* @param key - The unique identifier for the Neuron whose state changes should be listened to.
* @param callback - The function to invoke when the state changes.
*/
listen(s, t) {
var e, h;
this.eventEmitters.has(s) || this.eventEmitters.set(s, /* @__PURE__ */ new Set()), (e = this.eventEmitters.get(s)) == null || e.add(t), ((h = this.payload) == null ? void 0 : h.key) === s && t(this.payload);
}
/**
* Stops listening for changes for the given key and removes the specific callback.
* @param key - The unique identifier for the Neuron.
* @param callback - The callback to remove from the listeners.
*/
stopListening(s, t) {
const e = this.eventEmitters.get(s);
e && (e.delete(t), e.size === 0 && this.eventEmitters.delete(s));
}
/**
* Dispatches a payload to all registered listeners associated with the payload's key.
* @param payload - The payload containing the state and metadata to dispatch.
*/
dispatch(s) {
this.payload = s;
const t = this.eventEmitters.get(s.key);
t == null || t.forEach((e) => {
e(s);
});
}
}
var g;
class k {
constructor(s) {
i(this, "key");
i(this, "prevState");
i(this, "state");
v(this, g, !1);
i(this, "cancelDispatch", () => {
C(this, g, !0);
});
i(this, "isDispatchCancelled", () => y(this, g));
this.key = s.key, this.prevState = s.prevState, this.state = s.state;
}
}
g = new WeakMap();
class M {
constructor(s, t, e, h) {
i(this, "store");
i(this, "modules");
i(this, "dispatcher");
i(this, "key");
i(this, "set", (s) => {
var r, p, a, n;
const t = this.store.get(this.key), e = t.state, h = new k({
key: this.key,
prevState: e,
state: typeof s == "function" ? s(t == null ? void 0 : t.state) : s
});
if (h.state !== h.prevState) {
(r = t == null ? void 0 : t.onDispatch) == null || r.call(t, h);
for (let l = 0; l < this.modules.length; l++) {
if (!h.isDispatchCancelled()) {
(a = (p = this.modules[l]).onDispatch) == null || a.call(p, h);
continue;
}
break;
}
h.isDispatchCancelled() || (this.store.set(this.key, {
...t,
state: h == null ? void 0 : h.state,
prevState: e
}), this.dispatcher.dispatch(h), (n = t == null ? void 0 : t.onCallback) == null || n.call(t, h), this.modules.forEach((l) => {
var f;
(f = l == null ? void 0 : l.onCallback) == null || f.call(l, h);
}));
}
});
i(this, "getClone", () => {
var t;
const s = (t = this.store.get(this.key)) == null ? void 0 : t.state;
return structuredClone !== void 0 || structuredClone !== null ? structuredClone(s) : s;
});
i(this, "getRef", () => {
var s;
return (s = this.store.get(this.key)) == null ? void 0 : s.state;
});
i(this, "dispatch", (s) => {
var h, r, p, a;
const t = this.store.get(this.key), e = new k({
key: this.key,
state: t == null ? void 0 : t.state,
prevState: t == null ? void 0 : t.state
});
if (s(e), !e.isDispatchCancelled()) {
(h = t == null ? void 0 : t.onDispatch) == null || h.call(t, e);
for (let n = 0; n < this.modules.length; n++) {
if (!e.isDispatchCancelled()) {
(p = (r = this.modules[n]).onDispatch) == null || p.call(r, e);
continue;
}
break;
}
}
if (!e.isDispatchCancelled()) {
const n = {
...t,
state: e.state,
prevState: t.state
};
this.store.set(this.key, n), this.dispatcher.dispatch(e), (a = t == null ? void 0 : t.onCallback) == null || a.call(t, e), this.modules.forEach((l) => {
var f;
(f = l == null ? void 0 : l.onCallback) == null || f.call(l, e);
});
}
});
i(this, "getActions", () => {
var t;
const s = (t = this.store.get(this.key)) == null ? void 0 : t.actions;
return (s == null ? void 0 : s(this.dispatch)) ?? {};
});
i(this, "effect", (s) => {
this.dispatcher.stopListening(this.key, s), this.dispatcher.listen(this.key, s);
});
var p;
this.key = (t == null ? void 0 : t.key) ?? crypto.randomUUID(), this.store = e ?? /* @__PURE__ */ new Map(), this.modules = (t == null ? void 0 : t.modules) ?? [], this.dispatcher = h ?? new d();
const r = new k({
key: this.key,
state: s,
prevState: s
});
(p = t == null ? void 0 : t.onInit) == null || p.call(t, r), this.modules.forEach((a) => {
var n;
(n = a == null ? void 0 : a.onInit) == null || n.call(a, r);
}), this.store.set(r.key, {
...t,
key: r.key,
state: r == null ? void 0 : r.state,
prevState: r == null ? void 0 : r.prevState
});
}
}
class I {
constructor(s) {
i(this, "clientStore");
i(this, "clientModules");
i(this, "clientDispatcher");
i(this, "name");
i(this, "has", (s) => this.clientStore.has(s));
i(this, "remove", (s) => {
const t = this.clientStore.delete(s);
return this.clientDispatcher.stopListening(s, () => null), t;
});
i(this, "getRef", (s) => {
var t;
return (t = this.clientStore.get(s)) == null ? void 0 : t.state;
});
i(this, "getActions", (s) => {
var e;
const t = (e = this.clientStore.get(s)) == null ? void 0 : e.actions;
return (t == null ? void 0 : t((h) => this.dispatch(s, h))) ?? {};
});
i(this, "getSnapshot", () => Array.from(this.clientStore.entries()).map((s) => ({
key: s[1].key,
state: s[1].state
})));
i(this, "listen", (s) => {
this.clientStore.forEach((t, e) => {
this.clientDispatcher.stopListening(e, s);
}), this.clientStore.forEach((t, e) => {
this.clientDispatcher.listen(e, s);
});
});
i(this, "dispatch", (s, t) => {
var r, p;
const e = this.clientStore.get(s), h = new k({
key: s,
state: e == null ? void 0 : e.state,
prevState: e == null ? void 0 : e.state
});
t(h), this.clientModules.forEach((a) => {
var n;
(n = a == null ? void 0 : a.onDispatch) == null || n.call(a, h);
}), (r = e == null ? void 0 : e.onDispatch) == null || r.call(e, h), this.clientStore.set(s, {
...e,
state: h.state,
prevState: e.state
}), this.clientDispatcher.dispatch(h), this.clientModules.forEach((a) => {
var n;
(n = a == null ? void 0 : a.onCallback) == null || n.call(a, h);
}), (p = e == null ? void 0 : e.onCallback) == null || p.call(e, h);
});
i(this, "neuron", (s, t) => new M(
s,
{ modules: this.clientModules, ...t },
this.clientStore,
this.clientDispatcher
));
i(this, "client");
this.name = (s == null ? void 0 : s.name) ?? crypto.randomUUID(), this.clientStore = /* @__PURE__ */ new Map(), this.clientDispatcher = new d(), this.clientModules = (s == null ? void 0 : s.modules) ?? [], this.client = {
name: this.name,
has: this.has,
remove: this.remove,
getRef: this.getRef,
getActions: this.getActions,
getSnapshot: this.getSnapshot,
listen: this.listen,
dispatch: this.dispatch,
neuron: this.neuron
};
}
}
class R {
constructor(s) {
i(this, "name");
i(this, "onInit");
i(this, "onDispatch");
i(this, "onCallback");
this.name = s.name, this.onInit = s.onInit, this.onDispatch = s.onDispatch, this.onCallback = s.onCallback;
}
}
export {
R as Module,
M as Neuron,
I as NeuronClient
};
//# sourceMappingURL=core.js.map