diff --git a/dist/dde-with-signals.js b/dist/dde-with-signals.js index a716f22..b88d49c 100644 --- a/dist/dde-with-signals.js +++ b/dist/dde-with-signals.js @@ -1,7 +1,7 @@ //deka-dom-el library is available via global namespace `dde` (()=> { // src/signals-common.js - var x = { + var w = { isSignal(t) { return !1; }, @@ -9,22 +9,22 @@ return n; } }; - function P(t, e = !0) { - return e ? Object.assign(x, t) : (Object.setPrototypeOf(t, x), t); + function j(t, e = !0) { + return e ? Object.assign(w, t) : (Object.setPrototypeOf(t, w), t); } - function S(t) { - return x.isPrototypeOf(t) && t !== x ? t : x; + function A(t) { + return w.isPrototypeOf(t) && t !== w ? t : w; } // src/helpers.js function E(t) { return typeof t > "u"; } - function M(t) { + function k(t) { let e = typeof t; return e !== "object" ? e : t === null ? "null" : Object.prototype.toString.call(t); } - function A(t, e) { + function O(t, e) { if (!t || !(t instanceof AbortSignal)) return !0; if (!t.aborted) @@ -34,8 +34,8 @@ } // src/dom-common.js - var W = { setDeleteAttr: Z }; - function Z(t, e, n) { + var F = { setDeleteAttr: Q }; + function Q(t, e, n) { if (Reflect.set(t, e, n), !!E(n)) { if (Reflect.deleteProperty(t, e), t instanceof HTMLElement && t.getAttribute(e) === "undefined") return t.removeAttribute(e); @@ -50,7 +50,7 @@ namespace: "html", host: (t) => t ? t(document.body) : document.body, prevent: !0 - }], F = (t) => t === "svg" ? "http://www.w3.org/2000/svg" : t, m = { + }], z = (t) => t === "svg" ? "http://www.w3.org/2000/svg" : t, h = { get current() { return y[y.length - 1]; }, @@ -61,7 +61,7 @@ return this.current.namespace; }, set namespace(t) { - return this.current.namespace = F(t); + return this.current.namespace = z(t); }, preventDefault() { let { current: t } = this; @@ -71,7 +71,7 @@ let e = this.namespace; return this.namespace = t, { append(...n) { - return m.namespace = e, n.length === 1 ? n[0] : document.createDocumentFragment().append(...n); + return h.namespace = e, n.length === 1 ? n[0] : document.createDocumentFragment().append(...n); } }; }, @@ -79,17 +79,17 @@ return [...y]; }, push(t = {}) { - return t.namespace && (t.namespace = F(t.namespace)), y.push(Object.assign({}, this.current, { prevent: !1 }, t)); + return t.namespace && (t.namespace = z(t.namespace)), y.push(Object.assign({}, this.current, { prevent: !1 }, t)); }, pop() { return y.pop(); } }; - function at(t, e, ...n) { - let o = S(this), { namespace: r } = m, c = 0, s; + function pt(t, e, ...n) { + let o = A(this), { namespace: r } = h, c = 0, s; switch ((Object(e) !== e || o.isSignal(e)) && (e = { textContent: e }), !0) { case typeof t == "function": { - c = 1, m.push({ scope: t, host: (a) => a ? (c === 1 ? n.unshift(a) : a(s), void 0) : s }), s = t(e || void 0), (s instanceof HTMLElement ? B : G)(s, "Attribute", "dde-fun", t.name); + c = 1, h.push({ scope: t, host: (a) => a ? (c === 1 ? n.unshift(a) : a(s), void 0) : s }), s = t(e || void 0), (s instanceof HTMLElement ? I : J)(s, "Attribute", "dde-fun", t.name); break; } case t === "#text": @@ -104,100 +104,100 @@ case !s: s = R.call(this, document.createElement(t), e); } - return n.forEach((a) => a(s)), c && m.pop(), c = 2, s; + return n.forEach((a) => a(s)), c && h.pop(), c = 2, s; } - var { setDeleteAttr: z } = W, O = /* @__PURE__ */ new WeakMap(); + var { setDeleteAttr: H } = F, C = /* @__PURE__ */ new WeakMap(); function R(t, ...e) { if (!e.length) return t; - O.set(t, U(t, this)); + C.set(t, q(t, this)); for (let [n, o] of Object.entries(Object.assign({}, ...e))) - H.call(this, t, n, o); - return O.delete(t), t; + $.call(this, t, n, o); + return C.delete(t), t; } - function H(t, e, n) { - let { setRemoveAttr: o, s: r } = U(t, this), c = this; + function $(t, e, n) { + let { setRemoveAttr: o, s: r } = q(t, this), c = this; n = r.processReactiveAttribute( t, e, n, - (a, p) => H.call(c, t, a, p) + (a, p) => $.call(c, t, a, p) ); let [s] = e; if (s === "=") return o(e.slice(1), n); if (s === ".") - return k(t, e.slice(1), n); + return U(t, e.slice(1), n); if (/(aria|data)([A-Z])/.test(e)) return e = e.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(), o(e, n); switch (e === "className" && (e = "class"), e) { case "xlink:href": return o(e, n, "http://www.w3.org/1999/xlink"); case "textContent": - return z(t, e, n); + return H(t, e, n); case "style": if (typeof n != "object") break; case "dataset": - return j(r, n, k.bind(null, t[e])); + return T(r, n, U.bind(null, t[e])); case "ariaset": - return j(r, n, (a, p) => o("aria-" + a, p)); + return T(r, n, (a, p) => o("aria-" + a, p)); case "classList": - return K.call(c, t, n); + return X.call(c, t, n); } - return Q(t, e) ? z(t, e, n) : o(e, n); + return Y(t, e) ? H(t, e, n) : o(e, n); } - function U(t, e) { - if (O.has(t)) - return O.get(t); - let o = (t instanceof SVGElement ? G : B).bind(null, t, "Attribute"), r = S(e); + function q(t, e) { + if (C.has(t)) + return C.get(t); + let o = (t instanceof SVGElement ? J : I).bind(null, t, "Attribute"), r = A(e); return { setRemoveAttr: o, s: r }; } - function K(t, e) { - let n = S(this); - return j( + function X(t, e) { + let n = A(this); + return T( n, e, (o, r) => t.classList.toggle(o, r === -1 ? void 0 : !!r) ), t; } - function pt(t) { + function ht(t) { return Array.from(t.children).forEach((e) => e.remove()), t; } - function Q(t, e) { + function Y(t, e) { if (!Reflect.has(t, e)) return !1; - let n = $(t, e); + let n = B(t, e); return !E(n.set); } - function $(t, e) { + function B(t, e) { if (t = Object.getPrototypeOf(t), !t) return {}; let n = Object.getOwnPropertyDescriptor(t, e); - return n || $(t, e); + return n || B(t, e); } - function j(t, e, n) { + function T(t, e, n) { if (!(typeof e != "object" || e === null)) return Object.entries(e).forEach(function([r, c]) { r && (c = t.processReactiveAttribute(e, r, c, n), n(r, c)); }); } - function q(t) { + function G(t) { return Array.isArray(t) ? t.filter(Boolean).join(" ") : t; } - function B(t, e, n, o) { - return t[(E(o) ? "remove" : "set") + e](n, q(o)); + function I(t, e, n, o) { + return t[(E(o) ? "remove" : "set") + e](n, G(o)); } - function G(t, e, n, o, r = null) { - return t[(E(o) ? "remove" : "set") + e + "NS"](r, n, q(o)); + function J(t, e, n, o, r = null) { + return t[(E(o) ? "remove" : "set") + e + "NS"](r, n, G(o)); } - function k(t, e, n) { + function U(t, e, n) { if (Reflect.set(t, e, n), !!E(n)) return Reflect.deleteProperty(t, e); } // src/events.js - function gt(t, e, ...n) { + function bt(t, e, ...n) { let o = n.length ? new CustomEvent(e, { detail: n[0] }) : new Event(e); return t.dispatchEvent(o); } @@ -206,26 +206,26 @@ return r.addEventListener(t, e, n), r; }; } - var C = Y(), X = /* @__PURE__ */ new WeakSet(); + var L = et(), tt = /* @__PURE__ */ new WeakSet(); v.connected = function(t, e) { let n = "connected"; return typeof e != "object" && (e = {}), e.once = !0, function(r) { let c = "dde:" + n; - return r.addEventListener(c, t, e), r.__dde_lifecycleToEvents ? r : r.isConnected ? (r.dispatchEvent(new Event(c)), r) : (A(e.signal, () => C.offConnected(r, t)) && C.onConnected(r, t), r); + return r.addEventListener(c, t, e), r.__dde_lifecycleToEvents ? r : r.isConnected ? (r.dispatchEvent(new Event(c)), r) : (O(e.signal, () => L.offConnected(r, t)) && L.onConnected(r, t), r); }; }; v.disconnected = function(t, e) { let n = "disconnected"; return typeof e != "object" && (e = {}), e.once = !0, function(r) { let c = "dde:" + n; - return r.addEventListener(c, t, e), r.__dde_lifecycleToEvents || A(e.signal, () => C.offDisconnected(r, t)) && C.onDisconnected(r, t), r; + return r.addEventListener(c, t, e), r.__dde_lifecycleToEvents || O(e.signal, () => L.offDisconnected(r, t)) && L.onDisconnected(r, t), r; }; }; v.attributeChanged = function(t, e) { let n = "attributeChanged"; return typeof e != "object" && (e = {}), function(r) { let c = "dde:" + n; - if (r.addEventListener(c, t, e), r.__dde_lifecycleToEvents || X.has(r)) + if (r.addEventListener(c, t, e), r.__dde_lifecycleToEvents || tt.has(r)) return r; let s = new MutationObserver(function(p) { for (let { attributeName: b, target: _ } of p) @@ -233,10 +233,10 @@ new CustomEvent(c, { detail: [b, _.getAttribute(b)] }) ); }); - return A(e.signal, () => s.disconnect()) && s.observe(r, { attributes: !0 }), r; + return O(e.signal, () => s.disconnect()) && s.observe(r, { attributes: !0 }), r; }; }; - function Y() { + function et() { let t = /* @__PURE__ */ new Map(), e = !1, n = new MutationObserver(function(i) { for (let u of i) if (u.type === "childList") { @@ -307,18 +307,18 @@ } function b(i, u) { let f = !1; - for (let h of i) { - if (u && p(h).then(b), !t.has(h)) + for (let m of i) { + if (u && p(m).then(b), !t.has(m)) continue; - let w = t.get(h); - w.length_c && (h.dispatchEvent(new Event("dde:connected")), w.connected = /* @__PURE__ */ new WeakSet(), w.length_c = 0, w.length_d || t.delete(h), f = !0); + let x = t.get(m); + x.length_c && (m.dispatchEvent(new Event("dde:connected")), x.connected = /* @__PURE__ */ new WeakSet(), x.length_c = 0, x.length_d || t.delete(m), f = !0); } return f; } function _(i, u) { let f = !1; - for (let h of i) - u && p(h).then(_), !(!t.has(h) || !t.get(h).length_d) && (h.dispatchEvent(new Event("dde:disconnected")), t.delete(h), f = !0); + for (let m of i) + u && p(m).then(_), !(!t.has(m) || !t.get(m).length_d) && (m.dispatchEvent(new Event("dde:disconnected")), t.delete(m), f = !0); return f; } } @@ -333,25 +333,30 @@ // src/signals-lib.js var d = Symbol.for("Signal"); - function L(t) { + function N(t) { try { return Reflect.has(t, d); } catch { return !1; } } - var D = [], g = /* @__PURE__ */ new WeakMap(); - function l(t, e) { + var P = [], l = /* @__PURE__ */ new WeakMap(); + function g(t, e) { if (typeof t != "function") - return I(t, e); - if (L(t)) + return V(t, e); + if (N(t)) return t; - let n = I(), o = function() { - D.push(o), n(t()), D.pop(); + let n = V(), o = function() { + let [r, ...c] = l.get(o); + if (l.set(o, /* @__PURE__ */ new Set([r])), P.push(o), n(t()), P.pop(), !c.length) + return; + let s = l.get(o); + for (let a of c) + s.has(a) || S(a, o); }; - return g.set(o, /* @__PURE__ */ new Set([n])), g.set(n[d], o), o(), n; + return l.set(n[d], o), l.set(o, /* @__PURE__ */ new Set([n])), o(), n; } - l.action = function(t, e, ...n) { + g.action = function(t, e, ...n) { let o = t[d], { actions: r } = o; if (!r || !Reflect.has(r, e)) throw new Error(`'${t}' has no action with name '${e}'!`); @@ -359,30 +364,30 @@ return Reflect.deleteProperty(o, "skip"); o.listeners.forEach((c) => c(o.value)); }; - l.on = function t(e, n, o = {}) { + g.on = function t(e, n, o = {}) { let { signal: r } = o; if (!(r && r.aborted)) { if (Array.isArray(e)) return e.forEach((c) => t(c, n, o)); - T(e, n), r && r.addEventListener("abort", () => N(e, n)); + W(e, n), r && r.addEventListener("abort", () => S(e, n)); } }; - l.symbols = { + g.symbols = { signal: d, onclear: Symbol.for("Signal.onclear") }; - l.attribute = function(t, e = void 0) { - let { host: n } = m, o = n() && n().hasAttribute(t) ? n().getAttribute(t) : e, r = new AbortController(), c = l(o, { - [l.symbols.onclear]() { + g.attribute = function(t, e = void 0) { + let { host: n } = h, o = n() && n().hasAttribute(t) ? n().getAttribute(t) : e, r = new AbortController(), c = g(o, { + [g.symbols.onclear]() { r.abort(); } }); - return m.host(v.attributeChanged(function({ detail: a }) { + return h.host(v.attributeChanged(function({ detail: a }) { let [p, b] = a; p === t && c(b); }, { signal: r.signal })), c; }; - l.clear = function(...t) { + g.clear = function(...t) { for (let n of t) { Reflect.deleteProperty(n, "toJSON"); let o = n[d]; @@ -390,129 +395,136 @@ } function e(n, o) { o.listeners.forEach((r) => { - if (o.listeners.delete(r), !g.has(r)) + if (o.listeners.delete(r), !l.has(r)) return; - let c = g.get(r); - c.delete(n), !(c.size > 1) && (l.clear(...c), g.delete(r)); + let c = l.get(r); + c.delete(n), !(c.size > 1) && (g.clear(...c), l.delete(r)); }); } }; - l.el = function(t, e) { - let n = document.createComment("<#reactive>"), o = document.createComment(""), r = document.createDocumentFragment(); + var D = "__dde_reactive"; + g.el = function(t, e) { + let n = document.createComment(""), o = document.createComment(""), r = document.createDocumentFragment(); r.append(n, o); - let c = (s) => { + let { current: c } = h, s = (a) => { if (!n.parentNode || !o.parentNode) - return N(t, c); - let a = e(s); - Array.isArray(a) || (a = [a]); - let p = n; - for (; (p = n.nextSibling) !== o; ) - p.remove(); - n.after(...a); + return S(t, s); + h.push(c); + let p = e(a); + h.pop(), Array.isArray(p) || (p = [p]); + let b = n; + for (; (b = n.nextSibling) !== o; ) + b.remove(); + n.after(...p); }; - return T(t, c), V(t, c, n, e), c(t()), r; + return W(t, s), K(t, s, n, e), s(t()), r; }; - var J = { - isSignal: L, + var Z = { + isSignal: N, processReactiveAttribute(t, e, n, o) { - if (!L(n)) + if (!N(n)) return n; let r = (c) => o(e, c); - return T(n, r), V(n, r, t, e), n(); + return W(n, r), K(n, r, t, e), n(); } }; - function V(t, e, ...n) { - let { current: o } = m; - if (o.prevent) - return; - let r = "__dde_reactive"; - o.host(function(c) { - c[r] || (c[r] = [], v.disconnected( + function K(t, e, ...n) { + let { current: o } = h; + o.prevent || o.host(function(r) { + r[D] || (r[D] = [], v.disconnected( () => ( /*! * Clears all signals listeners added in the current scope/host (`S.el`, `assign`, …?). * You can investigate the `__dde_reactive` key of the element. * */ - c[r].forEach(([s]) => N(...s, t[d]?.host() === c)) + r[D].forEach(([[c, s]]) => S(c, s, c[d]?.host() === r)) ) - )(c)), c[r].push([[t, e], ...n]); + )(r)), r[D].push([[t, e], ...n]); }); } - function I(t, e) { - let n = (...o) => o.length ? ot(n, ...o) : rt(n); - return et(n, t, e); + function V(t, e) { + let n = (...o) => o.length ? st(n, ...o) : ct(n); + return rt(n, t, e); } - var tt = Object.assign(/* @__PURE__ */ Object.create(null), { + var nt = Object.assign(/* @__PURE__ */ Object.create(null), { stopPropagation() { this.skip = !0; } - }); - function et(t, e, n) { + }), M = class extends Error { + constructor() { + super(); + let [e, ...n] = this.stack.split(` + `), o = e.slice(e.indexOf("@"), e.indexOf(".js:") + 4); + this.stack = n.find((r) => !r.includes(o)); + } + }; + function rt(t, e, n) { let o = []; - M(n) !== "[object Object]" && (n = {}); - let { onclear: r } = l.symbols; + k(n) !== "[object Object]" && (n = {}); + let { onclear: r } = g.symbols; n[r] && (o.push(n[r]), Reflect.deleteProperty(n, r)); - let { host: c } = m; + let { host: c } = h; return t[d] = { value: e, actions: n, onclear: o, host: c, - listeners: /* @__PURE__ */ new Set() - }, t.toJSON = () => t(), Object.setPrototypeOf(t[d], tt), t; + listeners: /* @__PURE__ */ new Set(), + defined: new M() + }, t.toJSON = () => t(), Object.setPrototypeOf(t[d], nt), t; } - function nt() { - return D[D.length - 1]; + function ot() { + return P[P.length - 1]; } - function rt(t) { + function ct(t) { if (!t[d]) return; - let { value: e, listeners: n } = t[d], o = nt(); - return o && n.add(o), g.has(o) && g.get(o).add(t), e; + let { value: e, listeners: n } = t[d], o = ot(); + return o && n.add(o), l.has(o) && l.get(o).add(t), e; } - function ot(t, e, n) { + function st(t, e, n) { if (!t[d]) return; let o = t[d]; if (!(!n && o.value === e)) return o.value = e, o.listeners.forEach((r) => r(e)), e; } - function T(t, e) { + function W(t, e) { if (t[d]) return t[d].listeners.add(e); } - function N(t, e, n) { + function S(t, e, n) { let o = t[d]; if (!o) return; let r = o.listeners.delete(e); if (n && !o.listeners.size) { - if (l.clear(t), !g.has(o)) + if (g.clear(t), !l.has(o)) return r; - let c = g.get(o); - if (!g.has(c)) + let c = l.get(o); + if (!l.has(c)) return r; - g.get(c).forEach((s) => N(s, c, !0)); + l.get(c).forEach((s) => S(s, c, !0)); } return r; } // signals.js - P(J); + j(Z); globalThis.dde= { - S: l, + S: g, assign: R, - assignAttribute: H, - classListDeclarative: K, - createElement: at, - dispatchEvent: gt, - el: at, - empty: pt, - isSignal: L, + assignAttribute: $, + classListDeclarative: X, + createElement: pt, + dispatchEvent: bt, + el: pt, + empty: ht, + isSignal: N, on: v, - registerReactivity: P, - scope: m + registerReactivity: j, + scope: h }; })(); \ No newline at end of file diff --git a/dist/esm-with-signals.js b/dist/esm-with-signals.js index d10e671..40ca3af 100644 --- a/dist/esm-with-signals.js +++ b/dist/esm-with-signals.js @@ -1,5 +1,5 @@ // src/signals-common.js -var x = { +var w = { isSignal(t) { return !1; }, @@ -7,22 +7,22 @@ var x = { return n; } }; -function P(t, e = !0) { - return e ? Object.assign(x, t) : (Object.setPrototypeOf(t, x), t); +function j(t, e = !0) { + return e ? Object.assign(w, t) : (Object.setPrototypeOf(t, w), t); } -function S(t) { - return x.isPrototypeOf(t) && t !== x ? t : x; +function A(t) { + return w.isPrototypeOf(t) && t !== w ? t : w; } // src/helpers.js function E(t) { return typeof t > "u"; } -function M(t) { +function k(t) { let e = typeof t; return e !== "object" ? e : t === null ? "null" : Object.prototype.toString.call(t); } -function A(t, e) { +function O(t, e) { if (!t || !(t instanceof AbortSignal)) return !0; if (!t.aborted) @@ -32,8 +32,8 @@ function A(t, e) { } // src/dom-common.js -var W = { setDeleteAttr: Z }; -function Z(t, e, n) { +var F = { setDeleteAttr: Q }; +function Q(t, e, n) { if (Reflect.set(t, e, n), !!E(n)) { if (Reflect.deleteProperty(t, e), t instanceof HTMLElement && t.getAttribute(e) === "undefined") return t.removeAttribute(e); @@ -48,7 +48,7 @@ var y = [{ namespace: "html", host: (t) => t ? t(document.body) : document.body, prevent: !0 -}], F = (t) => t === "svg" ? "http://www.w3.org/2000/svg" : t, m = { +}], z = (t) => t === "svg" ? "http://www.w3.org/2000/svg" : t, h = { get current() { return y[y.length - 1]; }, @@ -59,7 +59,7 @@ var y = [{ return this.current.namespace; }, set namespace(t) { - return this.current.namespace = F(t); + return this.current.namespace = z(t); }, preventDefault() { let { current: t } = this; @@ -69,7 +69,7 @@ var y = [{ let e = this.namespace; return this.namespace = t, { append(...n) { - return m.namespace = e, n.length === 1 ? n[0] : document.createDocumentFragment().append(...n); + return h.namespace = e, n.length === 1 ? n[0] : document.createDocumentFragment().append(...n); } }; }, @@ -77,17 +77,17 @@ var y = [{ return [...y]; }, push(t = {}) { - return t.namespace && (t.namespace = F(t.namespace)), y.push(Object.assign({}, this.current, { prevent: !1 }, t)); + return t.namespace && (t.namespace = z(t.namespace)), y.push(Object.assign({}, this.current, { prevent: !1 }, t)); }, pop() { return y.pop(); } }; -function at(t, e, ...n) { - let o = S(this), { namespace: r } = m, c = 0, s; +function pt(t, e, ...n) { + let o = A(this), { namespace: r } = h, c = 0, s; switch ((Object(e) !== e || o.isSignal(e)) && (e = { textContent: e }), !0) { case typeof t == "function": { - c = 1, m.push({ scope: t, host: (a) => a ? (c === 1 ? n.unshift(a) : a(s), void 0) : s }), s = t(e || void 0), (s instanceof HTMLElement ? B : G)(s, "Attribute", "dde-fun", t.name); + c = 1, h.push({ scope: t, host: (a) => a ? (c === 1 ? n.unshift(a) : a(s), void 0) : s }), s = t(e || void 0), (s instanceof HTMLElement ? I : J)(s, "Attribute", "dde-fun", t.name); break; } case t === "#text": @@ -102,100 +102,100 @@ function at(t, e, ...n) { case !s: s = R.call(this, document.createElement(t), e); } - return n.forEach((a) => a(s)), c && m.pop(), c = 2, s; + return n.forEach((a) => a(s)), c && h.pop(), c = 2, s; } -var { setDeleteAttr: z } = W, O = /* @__PURE__ */ new WeakMap(); +var { setDeleteAttr: H } = F, C = /* @__PURE__ */ new WeakMap(); function R(t, ...e) { if (!e.length) return t; - O.set(t, U(t, this)); + C.set(t, q(t, this)); for (let [n, o] of Object.entries(Object.assign({}, ...e))) - H.call(this, t, n, o); - return O.delete(t), t; + $.call(this, t, n, o); + return C.delete(t), t; } -function H(t, e, n) { - let { setRemoveAttr: o, s: r } = U(t, this), c = this; +function $(t, e, n) { + let { setRemoveAttr: o, s: r } = q(t, this), c = this; n = r.processReactiveAttribute( t, e, n, - (a, p) => H.call(c, t, a, p) + (a, p) => $.call(c, t, a, p) ); let [s] = e; if (s === "=") return o(e.slice(1), n); if (s === ".") - return k(t, e.slice(1), n); + return U(t, e.slice(1), n); if (/(aria|data)([A-Z])/.test(e)) return e = e.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(), o(e, n); switch (e === "className" && (e = "class"), e) { case "xlink:href": return o(e, n, "http://www.w3.org/1999/xlink"); case "textContent": - return z(t, e, n); + return H(t, e, n); case "style": if (typeof n != "object") break; case "dataset": - return j(r, n, k.bind(null, t[e])); + return T(r, n, U.bind(null, t[e])); case "ariaset": - return j(r, n, (a, p) => o("aria-" + a, p)); + return T(r, n, (a, p) => o("aria-" + a, p)); case "classList": - return K.call(c, t, n); + return X.call(c, t, n); } - return Q(t, e) ? z(t, e, n) : o(e, n); + return Y(t, e) ? H(t, e, n) : o(e, n); } -function U(t, e) { - if (O.has(t)) - return O.get(t); - let o = (t instanceof SVGElement ? G : B).bind(null, t, "Attribute"), r = S(e); +function q(t, e) { + if (C.has(t)) + return C.get(t); + let o = (t instanceof SVGElement ? J : I).bind(null, t, "Attribute"), r = A(e); return { setRemoveAttr: o, s: r }; } -function K(t, e) { - let n = S(this); - return j( +function X(t, e) { + let n = A(this); + return T( n, e, (o, r) => t.classList.toggle(o, r === -1 ? void 0 : !!r) ), t; } -function pt(t) { +function ht(t) { return Array.from(t.children).forEach((e) => e.remove()), t; } -function Q(t, e) { +function Y(t, e) { if (!Reflect.has(t, e)) return !1; - let n = $(t, e); + let n = B(t, e); return !E(n.set); } -function $(t, e) { +function B(t, e) { if (t = Object.getPrototypeOf(t), !t) return {}; let n = Object.getOwnPropertyDescriptor(t, e); - return n || $(t, e); + return n || B(t, e); } -function j(t, e, n) { +function T(t, e, n) { if (!(typeof e != "object" || e === null)) return Object.entries(e).forEach(function([r, c]) { r && (c = t.processReactiveAttribute(e, r, c, n), n(r, c)); }); } -function q(t) { +function G(t) { return Array.isArray(t) ? t.filter(Boolean).join(" ") : t; } -function B(t, e, n, o) { - return t[(E(o) ? "remove" : "set") + e](n, q(o)); +function I(t, e, n, o) { + return t[(E(o) ? "remove" : "set") + e](n, G(o)); } -function G(t, e, n, o, r = null) { - return t[(E(o) ? "remove" : "set") + e + "NS"](r, n, q(o)); +function J(t, e, n, o, r = null) { + return t[(E(o) ? "remove" : "set") + e + "NS"](r, n, G(o)); } -function k(t, e, n) { +function U(t, e, n) { if (Reflect.set(t, e, n), !!E(n)) return Reflect.deleteProperty(t, e); } // src/events.js -function gt(t, e, ...n) { +function bt(t, e, ...n) { let o = n.length ? new CustomEvent(e, { detail: n[0] }) : new Event(e); return t.dispatchEvent(o); } @@ -204,26 +204,26 @@ function v(t, e, n) { return r.addEventListener(t, e, n), r; }; } -var C = Y(), X = /* @__PURE__ */ new WeakSet(); +var L = et(), tt = /* @__PURE__ */ new WeakSet(); v.connected = function(t, e) { let n = "connected"; return typeof e != "object" && (e = {}), e.once = !0, function(r) { let c = "dde:" + n; - return r.addEventListener(c, t, e), r.__dde_lifecycleToEvents ? r : r.isConnected ? (r.dispatchEvent(new Event(c)), r) : (A(e.signal, () => C.offConnected(r, t)) && C.onConnected(r, t), r); + return r.addEventListener(c, t, e), r.__dde_lifecycleToEvents ? r : r.isConnected ? (r.dispatchEvent(new Event(c)), r) : (O(e.signal, () => L.offConnected(r, t)) && L.onConnected(r, t), r); }; }; v.disconnected = function(t, e) { let n = "disconnected"; return typeof e != "object" && (e = {}), e.once = !0, function(r) { let c = "dde:" + n; - return r.addEventListener(c, t, e), r.__dde_lifecycleToEvents || A(e.signal, () => C.offDisconnected(r, t)) && C.onDisconnected(r, t), r; + return r.addEventListener(c, t, e), r.__dde_lifecycleToEvents || O(e.signal, () => L.offDisconnected(r, t)) && L.onDisconnected(r, t), r; }; }; v.attributeChanged = function(t, e) { let n = "attributeChanged"; return typeof e != "object" && (e = {}), function(r) { let c = "dde:" + n; - if (r.addEventListener(c, t, e), r.__dde_lifecycleToEvents || X.has(r)) + if (r.addEventListener(c, t, e), r.__dde_lifecycleToEvents || tt.has(r)) return r; let s = new MutationObserver(function(p) { for (let { attributeName: b, target: _ } of p) @@ -231,10 +231,10 @@ v.attributeChanged = function(t, e) { new CustomEvent(c, { detail: [b, _.getAttribute(b)] }) ); }); - return A(e.signal, () => s.disconnect()) && s.observe(r, { attributes: !0 }), r; + return O(e.signal, () => s.disconnect()) && s.observe(r, { attributes: !0 }), r; }; }; -function Y() { +function et() { let t = /* @__PURE__ */ new Map(), e = !1, n = new MutationObserver(function(i) { for (let u of i) if (u.type === "childList") { @@ -305,18 +305,18 @@ function Y() { } function b(i, u) { let f = !1; - for (let h of i) { - if (u && p(h).then(b), !t.has(h)) + for (let m of i) { + if (u && p(m).then(b), !t.has(m)) continue; - let w = t.get(h); - w.length_c && (h.dispatchEvent(new Event("dde:connected")), w.connected = /* @__PURE__ */ new WeakSet(), w.length_c = 0, w.length_d || t.delete(h), f = !0); + let x = t.get(m); + x.length_c && (m.dispatchEvent(new Event("dde:connected")), x.connected = /* @__PURE__ */ new WeakSet(), x.length_c = 0, x.length_d || t.delete(m), f = !0); } return f; } function _(i, u) { let f = !1; - for (let h of i) - u && p(h).then(_), !(!t.has(h) || !t.get(h).length_d) && (h.dispatchEvent(new Event("dde:disconnected")), t.delete(h), f = !0); + for (let m of i) + u && p(m).then(_), !(!t.has(m) || !t.get(m).length_d) && (m.dispatchEvent(new Event("dde:disconnected")), t.delete(m), f = !0); return f; } } @@ -331,25 +331,30 @@ function Y() { // src/signals-lib.js var d = Symbol.for("Signal"); -function L(t) { +function N(t) { try { return Reflect.has(t, d); } catch { return !1; } } -var D = [], g = /* @__PURE__ */ new WeakMap(); -function l(t, e) { +var P = [], l = /* @__PURE__ */ new WeakMap(); +function g(t, e) { if (typeof t != "function") - return I(t, e); - if (L(t)) + return V(t, e); + if (N(t)) return t; - let n = I(), o = function() { - D.push(o), n(t()), D.pop(); + let n = V(), o = function() { + let [r, ...c] = l.get(o); + if (l.set(o, /* @__PURE__ */ new Set([r])), P.push(o), n(t()), P.pop(), !c.length) + return; + let s = l.get(o); + for (let a of c) + s.has(a) || S(a, o); }; - return g.set(o, /* @__PURE__ */ new Set([n])), g.set(n[d], o), o(), n; + return l.set(n[d], o), l.set(o, /* @__PURE__ */ new Set([n])), o(), n; } -l.action = function(t, e, ...n) { +g.action = function(t, e, ...n) { let o = t[d], { actions: r } = o; if (!r || !Reflect.has(r, e)) throw new Error(`'${t}' has no action with name '${e}'!`); @@ -357,30 +362,30 @@ l.action = function(t, e, ...n) { return Reflect.deleteProperty(o, "skip"); o.listeners.forEach((c) => c(o.value)); }; -l.on = function t(e, n, o = {}) { +g.on = function t(e, n, o = {}) { let { signal: r } = o; if (!(r && r.aborted)) { if (Array.isArray(e)) return e.forEach((c) => t(c, n, o)); - T(e, n), r && r.addEventListener("abort", () => N(e, n)); + W(e, n), r && r.addEventListener("abort", () => S(e, n)); } }; -l.symbols = { +g.symbols = { signal: d, onclear: Symbol.for("Signal.onclear") }; -l.attribute = function(t, e = void 0) { - let { host: n } = m, o = n() && n().hasAttribute(t) ? n().getAttribute(t) : e, r = new AbortController(), c = l(o, { - [l.symbols.onclear]() { +g.attribute = function(t, e = void 0) { + let { host: n } = h, o = n() && n().hasAttribute(t) ? n().getAttribute(t) : e, r = new AbortController(), c = g(o, { + [g.symbols.onclear]() { r.abort(); } }); - return m.host(v.attributeChanged(function({ detail: a }) { + return h.host(v.attributeChanged(function({ detail: a }) { let [p, b] = a; p === t && c(b); }, { signal: r.signal })), c; }; -l.clear = function(...t) { +g.clear = function(...t) { for (let n of t) { Reflect.deleteProperty(n, "toJSON"); let o = n[d]; @@ -388,126 +393,133 @@ l.clear = function(...t) { } function e(n, o) { o.listeners.forEach((r) => { - if (o.listeners.delete(r), !g.has(r)) + if (o.listeners.delete(r), !l.has(r)) return; - let c = g.get(r); - c.delete(n), !(c.size > 1) && (l.clear(...c), g.delete(r)); + let c = l.get(r); + c.delete(n), !(c.size > 1) && (g.clear(...c), l.delete(r)); }); } }; -l.el = function(t, e) { - let n = document.createComment("<#reactive>"), o = document.createComment(""), r = document.createDocumentFragment(); +var D = "__dde_reactive"; +g.el = function(t, e) { + let n = document.createComment(""), o = document.createComment(""), r = document.createDocumentFragment(); r.append(n, o); - let c = (s) => { + let { current: c } = h, s = (a) => { if (!n.parentNode || !o.parentNode) - return N(t, c); - let a = e(s); - Array.isArray(a) || (a = [a]); - let p = n; - for (; (p = n.nextSibling) !== o; ) - p.remove(); - n.after(...a); + return S(t, s); + h.push(c); + let p = e(a); + h.pop(), Array.isArray(p) || (p = [p]); + let b = n; + for (; (b = n.nextSibling) !== o; ) + b.remove(); + n.after(...p); }; - return T(t, c), V(t, c, n, e), c(t()), r; + return W(t, s), K(t, s, n, e), s(t()), r; }; -var J = { - isSignal: L, +var Z = { + isSignal: N, processReactiveAttribute(t, e, n, o) { - if (!L(n)) + if (!N(n)) return n; let r = (c) => o(e, c); - return T(n, r), V(n, r, t, e), n(); + return W(n, r), K(n, r, t, e), n(); } }; -function V(t, e, ...n) { - let { current: o } = m; - if (o.prevent) - return; - let r = "__dde_reactive"; - o.host(function(c) { - c[r] || (c[r] = [], v.disconnected( +function K(t, e, ...n) { + let { current: o } = h; + o.prevent || o.host(function(r) { + r[D] || (r[D] = [], v.disconnected( () => ( /*! * Clears all signals listeners added in the current scope/host (`S.el`, `assign`, …?). * You can investigate the `__dde_reactive` key of the element. * */ - c[r].forEach(([s]) => N(...s, t[d]?.host() === c)) + r[D].forEach(([[c, s]]) => S(c, s, c[d]?.host() === r)) ) - )(c)), c[r].push([[t, e], ...n]); + )(r)), r[D].push([[t, e], ...n]); }); } -function I(t, e) { - let n = (...o) => o.length ? ot(n, ...o) : rt(n); - return et(n, t, e); +function V(t, e) { + let n = (...o) => o.length ? st(n, ...o) : ct(n); + return rt(n, t, e); } -var tt = Object.assign(/* @__PURE__ */ Object.create(null), { +var nt = Object.assign(/* @__PURE__ */ Object.create(null), { stopPropagation() { this.skip = !0; } -}); -function et(t, e, n) { +}), M = class extends Error { + constructor() { + super(); + let [e, ...n] = this.stack.split(` +`), o = e.slice(e.indexOf("@"), e.indexOf(".js:") + 4); + this.stack = n.find((r) => !r.includes(o)); + } +}; +function rt(t, e, n) { let o = []; - M(n) !== "[object Object]" && (n = {}); - let { onclear: r } = l.symbols; + k(n) !== "[object Object]" && (n = {}); + let { onclear: r } = g.symbols; n[r] && (o.push(n[r]), Reflect.deleteProperty(n, r)); - let { host: c } = m; + let { host: c } = h; return t[d] = { value: e, actions: n, onclear: o, host: c, - listeners: /* @__PURE__ */ new Set() - }, t.toJSON = () => t(), Object.setPrototypeOf(t[d], tt), t; + listeners: /* @__PURE__ */ new Set(), + defined: new M() + }, t.toJSON = () => t(), Object.setPrototypeOf(t[d], nt), t; } -function nt() { - return D[D.length - 1]; +function ot() { + return P[P.length - 1]; } -function rt(t) { +function ct(t) { if (!t[d]) return; - let { value: e, listeners: n } = t[d], o = nt(); - return o && n.add(o), g.has(o) && g.get(o).add(t), e; + let { value: e, listeners: n } = t[d], o = ot(); + return o && n.add(o), l.has(o) && l.get(o).add(t), e; } -function ot(t, e, n) { +function st(t, e, n) { if (!t[d]) return; let o = t[d]; if (!(!n && o.value === e)) return o.value = e, o.listeners.forEach((r) => r(e)), e; } -function T(t, e) { +function W(t, e) { if (t[d]) return t[d].listeners.add(e); } -function N(t, e, n) { +function S(t, e, n) { let o = t[d]; if (!o) return; let r = o.listeners.delete(e); if (n && !o.listeners.size) { - if (l.clear(t), !g.has(o)) + if (g.clear(t), !l.has(o)) return r; - let c = g.get(o); - if (!g.has(c)) + let c = l.get(o); + if (!l.has(c)) return r; - g.get(c).forEach((s) => N(s, c, !0)); + l.get(c).forEach((s) => S(s, c, !0)); } return r; } // signals.js -P(J); +j(Z); export { - l as S, + g as S, R as assign, - H as assignAttribute, - K as classListDeclarative, - at as createElement, - gt as dispatchEvent, - at as el, - pt as empty, - L as isSignal, + $ as assignAttribute, + X as classListDeclarative, + pt as createElement, + bt as dispatchEvent, + pt as el, + ht as empty, + N as isSignal, v as on, - P as registerReactivity, - m as scope + j as registerReactivity, + h as scope }; diff --git a/src/signals-lib.js b/src/signals-lib.js index f4c55dc..25545fb 100644 --- a/src/signals-lib.js +++ b/src/signals-lib.js @@ -6,7 +6,13 @@ export function isSignal(candidate){ } /** @type {function[]} */ const stack_watch= []; -/** @type {WeakMap,Set>|function>} */ +/** + * ### `WeakMap>>` + * The `Set` is in the form of `[ source, ...depended signals (DSs) ]`. When the DS is cleaned it is removed from DSs, if remains only one (`source`) it is cleared too. + * ### `WeakMap` + * This is used for revesed deps, the `function` is also key for `deps`. + * @type {WeakMap>|function>} + * */ const deps= new WeakMap(); export function S(value, actions){ if(typeof value!=="function") @@ -15,12 +21,22 @@ export function S(value, actions){ const out= create(); const contextReWatch= function(){ + const [ origin, ...deps_old ]= deps.get(contextReWatch); + deps.set(contextReWatch, new Set([ origin ])); + stack_watch.push(contextReWatch); out(value()); stack_watch.pop(); + + if(!deps_old.length) return; + const deps_curr= deps.get(contextReWatch); + for (const dep_signal of deps_old){ + if(deps_curr.has(dep_signal)) continue; + removeSignalListener(dep_signal, contextReWatch); + } }; - deps.set(contextReWatch, new Set([ out ])); deps.set(out[mark], contextReWatch); + deps.set(contextReWatch, new Set([ out ])); contextReWatch(); return out; } @@ -82,15 +98,19 @@ S.clear= function(...signals){ }); } }; +const key_reactive= "__dde_reactive"; S.el= function(signal, map){ - const mark_start= document.createComment("<#reactive>"); - const mark_end= document.createComment(""); + const mark_start= document.createComment(``); + const mark_end= document.createComment(""); const out= document.createDocumentFragment(); out.append(mark_start, mark_end); + const { current }= scope; const reRenderReactiveElement= v=> { if(!mark_start.parentNode || !mark_end.parentNode) return removeSignalListener(signal, reRenderReactiveElement); + scope.push(current); let els= map(v); + scope.pop(); if(!Array.isArray(els)) els= [ els ]; let el_r= mark_start; @@ -107,30 +127,30 @@ S.el= function(signal, map){ import { typeOf } from './helpers.js'; export const signals_config= { isSignal, - processReactiveAttribute(_, key, attrs, set){ + processReactiveAttribute(element, key, attrs, set){ if(!isSignal(attrs)) return attrs; const l= attr=> set(key, attr); addSignalListener(attrs, l); - removeSignalsFromElements(attrs, l, _, key); + removeSignalsFromElements(attrs, l, element, key); return attrs(); } }; function removeSignalsFromElements(signal, listener, ...notes){ const { current }= scope; if(current.prevent) return; - const k= "__dde_reactive"; current.host(function(element){ - if(!element[k]){ - element[k]= []; + if(!element[key_reactive]){ + element[key_reactive]= []; on.disconnected(()=> /*! * Clears all signals listeners added in the current scope/host (`S.el`, `assign`, …?). - * You can investigate the `__dde_reactive` key of the element. - * */ - element[k].forEach(([ sl ])=> removeSignalListener(...sl, signal[mark]?.host() === element)) + * You can investigate the `__dde_reactive` key of the element. + * */ + element[key_reactive].forEach(([ [ signal, listener ] ])=> + removeSignalListener(signal, listener, signal[mark]?.host() === element)) )(element); } - element[k].push([ [ signal, listener ], ...notes ]); + element[key_reactive].push([ [ signal, listener ], ...notes ]); }); } @@ -144,6 +164,14 @@ const protoSigal= Object.assign(Object.create(null), { this.skip= true; } }); +class SignalDefined extends Error{ + constructor(){ + super(); + const [ curr, ...rest ]= this.stack.split("\n"); + const curr_file= curr.slice(curr.indexOf("@"), curr.indexOf(".js:")+4); + this.stack= rest.find(l=> !l.includes(curr_file)); + } +} function toSignal(signal, value, actions){ const onclear= []; if(typeOf(actions)!=="[object Object]") @@ -156,7 +184,8 @@ function toSignal(signal, value, actions){ const { host }= scope; signal[mark]= { value, actions, onclear, host, - listeners: new Set() + listeners: new Set(), + defined: new SignalDefined() }; signal.toJSON= ()=> signal(); Object.setPrototypeOf(signal[mark], protoSigal);