@traxjs/trax
Version:
Reactive state management
1,153 lines (1,152 loc) • 34.4 kB
JavaScript
let ie;
function Ee(r) {
r.value = null, r.next = ie, ie = r;
}
function we(r, s) {
if (ie) {
const c = ie;
return ie = c.next, c.value = r, c.next = s, c;
}
}
class Pe {
constructor() {
this._size = 0;
}
get head() {
return this._head;
}
get size() {
return this._size;
}
/**
* Add a new value at the head of the list
* @param value
* @returns the list item
*/
add(s) {
const c = this._head;
let f = we(s, c) || { value: s, next: c };
return this._head = f, this._size++, f;
}
/**
* Insert an item in the lhe linked list
* @param fn function that decides where to insert the item.
* Once the function returns an item, the iteration will stop
*/
insert(s) {
let c = this._head, f;
if (!c)
f = s() || void 0, f !== void 0 && this.add(f);
else {
let u;
for (; u || c; ) {
if (f = s(u == null ? void 0 : u.value, c == null ? void 0 : c.value) || void 0, f !== void 0) {
let i = we(f, c) || { value: f, next: c };
u ? u.next = i : this._head = i, this._size++;
return;
}
u = c, c = c == null ? void 0 : c.next;
}
}
}
/**
* Return the item value at the head of the list,
* but doesn't remove it from the list
* @returns the head value or undefined
*/
peek() {
return this._head ? this._head.value : void 0;
}
/**
* Return the item value at the head of the list,
* and remove it from the list
* @returns the head value or undefined
*/
shift() {
const s = this._head;
if (s) {
const c = s.value;
return this._head = s.next, this._size--, Ee(s), c;
}
}
/**
* Scan the list and remove the first item with the corresponding value
* @param value
* @returns true if an item was found and removed
*/
remove(s) {
let c = this._head, f = null;
for (; c; ) {
if (c.value === s)
return f ? (f.next = c.next, this._size--, Ee(c)) : this.shift(), !0;
f = c, c = c.next;
}
return !1;
}
}
var Y;
(function(r) {
r.NotATraxObject = "", r.Object = "O", r.Array = "A", r.Store = "S", r.Processor = "P";
})(Y || (Y = {}));
const p = Object.freeze({
/** When info data are logged */
Info: "!LOG",
/** When a warning is logged */
Warning: "!WRN",
/** When an error is logged */
Error: "!ERR",
/** When a cycle is created */
CycleStart: "!CS",
/** When a cycle ends */
CycleComplete: "!CC",
/** When a trax entity is created (e.g. object / processor / store) */
New: "!NEW",
/** When a trax entity is disposed (e.g. object / processor / store) */
Dispose: "!DEL",
/** When an object property is set (changed) */
Set: "!SET",
/** When an object property is read */
Get: "!GET",
/** When a processor is set dirty */
ProcessorDirty: "!DRT",
/** When a lazy processor is skipped */
ProcessorSkipped: "!SKP",
/** When a processing context starts */
ProcessingStart: "!PCS",
/** When an async processing context pauses */
ProcessingPause: "!PCP",
/** When an async processing context resumes */
ProcessingResume: "!PCR",
/** When a processing context ends */
ProcessingEnd: "!PCE"
}), ke = /* @__PURE__ */ new Set();
Object.getOwnPropertyNames(p).forEach((r) => {
ke.add(p[r]);
});
var Xe = globalThis && globalThis.__awaiter || function(r, s, c, f) {
function u(i) {
return i instanceof c ? i : new c(function(m) {
m(i);
});
}
return new (c || (c = Promise))(function(i, m) {
function L(I) {
try {
w(f.next(I));
} catch (k) {
m(k);
}
}
function v(I) {
try {
w(f.throw(I));
} catch (k) {
m(k);
}
}
function w(I) {
I.done ? i(I.value) : u(I.value).then(L, v);
}
w((f = f.apply(r, s || [])).next());
});
};
const Ie = /* @__PURE__ */ new Set(["", "Main", "AllButGet", "All"]), Re = "color: #65abff", _e = "color: ", je = "color: #e08d00;font-weight:bold", Ue = "color: #00ff00;font-weight:bold";
function Fe(r, s, c) {
let f = 0, u = 1e3, i, m;
const L = /* @__PURE__ */ new Map(), v = [];
let w = "", I = -1, k = -1, U = null, A = 0;
function T() {
return U === null && (k = -1, I++, U = Promise.resolve().then(R), E(p.CycleStart)), k++, I + ":" + k;
}
function R() {
H(), E(p.CycleComplete), U = null;
}
function E(l) {
l === p.CycleComplete && c && c();
const g = Date.now(), x = A !== 0 ? g - A : 0;
A = g, z(l, { elapsedTime: x }, r);
}
const D = 1, W = 2, K = 3, F = new Pe();
function Z(l) {
F.add(l);
}
function J(l) {
let g = F.shift();
for (; g && g !== l; )
C("[trax/processing context] Contexts must be ended or paused before parent:", g.id), g = F.shift();
}
function H() {
if (F.size !== 0) {
let l = F.shift();
for (; l; )
C("[trax/processing context] Contexts must be ended or paused before cycle ends:", l.id), l = F.shift();
}
}
function ee(l, g) {
let x = D;
const y = Object.assign({ processId: l }, g), d = {
get id() {
return l;
},
pause() {
x !== D ? C("[trax/processing context] Only started or resumed contexts can be paused:", l) : (J(d), z(p.ProcessingPause, y, r), x = W);
},
resume() {
x !== W ? C("[trax/processing context] Only paused contexts can be resumed:", l) : (Z(d), z(p.ProcessingResume, y, r), x = D);
},
end() {
x === K ? C("[trax/processing context] Contexts cannot be ended twice:", l) : (J(d), z(p.ProcessingEnd, y, r), x = K);
}
};
return Z(d), d;
}
function C(...l) {
z(p.Error, ye(l));
}
function z(l, g, x, y) {
let d;
if (f >= u && u > 1 ? (d = i, i = i.next, f--, d.id = "", d.type = "", d.next = d.data = d.parentId = void 0) : d = { id: "", type: "" }, Be(r, d, l, s, g, x), d.id = T(), d.parentId = y, d.type !== "") {
i === void 0 ? (i = m = d, f = 1) : (m.next = d, m = d, f++);
for (const _ of v)
try {
_({ id: d.id, type: d.type, data: d.data });
} catch {
}
M(d.type, d);
}
if (w) {
const _ = d.type;
let G = !1;
if (w === "All" ? G = !0 : w === "AllButGet" ? G = _ !== p.Get : w === "Main" && (ke.has(_) ? G = _ === p.Error || _ === p.Info || _ === p.Warning || _ === p.ProcessingResume || _ === p.ProcessorDirty || _ === p.ProcessorSkipped || _ === p.Set || _ === p.ProcessingStart : G = !0), G && _ !== p.CycleStart && _ !== p.CycleComplete) {
let q = Ye(d.type, d.data, !0), te = "";
d.parentId && (te = " - parent:%c" + d.parentId);
const e = `%cTRX %c${d.id} %c${d.type}${q ? "%c - " + q : ""}${te}`, t = e.replaceAll("%c", ""), o = Math.floor((e.length - t.length) / 2), a = [e, Re, _e, Ue];
for (let n = 3; o > n; n++)
a.push(n % 2 ? _e : je);
console.log.apply(console, a);
}
}
return d;
}
function M(l, g) {
const x = L.get(l);
if (x) {
const y = x.filter((d) => !d({ id: g.id, type: g.type, data: g.data, parentId: g.parentId }));
y.length === 0 ? L.delete(l) : L.set(l, y);
}
}
return {
get consoleOutput() {
return w;
},
set consoleOutput(l) {
if (Ie.has(l))
w = l;
else {
const g = `"${[...Ie].join('" or "')}"`;
console.log(`%cTRX %cInvalid consoleOutput value - should be either %c${g}`, Re, "color: ", je);
}
},
event(l, g, x) {
z(l, g, x);
},
info(...l) {
z(p.Info, ye(l));
},
warn(...l) {
z(p.Warning, ye(l));
},
error: C,
set maxSize(l) {
const g = u;
if (l < 0 ? u = -1 : l < 2 ? u = 2 : u = l, u > 0 && u < g && u < f) {
let x = f - u;
for (; i && x; )
i = i.next, x--, f--;
}
},
startProcessingContext(l, g) {
let x = l.name;
x.charAt(0) === "!" && g !== r && (C(`Processing Context name cannot start with reserved prefix: ${x}`), l.name = x.replace(/\!+/, ""));
const y = F.peek(), d = y ? y.id : void 0, _ = z(p.ProcessingStart, l, r, d);
return ee(_.id, l);
},
get maxSize() {
return u;
},
get size() {
return f;
},
scan(l) {
let g = i, x = !0;
for (; x && g; )
try {
l(g) === !1 && (x = !1), g = g.next;
} catch {
}
},
lastEvent() {
return m;
},
awaitEvent(l, g) {
return Xe(this, void 0, void 0, function* () {
if (l === "" || l === "*")
return z(p.Error, `[trax/eventStream.await] Invalid event type: '${l}'`), { id: m.id, type: m.type, data: m.data };
let x = L.get(l);
x === void 0 && (x = [], L.set(l, x));
let y, d = new Promise((_) => {
y = _;
});
return x.push((_) => Je(_, g) ? (y(_), !0) : !1), d;
});
},
subscribe(l, g) {
let x;
return l === "*" ? x = (y) => g(y) : x = (y) => {
y.type === l && g(y);
}, v.push(x), x;
},
unsubscribe(l) {
const g = v.indexOf(l);
return g > -1 ? (v.splice(g, 1), !0) : !1;
}
};
}
function Je(r, s) {
if (s === void 0 || r.data === void 0)
return !0;
const c = JSON.parse(r.data);
if (typeof s != "object" && s !== null)
return c === s;
if (s !== null && c !== null && typeof c == "object") {
for (const f of Object.keys(s)) {
const u = c[f], i = s[f];
if (i instanceof RegExp)
if (typeof u == "string") {
if (u.match(i) === null)
return !1;
} else
return !1;
else if (u !== s[f])
return !1;
}
return !0;
}
return !1;
}
function ye(r) {
let s = "";
const c = [];
for (let f of r) {
const u = typeof f;
u === "string" || u === "number" || u === "boolean" || f === null ? s ? s += " " + f : s = "" + f : (s && (c.push(s), s = ""), c.push(f));
}
if (s && c.push(s), c.length !== 0)
return c.length === 1 ? c[0] : c;
}
function Be(r, s, c, f, u, i) {
let m = !1, L = "";
if (c === "")
m = !0, L = "Event type cannot be empty";
else if (c.charAt(0) === "!" && i !== r && c !== p.Error && c !== p.Warning && c !== p.Info)
m = !0, L = "Event type cannot start with reserved prefix: " + c;
else if (s.type = c, u !== void 0)
try {
f ? s.data = f(u) : s.data = JSON.stringify(u);
} catch (v) {
m = !0, L = "Event strinfication error: " + v;
}
else
u = void 0;
m && (s.type = p.Error, s.data = L);
}
function Ye(r, s, c = !1) {
if (!s || !r || r.charAt(0) !== "!")
return s;
const f = c ? "%c" : "";
try {
const u = JSON.parse("" + s);
if (r === p.CycleStart || r === p.CycleComplete)
return `${f}0`;
if (r === p.Info || r === p.Warning || r === p.Error)
return `${f}${s.replace(/"/g, "")}`;
if (r === p.ProcessingPause || r === p.ProcessingResume || r === p.ProcessingEnd)
return `${JSON.parse(s).processId}`;
if (r === p.ProcessingStart) {
const i = u;
if (i.name === "!StoreInit")
return `${i.name} (${f}${i.storeId}${f})`;
if (i.name === "!Compute") {
const m = i.isRenderer ? "R" : "";
return `${i.name} #${i.computeCount} (${f}${i.processorId}${f}) P${i.processorPriority}${m} ${i.trigger}`;
} else
return i.name === "!Reconciliation" ? `${i.name} #${i.index} - ${i.processorCount} processor${i.processorCount !== 1 ? "s" : ""}` : i.name === "!ArrayUpdate" ? `${i.name} (${f}${i.objectId}${f})` : `${f}${i.name}`;
} else if (r === p.New) {
const i = u;
return i.objectId === void 0 ? s : `${i.objectType}: ${f}${i.objectId}`;
} else if (r === p.Dispose) {
const i = u;
return i.objectId === void 0 ? s : `${f}${i.objectId}`;
} else if (r === p.Get) {
const i = u;
return `${f}${i.objectId}.${i.propName}${f} -> ${f}${ge(i.propValue)}`;
} else if (r === p.Set) {
const i = u;
return `${f}${i.objectId}.${i.propName}${f} = ${f}${ge(i.toValue)}${f} (prev: ${ge(i.fromValue)})`;
} else if (r === p.ProcessorDirty) {
const i = u;
return `${f}${i.processorId}${f} <- ${f}${i.objectId}.${i.propName}`;
} else if (r === p.ProcessorSkipped)
return `${f}${u.processorId}`;
} catch {
}
return s;
}
function ge(r) {
if (r === void 0)
return "undefined";
if (r === null)
return "null";
if (typeof r == "object") {
const s = N(r);
return s ? s.id : JSON.stringify(r);
} else
return typeof r == "string" ? "'" + r.replace(/\'/g, "\\'") + "'" : "" + r;
}
function ve() {
}
function xe(r, s, c, f = ve, u = ve) {
let i = null, m, L, v, w;
return (...A) => {
f(), i = s();
let T, R = !0;
m = void 0;
let E;
try {
E = r(...A), E && E.next && typeof E.next == "function" ? (m = E, T = E.next(), R = !!T.done) : E && typeof E == "object" && typeof E.then == "function" && typeof E.catch == "function" && E.catch((D) => {
c(D);
});
} catch (D) {
c(D);
}
return u(R), R ? (i.end(), i = null) : (i.pause(), I(T, m)), m !== void 0 ? (L && w && w("Processing Cancelled"), L = v = w = void 0, R ? Promise.resolve(E) : (L = new Promise((D, W) => {
v = D, w = W;
}), L)) : E;
};
function I(A, T) {
if (A && !A.done) {
const R = A.value, E = (D) => {
c(D);
};
R && R.then ? R.then((D) => {
k(D, T);
}).catch(E) : Promise.resolve().then(() => {
k(R, T);
}).catch(E);
}
}
function k(A, T) {
if (T !== m || !i)
return;
if (f() === !1) {
U();
return;
}
i.resume();
let R, E = !0;
try {
R = T.next(A), E = !!R.done;
} catch (D) {
c(D), E = !0, w && w(D), U();
}
u(E), E ? (i.end(), v && v(R.value)) : (i.pause(), I(R, T));
}
function U() {
v = w = void 0;
}
}
const Ke = "~";
function Ze(r, s, c, f, u, i, m, L, v, w, I = !0, k = !1) {
let U = 0, A = -1, T = !0, R = !1, E = !1, D = /* @__PURE__ */ new Set(), W = /* @__PURE__ */ new Set(), K, F = -1, Z = "DirectCall", J, H = !1;
s && s[0] === Ke && (v != null ? H = !0 : ee(`(${r}) Eager processors must not use the ~ prefix`));
function ee(y) {
m({ type: p.Error, data: y });
}
let C, z;
g(f);
const M = {
get id() {
return r;
},
get target() {
return v;
},
get reconciliationId() {
return F;
},
get autoCompute() {
return I;
},
get priority() {
return c;
},
get computeCount() {
return U;
},
get dirty() {
return T;
},
get isRenderer() {
return k;
},
get isLazy() {
return H;
},
get disposed() {
return R;
},
get dependencies() {
return R ? [] : Array.from(D).sort();
},
onDirty: null,
notifyChange(y, d) {
if (R || E)
return !1;
if (!T && D.has(l(y, d))) {
if (T = !0, m({ type: p.ProcessorDirty, processorId: r, objectId: y, propName: d }), this.onDirty)
try {
this.onDirty();
} catch (_) {
ee(`(${r}) onDirty callback execution error: ${_}`);
}
return !0;
}
return !1;
},
registerDependency(y, d, _) {
!E || R || _ === "then" && y.then === void 0 || (D.add(l(d, _)), W.add(d), rt(M, N(i(d))));
},
compute(y = !1, d = "DirectCall", _ = -1) {
var G;
if (R || !y && !I && (d === "Init" || d === "Reconciliation"))
return;
let q = !0;
const te = d === "TargetRead" && u.size > 0;
if (H && !te && (q = ((G = N(v)) === null || G === void 0 ? void 0 : G.hasExternalPropListener) || !1), q && T || y) {
T = !1, D.clear(), K = W, W = /* @__PURE__ */ new Set(), U++, F = _, Z = d, z && (g(z), z = void 0), J = {
processorId: r,
processorName: s,
computeCount: U,
maxComputeCount: A
};
let e;
v !== null ? e = C(v, J) : e = C(J), e && typeof e == "object" && typeof e.catch == "function" && e.catch(() => {
}), I && D.size === 0 && ee(`(${r}) No dependencies found: processor will never be re-executed`);
} else
q || m({ type: p.ProcessorSkipped, processorId: r });
},
updateComputeFn(y) {
z = y;
},
dispose() {
if (R)
return !1;
R = !0, E = !1, w && w(r);
for (const y of W)
ze(M, N(i(y)));
return m({ type: p.Dispose, objectId: r }), !0;
}
};
return m({ type: p.New, objectId: r, objectType: Y.Processor }), M.compute(!1, "Init"), M;
function l(y, d) {
return y + "." + d;
}
function g(y) {
C = xe(y, () => L({
type: "!PCS",
name: "!Compute",
processorId: r,
processorPriority: c,
trigger: Z,
isRenderer: k,
computeCount: U
}), (d) => {
ee(`(${r}) Compute error: ${d}`);
}, () => (E = !0, u.add(M), R ? !1 : void 0), (d) => {
E = !1, u.shift(), A = (J == null ? void 0 : J.maxComputeCount) || -1, d && A > -1 && U >= A ? M.dispose() : x();
});
}
function x() {
if (K) {
for (const y of K)
W.has(y) || ze(M, N(i(y)));
K = void 0;
}
}
}
var He = globalThis && globalThis.__awaiter || function(r, s, c, f) {
function u(i) {
return i instanceof c ? i : new c(function(m) {
m(i);
});
}
return new (c || (c = Promise))(function(i, m) {
function L(I) {
try {
w(f.next(I));
} catch (k) {
m(k);
}
}
function v(I) {
try {
w(f.throw(I));
} catch (k) {
m(k);
}
}
function w(I) {
I.done ? i(I.value) : u(I.value).then(L, v);
}
w((f = f.apply(r, s || [])).next());
});
};
const be = /* @__PURE__ */ new WeakMap(), oe = Symbol("trax.proxy.target"), Ae = Symbol("trax.dict.size"), Oe = /(\/|\>|\.|\#)/g, fe = "data", qe = ":", Qe = "*", et = "-", tt = "$", Le = "$", me = "#", De = ">", nt = "/", ue = "☆trax.dictionary.size☆";
function rt(r, s) {
s && (s.propListeners || (s.propListeners = /* @__PURE__ */ new Set()), s.propListeners.add(r), s.contentProcessors !== void 0 && !s.hasExternalPropListener && (!r.target || N(r.target) !== s) && (s.hasExternalPropListener = !0));
}
function ze(r, s) {
if (s && s.propListeners)
if (s.propListeners.delete(r), s.propListeners.size === 0)
s.propListeners = void 0, s.hasExternalPropListener = !1;
else if (s.contentProcessors) {
let c = !1;
for (const f of s.propListeners)
!c && (!f.target || N(f.target) !== s) && (c = !0);
s.hasExternalPropListener = c;
} else
s.hasExternalPropListener = !1;
}
function N(r) {
return r ? be.get(r[oe] || r) : void 0;
}
function le(r, s, c, f) {
const u = {
id: s,
type: c,
storeId: f,
propListeners: void 0,
computedProps: void 0,
computedContent: void 0,
awLevel: void 0,
dictSize: void 0,
contentProcessors: void 0,
hasExternalPropListener: !1,
processingLazyCheck: !1
};
return be.set(r[oe] || r, u), u;
}
function it(r) {
be.delete(r[oe] || r);
}
function ot() {
const r = {};
let s = 0;
const c = /* @__PURE__ */ new Map(), f = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), m = Array.isArray;
let L = 0, v = 0;
const w = new Pe();
let I = new Pe(), k = 0;
const A = Fe(r, (e) => JSON.stringify(e, (t, o) => {
if (typeof o == "object") {
const a = N(o);
return a ? `[TRAX ${a.id}]` : o;
} else if (typeof o == "function")
return "[Function]";
return o;
}), () => {
x(), R.processChanges();
});
let T = !1;
const R = {
log: A,
createStore(e, t) {
return te(e, "", t);
},
get pendingChanges() {
return I.size > 0;
},
processChanges() {
if (I.size > 0) {
k++;
const e = M({
type: "!PCS",
name: "!Reconciliation",
index: k,
processorCount: v
});
let t = I.shift();
for (; t; )
t.reconciliationId === k ? C(`(${t.id}) Circular reference: Processors cannot run twice during reconciliation`) : t.compute(!1, "Reconciliation", k), t = I.shift();
e.end();
}
},
reconciliation() {
return He(this, void 0, void 0, function* () {
let e = A.lastEvent();
(!e || e.type === p.CycleComplete) && (yield Promise.resolve(1)), (I.size || e && e.type !== p.CycleComplete) && (yield A.awaitEvent(p.CycleComplete));
});
},
isTraxObject(e) {
return N(e) !== void 0;
},
getTraxId(e) {
var t;
return ((t = N(e)) === null || t === void 0 ? void 0 : t.id) || "";
},
getTraxObjectType(e) {
var t;
return ((t = N(e)) === null || t === void 0 ? void 0 : t.type) || Y.NotATraxObject;
},
getProcessor(e) {
return u.get(e);
},
getStore(e) {
return c.get(e);
},
getData(e) {
return y(e) || void 0;
},
getActiveProcessor() {
return w.peek();
},
updateArray(e, t) {
if (!m(e) || !m(t)) {
C("updateArray: Invalid argument (array expected)");
return;
}
const o = E(e), a = M({ type: p.ProcessingStart, name: "!ArrayUpdate", objectId: o }), n = e.length, b = t.length;
for (let $ = 0; b > $; $++)
e[$] = t[$];
if (b < n) {
for (let $ = b; n > $; $++)
e[$] = void 0;
e.splice(b, n - b);
}
a.end();
},
updateDictionary(e, t) {
if (e === null || typeof e != "object") {
C("updateDictionary: Invalid argument (object expected)");
return;
}
const o = E(e), a = M({ type: p.ProcessingStart, name: "!DictionaryUpdate", objectId: o }), n = R.getObjectKeys(e), b = R.getObjectKeys(t);
for (const $ of n)
b.includes($) || delete e[$];
for (const $ of b)
e[$] = t[$];
a.end();
},
getObjectKeys(e) {
return N(e) ? e[Ae] ? Object.keys(e) : [] : Object.keys(e);
}
};
function E(e) {
const t = N(e);
if (t) {
ee(t);
const o = w.peek();
return o && (t.computedContent ? o.id !== t.computedContent && C(`Computed content conflict: ${t.id} can only be changed by ${t.computedContent}`) : t.computedContent = o.id), t.id;
}
return "";
}
const D = {
/**
* Function called on each property get
* @param target the original object that is proxied
* @param prop the property name
*/
get(e, t) {
const o = N(e);
if (t === oe)
return e;
if (t === "toJSON")
return;
if (typeof t == "string") {
let a, n = !1;
if (o) {
const b = w.peek();
b && b.registerDependency(e, o.id, t), g(o), n = t !== "then" && t !== "constructor" || a !== void 0, e[t] !== void 0 && (a = e[t] = W(e[t], t, o)), n && z({ type: "!GET", objectId: o.id, propName: t, propValue: a });
} else
a = e[t];
return a;
} else if (t === Ae && o) {
let a = o.dictSize;
a === void 0 && (o.dictSize = a = Object.keys(e).length);
const n = w.peek();
return n && n.registerDependency(e, o.id, ue), g(o), z({ type: "!GET", objectId: o.id, propName: ue, propValue: a }), a;
}
return e[t];
},
/**
* Function called on each property set
* @param target the original object that is proxied
* @param prop the property name
* @param value the value
*/
set(e, t, o) {
if (typeof t != "symbol") {
const a = e[t], n = N(e);
if (n) {
const b = w.peek();
if (ee(n), b) {
let $ = n.computedContent || void 0;
if (!n.computedContent) {
let X = n.computedProps;
X || (X = n.computedProps = {}), $ = X[t], $ || (X[t] = b.id);
}
$ && $ !== b.id && (n.computedContent ? C(`Computed content conflict: ${n.id}.${t} can only be set by ${$}`) : C(`Computed property conflict: ${n.id}.${t} can only be set by ${$}`), o = a);
} else
n.computedContent && (C(`Computed content conflict: ${n.id}.${t} can only be set by ${n.computedContent}`), o = a);
if (a !== o) {
let $ = !1, X = !1;
if (m(e)) {
const ne = e.length;
o = e[t] = W(o, "" + t, n), $ = e.length !== ne;
} else {
o = e[t] = W(o, "" + t, n);
const ne = n.dictSize;
if (a === void 0 && ne !== void 0) {
const se = Object.keys(e).length;
se !== ne && (n.dictSize = se, X = !0);
}
}
z({ type: "!SET", objectId: n.id, propName: t, fromValue: a, toValue: o }), typeof t == "string" && l(n, t), $ && l(n, "length"), X && l(n, ue);
}
} else
e[t] = o;
}
return !0;
},
/**
* Proxy handler method called when a property is deleted through the delete operator
* @param target
* @param prop
*/
deleteProperty(e, t) {
if (typeof t == "string" && t in e) {
const o = N(e);
return o && o.dictSize && (o.dictSize--, l(o, ue)), delete e[t], !0;
}
return !1;
}
};
function W(e, t, o) {
if (e != null && typeof e == "object") {
if (e[oe])
return e;
let a = N(e);
if (a)
return e;
let n = 0;
if (t[0] === Le) {
let b = 0, $ = t.length;
for (; b < $ && t[b] === Le; ) {
n++;
b++;
}
} else
o.awLevel && (n = o.awLevel);
if (n !== 1 && !a && (e = K(o.id + Qe + t, e, o.storeId, !0), n && n > 1)) {
let b = N(e);
b && !b.awLevel && (b.awLevel = n - 1);
}
}
return e;
}
function K(e, t, o, a = !1) {
const n = y(e);
if (n)
if (t && a) {
const X = e;
for (; s++, e = X + tt + s, !!f.get(e); )
;
} else
return n;
let b;
m(t) ? b = le(t, e, Y.Array, o) : b = le(t, e, Y.Object, o), z({ type: "!NEW", objectId: e, objectType: b.type });
const $ = new Proxy(t, D);
return d(e, $, o), $;
}
return R;
function F(e) {
const t = e.priority, o = e.isRenderer;
I.insert((a, n) => {
if (n)
if (o) {
if (n.isRenderer)
return t <= n.priority ? e : void 0;
} else
return n.isRenderer || t <= n.priority ? e : void 0;
else
return e;
});
}
function Z(e, t) {
let o = "";
if (m(e) ? o = e.map((a) => {
if (typeof a == "object") {
const n = N(a);
if (n) {
const b = n.id;
if (t) {
const $ = t.length + 1;
if (b.length > $ && b.slice(0, $) === t + "/")
return b.slice($);
}
return b.replace(/\//g, et);
} else
return C("Invalid id param: not a trax object"), J();
}
return "" + a;
}).join(qe) : o = e, o.match(Oe)) {
const a = o.replace(Oe, "");
C(`Invalid trax id: ${o} (changed into ${a})`), o = a;
}
return o;
}
function J() {
return "" + Math.floor(Math.random() * 1e5);
}
function H(e, t, o) {
let a = Z(e, t);
return o ? t + me + a : t + nt + a;
}
function ee(e, t) {
if (e) {
o(e.computedContent) && (e.computedContent = void 0);
const a = e.computedProps;
a && t && o(a[t]) && (a[t] = void 0);
}
function o(a) {
return !!(a && !u.has(a));
}
}
function C(e) {
A.error("[TRAX] " + e);
}
function z(e) {
e.type === p.Error ? C("" + e.data) : e.type === p.Info ? A.info("" + e.data) : A.event(e.type, e, r);
}
function M(e) {
return A.startProcessingContext(e, r);
}
function l(e, t) {
const o = e.propListeners;
if (o)
for (const a of o)
a.notifyChange(e.id, t) && F(a);
}
function g(e) {
if (!e.processingLazyCheck && e.contentProcessors) {
e.processingLazyCheck = !0;
for (const t of e.contentProcessors)
t.isLazy && t.compute(!1, "TargetRead");
e.processingLazyCheck = !1;
}
}
function x() {
!T && globalThis.__TRAX_DEVTOOLS__ && (T = !0, console.log("[Trax] DevTools detected"), globalThis.__TRAX_DEVTOOLS__.connectTrax(R));
}
function y(e) {
const t = f.get(e);
return t && t.deref() || null;
}
function d(e, t, o) {
f.set(e, new WeakRef(t)), G(e, o);
}
function _(e, t, o) {
if (t && it(t), o) {
if (o.contentProcessors) {
for (const a of o.contentProcessors)
a.dispose();
o.contentProcessors = void 0;
}
q(e, o.storeId);
}
return z({ type: "!DEL", objectId: e }), f.delete(e);
}
function G(e, t) {
let o = i.get(t);
o || (o = /* @__PURE__ */ new Set(), i.set(t, o)), o.add(e);
}
function q(e, t) {
let o = i.get(t);
o && o.delete(e);
}
function te(e, t, o, a) {
const n = $e(e, t, !0);
let b, $ = !0, X = !1;
const ne = M({ type: "!PCS", name: "!StoreInit", storeId: n }), se = typeof o == "function" ? o : (h) => {
h.init(o);
}, ce = {
get id() {
return n;
},
get data() {
return b;
},
get disposed() {
return X;
},
createStore(h, P) {
const S = te(h, n, P, Te);
return G(S.id, n), S;
},
init(h, P) {
return $ ? b = pe(fe, h, !0, P) : C(`(${n}) Store.init can only be called during the store init phase`), b;
},
add(h, P, S) {
return pe(h, P, !1, S);
},
get(h) {
const P = H(h, n, !1);
return y(P) || void 0;
},
remove(h) {
const P = N(h);
let S = "";
if (P)
if (S = P.id, P.type === Y.Processor)
C(`(${S}) Processors cannot be disposed through store.remove()`);
else if (P.type === Y.Store)
C(`(${S}) Stores cannot be disposed through store.remove()`);
else if (S.slice(n.length + 1) === fe)
C(`(${S}) Root objects cannot be disposed through store.remove()`);
else
return _(S, h, P);
return !1;
},
compute(h, P, S, j) {
const O = Z(h, n), V = H(O, n, !0);
return Se(V, O, P, null, S, j);
},
getProcessor(h) {
const P = H(h, n, !0);
return u.get(P);
},
getStore(h) {
const P = $e(h, n, !1);
return c.get(P);
},
dispose() {
return ae();
},
async(h, P) {
let S = "[ASYNC]", j;
typeof h == "string" ? (S = h, j = P) : j = h;
const O = xe(j, () => A.startProcessingContext({ name: n + "." + S + "()", storeId: n }), (V) => {
C(`(${n}.${S}) error: ${V}`);
});
return O.updateAsyncName = (V) => {
S = V;
}, O;
}
};
le(ce, n, Y.Store, ""), z({ type: "!NEW", objectId: n, objectType: Y.Store }), c.set(n, ce);
function ae() {
if (X)
return !1;
c.delete(n), X = !0, a && a(n);
const h = i.get(n);
if (h) {
i.delete(n);
const P = n.length;
let S, j;
for (const O of h)
j = void 0, S = O.charAt(P), S === De ? (j = c.get(O), j && j.dispose()) : S === me ? (j = u.get(O), j && j.dispose(), u.get(O) && (console.error("Unexpected processor dispose error"), u.delete(O))) : _(O);
h.clear();
}
return z({ type: "!DEL", objectId: n }), !0;
}
function Te(h) {
q(h, n);
}
function Ne(h) {
u.delete(h) && (v--, q(h, n));
}
function Me(h) {
const P = h;
for (const S of Object.keys(P))
if (typeof P[S] == "function") {
const j = P[S];
typeof j.updateAsyncName == "function" ? j.updateAsyncName(S) : P[S] = xe(j, () => A.startProcessingContext({ name: n + "." + S + "()", storeId: n }), (O) => {
C(`(${n}.${S}) error: ${O}`);
});
}
}
let Q;
try {
Q = se(ce), $ = !1, Q !== void 0 && (Q !== null && typeof Q == "object" ? Me(Q) : (C(`createStore init function must return a valid object (${n})`), Q = {}));
} catch (h) {
C(`createStore init error (${n}): ${h}`), Q = {};
}
if ($ = !1, Ve(), Q === void 0)
return ne.end(), ce;
const re = Q;
if (typeof re.dispose == "function") {
const h = re.dispose;
re.dispose = () => {
h.call(Q), ae();
};
} else
re.dispose = ae;
return re.id && C(`Store id will be overridden and must not be provided by init function (${n})`), re.id = n, ne.end(), re;
function Ve() {
b == null && (C(`(${n}) createStore init must define a root data object - see also: init()`), b = pe(fe, {}, !0));
}
function We() {
return X ? (C(`(${n}) Stores cannot be used after being disposed`), !1) : !0;
}
function $e(h, P, S = !0) {
let j = Z(h);
P !== "" && (j = P + De + j);
let O = "";
if (S) {
let V = c.get(j), B = 0;
for (; V; )
O = "" + ++B, V = V = c.get(j + O);
}
return j + O;
}
function pe(h, P, S, j) {
let O = Z(h, n);
if (S || O === fe && (C("Store.add: Invalid id 'data' (reserved)"), O = J()), We()) {
(P == null || typeof P != "object") && (C(`(${n}) Store.add(${h}): Invalid init object parameter: [${typeof P}]`), P = {});
const V = K(H(O, n, !1), P, n);
if (j !== void 0) {
const B = N(V), Ce = [];
for (const he of Object.getOwnPropertyNames(j)) {
const Ge = `${n}${me}${O}[${he}]`;
Ce.push(Se(Ge, he, j[he], V, !0, !1));
}
B.contentProcessors = Ce;
}
return V;
} else
return P;
}
function Se(h, P, S, j, O, V) {
let B = u.get(h);
return B ? (B.updateComputeFn(S), B) : (L++, v++, B = Ze(h, P, L, S, w, y, z, M, j, Ne, O, V), B.disposed || (le(B, h, Y.Processor, n), u.set(h, B), G(h, n)), B);
}
}
}
let de;
globalThis.trax ? de = globalThis.trax : de = ot();
globalThis.trax = de;
const st = de;
export {
Y as TraxObjectType,
st as trax,
p as traxEvents
};