1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-22 07:49:38 +01:00

Signals finalization

This commit is contained in:
Jan Andrle 2023-09-07 17:35:23 +02:00
parent 55a8f14b6b
commit 6fae2fc4e6
Signed by: jaandrle
GPG Key ID: B3A25AED155AFFAB
5 changed files with 215 additions and 161 deletions

View File

@ -7,7 +7,7 @@
} }
// src/signals-common.js // src/signals-common.js
var m = { var b = {
isReactiveAtrribute(e, t) { isReactiveAtrribute(e, t) {
return !1; return !1;
}, },
@ -21,11 +21,11 @@
return null; return null;
} }
}; };
function S(e, t = !0) { function C(e, t = !0) {
return t ? Object.assign(m, e) : (Object.setPrototypeOf(e, m), e); return t ? Object.assign(b, e) : (Object.setPrototypeOf(e, b), e);
} }
function j(e) { function j(e) {
return m.isPrototypeOf(e) && e !== m ? e : m; return b.isPrototypeOf(e) && e !== b ? e : b;
} }
// src/dom.js // src/dom.js
@ -65,41 +65,41 @@
if (!t.length) if (!t.length)
return e; return e;
let r = e instanceof SVGElement, o = (r ? z : P).bind(null, e, "Attribute"); let r = e instanceof SVGElement, o = (r ? z : P).bind(null, e, "Attribute");
return Object.entries(Object.assign({}, ...t)).forEach(function a([u, i]) { return Object.entries(Object.assign({}, ...t)).forEach(function a([f, i]) {
n.isReactiveAtrribute(i, u) && (i = n.processReactiveAttribute(e, u, i, a)); n.isReactiveAtrribute(i, f) && (i = n.processReactiveAttribute(e, f, i, a));
let [p] = u; let [p] = f;
if (p === "=") if (p === "=")
return o(u.slice(1), i); return o(f.slice(1), i);
if (p === ".") if (p === ".")
return L(e, u.slice(1), i); return L(e, f.slice(1), i);
if (typeof i == "object") if (typeof i == "object")
switch (u) { switch (f) {
case "style": case "style":
return w(i, P.bind(null, e.style, "Property")); return y(i, P.bind(null, e.style, "Property"));
case "dataset": case "dataset":
return w(i, L.bind(null, e.dataset)); return y(i, L.bind(null, e.dataset));
case "ariaset": case "ariaset":
return w(i, (E, v) => o("aria-" + E, v)); return y(i, (E, v) => o("aria-" + E, v));
case "classList": case "classList":
return k(e, i); return k(e, i);
default: default:
return Reflect.set(e, u, i); return Reflect.set(e, f, i);
} }
if (/(aria|data)([A-Z])/.test(u)) if (/(aria|data)([A-Z])/.test(f))
return u = u.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(), o(u, i); return f = f.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(), o(f, i);
switch (u) { switch (f) {
case "xlink:href": case "xlink:href":
return o(u, i, "http://www.w3.org/1999/xlink"); return o(f, i, "http://www.w3.org/1999/xlink");
case "textContent": case "textContent":
if (!r) if (!r)
break; break;
return e.appendChild(document.createTextNode(i)); return e.appendChild(document.createTextNode(i));
} }
return q(e, u) ? L(e, u, i) : o(u, i); return q(e, f) ? L(e, f, i) : o(f, i);
}), e; }), e;
} }
function k(e, t) { function k(e, t) {
return typeof t != "object" || w( return typeof t != "object" || y(
t, t,
(n, r) => e.classList.toggle(n, r === -1 ? void 0 : !!r) (n, r) => e.classList.toggle(n, r === -1 ? void 0 : !!r)
), e; ), e;
@ -114,8 +114,8 @@
let r = e.nodeName + "," + t; let r = e.nodeName + "," + t;
if (x.has(r)) if (x.has(r))
return x.get(r); return x.get(r);
let [o, a, u] = T(e, t), i = !N(o.set); let [o, a, f] = T(e, t), i = !N(o.set);
return (!i || a) && x.set(u === HTMLElement.prototype ? n : r, i), i; return (!i || a) && x.set(f === HTMLElement.prototype ? n : r, i), i;
} }
function T(e, t, n = 0) { function T(e, t, n = 0) {
if (e = Object.getPrototypeOf(e), !e) if (e = Object.getPrototypeOf(e), !e)
@ -123,7 +123,7 @@
let r = Object.getOwnPropertyDescriptor(e, t); let r = Object.getOwnPropertyDescriptor(e, t);
return r ? [r, n, e] : T(e, t, n + 1); return r ? [r, n, e] : T(e, t, n + 1);
} }
function w(e, t) { function y(e, t) {
return Object.entries(e).forEach(([n, r]) => t(n, r)); return Object.entries(e).forEach(([n, r]) => t(n, r));
} }
function N(e) { function N(e) {
@ -143,15 +143,15 @@
function D(e, t, n) { function D(e, t, n) {
return (r) => (r.addEventListener(e, t, n), r); return (r) => (r.addEventListener(e, t, n), r);
} }
var y = I(); var w = I();
D.connected = function(e, t) { D.connected = function(e, t) {
return function(r) { return function(r) {
y.onConnected(r, e), t && t.signal && t.signal.addEventListener("abort", () => y.offConnected(r, e)); w.onConnected(r, e), t && t.signal && t.signal.addEventListener("abort", () => w.offConnected(r, e));
}; };
}; };
D.disconnected = function(e, t) { D.disconnected = function(e, t) {
return function(r) { return function(r) {
y.onDisconnected(r, e), t && t.signal && t.signal.addEventListener("abort", () => y.offDisconnected(r, e)); w.onDisconnected(r, e), t && t.signal && t.signal.addEventListener("abort", () => w.offDisconnected(r, e));
}; };
}; };
function I() { function I() {
@ -159,10 +159,10 @@
for (let s of c) for (let s of c)
if (s.type === "childList") { if (s.type === "childList") {
if (E(s.addedNodes, !0)) { if (E(s.addedNodes, !0)) {
u(); f();
continue; continue;
} }
v(s.removedNodes, !0) && u(); v(s.removedNodes, !0) && f();
} }
}); });
return { return {
@ -172,8 +172,8 @@
offConnected(c, s) { offConnected(c, s) {
if (!e.has(c)) if (!e.has(c))
return; return;
let f = e.get(c), d = f.connected; let u = e.get(c), d = u.connected;
d.splice(d.indexOf(s), 1), r(c, f); d.splice(d.indexOf(s), 1), r(c, u);
}, },
onDisconnected(c, s) { onDisconnected(c, s) {
a(), o(c).disconnected.push(s); a(), o(c).disconnected.push(s);
@ -181,12 +181,12 @@
offDisconnected(c, s) { offDisconnected(c, s) {
if (!e.has(c)) if (!e.has(c))
return; return;
let f = e.get(c), d = f.disconnected; let u = e.get(c), d = u.disconnected;
d.splice(d.indexOf(s), 1), r(c, f); d.splice(d.indexOf(s), 1), r(c, u);
} }
}; };
function r(c, s) { function r(c, s) {
s.connected.length || s.disconnect.length || (e.delete(c), u()); s.connected.length || s.disconnect.length || (e.delete(c), f());
} }
function o(c) { function o(c) {
if (e.has(c)) if (e.has(c))
@ -197,7 +197,7 @@
function a() { function a() {
t || (t = !0, n.observe(document.body, { childList: !0, subtree: !0 })); t || (t = !0, n.observe(document.body, { childList: !0, subtree: !0 }));
} }
function u() { function f() {
!t || e.size || (t = !1, n.disconnect()); !t || e.size || (t = !1, n.disconnect());
} }
function i() { function i() {
@ -210,24 +210,24 @@
let s = []; let s = [];
if (!(c instanceof Node)) if (!(c instanceof Node))
return s; return s;
for (let f of e.keys()) for (let u of e.keys())
f === c || !(f instanceof Node) || c.contains(f) && s.push(f); u === c || !(u instanceof Node) || c.contains(u) && s.push(u);
return s; return s;
} }
function E(c, s) { function E(c, s) {
for (let f of c) { for (let u of c) {
if (s && p(f).then(E), !e.has(f)) if (s && p(u).then(E), !e.has(u))
return !1; return !1;
let d = e.get(f); let d = e.get(u);
return d.connected.forEach((C) => C(f)), d.connected.length = 0, d.disconnected.length || e.delete(f), !0; return d.connected.forEach((S) => S(u)), d.connected.length = 0, d.disconnected.length || e.delete(u), !0;
} }
} }
function v(c, s) { function v(c, s) {
for (let f of c) { for (let u of c) {
if (s && p(f).then(v), !e.has(f)) if (s && p(u).then(v), !e.has(u))
return !1; return !1;
let d = e.get(f); let d = e.get(u);
return d.disconnected.forEach((C) => C(f)), d.connected.length = 0, d.disconnected.length = 0, e.delete(f), !0; return d.disconnected.forEach((S) => S(u)), d.connected.length = 0, d.disconnected.length = 0, e.delete(u), !0;
} }
} }
} }
@ -241,25 +241,23 @@
}); });
// src/signals-lib.js // src/signals-lib.js
var l = Symbol.for("signal"); var l = Symbol.for("Signal");
function b(e) { function R(e) {
try { try {
return Reflect.has(e, l); return Reflect.has(e, l);
} catch { } catch {
return !1; return !1;
} }
} }
function R(e, t) { function m(e, t) {
if (typeof e != "function") if (typeof e != "function")
return M(e, t); return M(e, t);
if (b(e)) if (R(e))
return e; return e;
let n = M(); let n = M();
return $(() => n(e())), n; return F(() => n(e())), n;
} }
R.action = function(e, t, ...n) { m.action = function(e, t, ...n) {
if (!b(e))
throw new Error(`'${e}' is not a signal!`);
let r = e[l], { actions: o } = r; let r = e[l], { actions: o } = r;
if (!o || !Reflect.has(o, t)) if (!o || !Reflect.has(o, t))
throw new Error(`'${e}' has no action with name '${t}'!`); throw new Error(`'${e}' has no action with name '${t}'!`);
@ -267,32 +265,41 @@
return Reflect.deleteProperty(r, "skip"); return Reflect.deleteProperty(r, "skip");
r.listeners.forEach((a) => a(r.value)); r.listeners.forEach((a) => a(r.value));
}; };
R.on = function e(t, n, r) { m.on = function e(t, n, r = {}) {
if (Array.isArray(t)) let { signal: o } = r;
return t.forEach((o) => e(o, n, r)); if (!(o && o.aborted)) {
_(t, n), r && r.signal && r.signal.addEventListener("abort", () => F(t, n)); if (Array.isArray(t))
return t.forEach((a) => e(a, n, r));
_(t, n), o && o.addEventListener("abort", () => $(t, n));
}
}; };
R.clear = function(...e) { m.symbols = {
for (let t of e) signal: l,
t[l].listeners.clear(), Reflect.deleteProperty(t, l); onclear: Symbol.for("Signal.onclear")
};
m.clear = function(...e) {
for (let t of e) {
let n = t[l], { onclear: r } = m.symbols;
n.actions && n.actions[r] && n.actions[r].call(n), n.listeners.clear(), Reflect.deleteProperty(t, l);
}
}; };
var H = { var H = {
isReactiveAtrribute(e, t) { isReactiveAtrribute(e, t) {
return b(e); return R(e);
}, },
isTextContent(e) { isTextContent(e) {
return h(e) === "string" || b(e) && h(U(e)) === "string"; return h(e) === "string" || R(e) && h(U(e)) === "string";
}, },
processReactiveAttribute(e, t, n, r) { processReactiveAttribute(e, t, n, r) {
return _(n, (o) => r([t, o])), n(); return _(n, (o) => r([t, o])), n();
}, },
reactiveElement(e, t) { reactiveElement(e, t) {
let n = document.createComment("<> #reactive"), r = document.createComment("</> #reactive"), o = document.createDocumentFragment(); let n = document.createComment("<#reactive>"), r = document.createComment("</#reactive>"), o = document.createDocumentFragment();
o.append(n, r); o.append(n, r);
let a = (u) => { let a = (f) => {
if (!n.parentNode || !r.parentNode) if (!n.parentNode || !r.parentNode)
return F(e, a); return $(e, a);
let i = t(u); let i = t(f);
Array.isArray(i) || (i = [i]); Array.isArray(i) || (i = [i]);
let p = n; let p = n;
for (; (p = n.nextSibling) !== r; ) for (; (p = n.nextSibling) !== r; )
@ -319,7 +326,7 @@
}, Object.setPrototypeOf(e[l], W), e; }, Object.setPrototypeOf(e[l], W), e;
} }
var g = []; var g = [];
function $(e) { function F(e) {
let t = function() { let t = function() {
g.push(t), e(), g.pop(); g.push(t), e(), g.pop();
}; };
@ -347,25 +354,25 @@
function _(e, t) { function _(e, t) {
return e[l].listeners.add(t); return e[l].listeners.add(t);
} }
function F(e, t) { function $(e, t) {
return e[l].listeners.delete(t); return e[l].listeners.delete(t);
} }
// src/signals.js // src/signals.js
S(H); C(H);
globalThis.dde= { globalThis.dde= {
S: R, S: m,
assign: A, assign: A,
classListDeclartive: k, classListDeclartive: k,
createElement: ee, createElement: ee,
el: ee, el: ee,
empty: te, empty: te,
isSignal: b, isSignal: R,
namespace: Y, namespace: Y,
on: D, on: D,
registerReactivity: S, registerReactivity: C,
watch: $ watch: F
}; };
})(); })();

View File

@ -5,7 +5,7 @@ function h(e) {
} }
// src/signals-common.js // src/signals-common.js
var m = { var b = {
isReactiveAtrribute(e, t) { isReactiveAtrribute(e, t) {
return !1; return !1;
}, },
@ -19,11 +19,11 @@ var m = {
return null; return null;
} }
}; };
function S(e, t = !0) { function C(e, t = !0) {
return t ? Object.assign(m, e) : (Object.setPrototypeOf(e, m), e); return t ? Object.assign(b, e) : (Object.setPrototypeOf(e, b), e);
} }
function j(e) { function j(e) {
return m.isPrototypeOf(e) && e !== m ? e : m; return b.isPrototypeOf(e) && e !== b ? e : b;
} }
// src/dom.js // src/dom.js
@ -63,41 +63,41 @@ function A(e, ...t) {
if (!t.length) if (!t.length)
return e; return e;
let r = e instanceof SVGElement, o = (r ? z : P).bind(null, e, "Attribute"); let r = e instanceof SVGElement, o = (r ? z : P).bind(null, e, "Attribute");
return Object.entries(Object.assign({}, ...t)).forEach(function a([u, i]) { return Object.entries(Object.assign({}, ...t)).forEach(function a([f, i]) {
n.isReactiveAtrribute(i, u) && (i = n.processReactiveAttribute(e, u, i, a)); n.isReactiveAtrribute(i, f) && (i = n.processReactiveAttribute(e, f, i, a));
let [p] = u; let [p] = f;
if (p === "=") if (p === "=")
return o(u.slice(1), i); return o(f.slice(1), i);
if (p === ".") if (p === ".")
return L(e, u.slice(1), i); return L(e, f.slice(1), i);
if (typeof i == "object") if (typeof i == "object")
switch (u) { switch (f) {
case "style": case "style":
return w(i, P.bind(null, e.style, "Property")); return y(i, P.bind(null, e.style, "Property"));
case "dataset": case "dataset":
return w(i, L.bind(null, e.dataset)); return y(i, L.bind(null, e.dataset));
case "ariaset": case "ariaset":
return w(i, (E, v) => o("aria-" + E, v)); return y(i, (E, v) => o("aria-" + E, v));
case "classList": case "classList":
return k(e, i); return k(e, i);
default: default:
return Reflect.set(e, u, i); return Reflect.set(e, f, i);
} }
if (/(aria|data)([A-Z])/.test(u)) if (/(aria|data)([A-Z])/.test(f))
return u = u.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(), o(u, i); return f = f.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase(), o(f, i);
switch (u) { switch (f) {
case "xlink:href": case "xlink:href":
return o(u, i, "http://www.w3.org/1999/xlink"); return o(f, i, "http://www.w3.org/1999/xlink");
case "textContent": case "textContent":
if (!r) if (!r)
break; break;
return e.appendChild(document.createTextNode(i)); return e.appendChild(document.createTextNode(i));
} }
return q(e, u) ? L(e, u, i) : o(u, i); return q(e, f) ? L(e, f, i) : o(f, i);
}), e; }), e;
} }
function k(e, t) { function k(e, t) {
return typeof t != "object" || w( return typeof t != "object" || y(
t, t,
(n, r) => e.classList.toggle(n, r === -1 ? void 0 : !!r) (n, r) => e.classList.toggle(n, r === -1 ? void 0 : !!r)
), e; ), e;
@ -112,8 +112,8 @@ function q(e, t) {
let r = e.nodeName + "," + t; let r = e.nodeName + "," + t;
if (x.has(r)) if (x.has(r))
return x.get(r); return x.get(r);
let [o, a, u] = T(e, t), i = !N(o.set); let [o, a, f] = T(e, t), i = !N(o.set);
return (!i || a) && x.set(u === HTMLElement.prototype ? n : r, i), i; return (!i || a) && x.set(f === HTMLElement.prototype ? n : r, i), i;
} }
function T(e, t, n = 0) { function T(e, t, n = 0) {
if (e = Object.getPrototypeOf(e), !e) if (e = Object.getPrototypeOf(e), !e)
@ -121,7 +121,7 @@ function T(e, t, n = 0) {
let r = Object.getOwnPropertyDescriptor(e, t); let r = Object.getOwnPropertyDescriptor(e, t);
return r ? [r, n, e] : T(e, t, n + 1); return r ? [r, n, e] : T(e, t, n + 1);
} }
function w(e, t) { function y(e, t) {
return Object.entries(e).forEach(([n, r]) => t(n, r)); return Object.entries(e).forEach(([n, r]) => t(n, r));
} }
function N(e) { function N(e) {
@ -141,15 +141,15 @@ function L(e, t, n) {
function D(e, t, n) { function D(e, t, n) {
return (r) => (r.addEventListener(e, t, n), r); return (r) => (r.addEventListener(e, t, n), r);
} }
var y = I(); var w = I();
D.connected = function(e, t) { D.connected = function(e, t) {
return function(r) { return function(r) {
y.onConnected(r, e), t && t.signal && t.signal.addEventListener("abort", () => y.offConnected(r, e)); w.onConnected(r, e), t && t.signal && t.signal.addEventListener("abort", () => w.offConnected(r, e));
}; };
}; };
D.disconnected = function(e, t) { D.disconnected = function(e, t) {
return function(r) { return function(r) {
y.onDisconnected(r, e), t && t.signal && t.signal.addEventListener("abort", () => y.offDisconnected(r, e)); w.onDisconnected(r, e), t && t.signal && t.signal.addEventListener("abort", () => w.offDisconnected(r, e));
}; };
}; };
function I() { function I() {
@ -157,10 +157,10 @@ function I() {
for (let s of c) for (let s of c)
if (s.type === "childList") { if (s.type === "childList") {
if (E(s.addedNodes, !0)) { if (E(s.addedNodes, !0)) {
u(); f();
continue; continue;
} }
v(s.removedNodes, !0) && u(); v(s.removedNodes, !0) && f();
} }
}); });
return { return {
@ -170,8 +170,8 @@ function I() {
offConnected(c, s) { offConnected(c, s) {
if (!e.has(c)) if (!e.has(c))
return; return;
let f = e.get(c), d = f.connected; let u = e.get(c), d = u.connected;
d.splice(d.indexOf(s), 1), r(c, f); d.splice(d.indexOf(s), 1), r(c, u);
}, },
onDisconnected(c, s) { onDisconnected(c, s) {
a(), o(c).disconnected.push(s); a(), o(c).disconnected.push(s);
@ -179,12 +179,12 @@ function I() {
offDisconnected(c, s) { offDisconnected(c, s) {
if (!e.has(c)) if (!e.has(c))
return; return;
let f = e.get(c), d = f.disconnected; let u = e.get(c), d = u.disconnected;
d.splice(d.indexOf(s), 1), r(c, f); d.splice(d.indexOf(s), 1), r(c, u);
} }
}; };
function r(c, s) { function r(c, s) {
s.connected.length || s.disconnect.length || (e.delete(c), u()); s.connected.length || s.disconnect.length || (e.delete(c), f());
} }
function o(c) { function o(c) {
if (e.has(c)) if (e.has(c))
@ -195,7 +195,7 @@ function I() {
function a() { function a() {
t || (t = !0, n.observe(document.body, { childList: !0, subtree: !0 })); t || (t = !0, n.observe(document.body, { childList: !0, subtree: !0 }));
} }
function u() { function f() {
!t || e.size || (t = !1, n.disconnect()); !t || e.size || (t = !1, n.disconnect());
} }
function i() { function i() {
@ -208,24 +208,24 @@ function I() {
let s = []; let s = [];
if (!(c instanceof Node)) if (!(c instanceof Node))
return s; return s;
for (let f of e.keys()) for (let u of e.keys())
f === c || !(f instanceof Node) || c.contains(f) && s.push(f); u === c || !(u instanceof Node) || c.contains(u) && s.push(u);
return s; return s;
} }
function E(c, s) { function E(c, s) {
for (let f of c) { for (let u of c) {
if (s && p(f).then(E), !e.has(f)) if (s && p(u).then(E), !e.has(u))
return !1; return !1;
let d = e.get(f); let d = e.get(u);
return d.connected.forEach((C) => C(f)), d.connected.length = 0, d.disconnected.length || e.delete(f), !0; return d.connected.forEach((S) => S(u)), d.connected.length = 0, d.disconnected.length || e.delete(u), !0;
} }
} }
function v(c, s) { function v(c, s) {
for (let f of c) { for (let u of c) {
if (s && p(f).then(v), !e.has(f)) if (s && p(u).then(v), !e.has(u))
return !1; return !1;
let d = e.get(f); let d = e.get(u);
return d.disconnected.forEach((C) => C(f)), d.connected.length = 0, d.disconnected.length = 0, e.delete(f), !0; return d.disconnected.forEach((S) => S(u)), d.connected.length = 0, d.disconnected.length = 0, e.delete(u), !0;
} }
} }
} }
@ -239,25 +239,23 @@ function I() {
}); });
// src/signals-lib.js // src/signals-lib.js
var l = Symbol.for("signal"); var l = Symbol.for("Signal");
function b(e) { function R(e) {
try { try {
return Reflect.has(e, l); return Reflect.has(e, l);
} catch { } catch {
return !1; return !1;
} }
} }
function R(e, t) { function m(e, t) {
if (typeof e != "function") if (typeof e != "function")
return M(e, t); return M(e, t);
if (b(e)) if (R(e))
return e; return e;
let n = M(); let n = M();
return $(() => n(e())), n; return F(() => n(e())), n;
} }
R.action = function(e, t, ...n) { m.action = function(e, t, ...n) {
if (!b(e))
throw new Error(`'${e}' is not a signal!`);
let r = e[l], { actions: o } = r; let r = e[l], { actions: o } = r;
if (!o || !Reflect.has(o, t)) if (!o || !Reflect.has(o, t))
throw new Error(`'${e}' has no action with name '${t}'!`); throw new Error(`'${e}' has no action with name '${t}'!`);
@ -265,32 +263,41 @@ R.action = function(e, t, ...n) {
return Reflect.deleteProperty(r, "skip"); return Reflect.deleteProperty(r, "skip");
r.listeners.forEach((a) => a(r.value)); r.listeners.forEach((a) => a(r.value));
}; };
R.on = function e(t, n, r) { m.on = function e(t, n, r = {}) {
if (Array.isArray(t)) let { signal: o } = r;
return t.forEach((o) => e(o, n, r)); if (!(o && o.aborted)) {
_(t, n), r && r.signal && r.signal.addEventListener("abort", () => F(t, n)); if (Array.isArray(t))
return t.forEach((a) => e(a, n, r));
_(t, n), o && o.addEventListener("abort", () => $(t, n));
}
}; };
R.clear = function(...e) { m.symbols = {
for (let t of e) signal: l,
t[l].listeners.clear(), Reflect.deleteProperty(t, l); onclear: Symbol.for("Signal.onclear")
};
m.clear = function(...e) {
for (let t of e) {
let n = t[l], { onclear: r } = m.symbols;
n.actions && n.actions[r] && n.actions[r].call(n), n.listeners.clear(), Reflect.deleteProperty(t, l);
}
}; };
var H = { var H = {
isReactiveAtrribute(e, t) { isReactiveAtrribute(e, t) {
return b(e); return R(e);
}, },
isTextContent(e) { isTextContent(e) {
return h(e) === "string" || b(e) && h(U(e)) === "string"; return h(e) === "string" || R(e) && h(U(e)) === "string";
}, },
processReactiveAttribute(e, t, n, r) { processReactiveAttribute(e, t, n, r) {
return _(n, (o) => r([t, o])), n(); return _(n, (o) => r([t, o])), n();
}, },
reactiveElement(e, t) { reactiveElement(e, t) {
let n = document.createComment("<> #reactive"), r = document.createComment("</> #reactive"), o = document.createDocumentFragment(); let n = document.createComment("<#reactive>"), r = document.createComment("</#reactive>"), o = document.createDocumentFragment();
o.append(n, r); o.append(n, r);
let a = (u) => { let a = (f) => {
if (!n.parentNode || !r.parentNode) if (!n.parentNode || !r.parentNode)
return F(e, a); return $(e, a);
let i = t(u); let i = t(f);
Array.isArray(i) || (i = [i]); Array.isArray(i) || (i = [i]);
let p = n; let p = n;
for (; (p = n.nextSibling) !== r; ) for (; (p = n.nextSibling) !== r; )
@ -317,7 +324,7 @@ function Z(e, t, n) {
}, Object.setPrototypeOf(e[l], W), e; }, Object.setPrototypeOf(e[l], W), e;
} }
var g = []; var g = [];
function $(e) { function F(e) {
let t = function() { let t = function() {
g.push(t), e(), g.pop(); g.push(t), e(), g.pop();
}; };
@ -345,22 +352,22 @@ function U(e) {
function _(e, t) { function _(e, t) {
return e[l].listeners.add(t); return e[l].listeners.add(t);
} }
function F(e, t) { function $(e, t) {
return e[l].listeners.delete(t); return e[l].listeners.delete(t);
} }
// src/signals.js // src/signals.js
S(H); C(H);
export { export {
R as S, m as S,
A as assign, A as assign,
k as classListDeclartive, k as classListDeclartive,
ee as createElement, ee as createElement,
ee as el, ee as el,
te as empty, te as empty,
b as isSignal, R as isSignal,
Y as namespace, Y as namespace,
D as on, D as on,
S as registerReactivity, C as registerReactivity,
$ as watch F as watch
}; };

View File

@ -1,4 +1,4 @@
export const mark= Symbol.for("signal"); export const mark= Symbol.for("Signal");
export function isSignal(candidate){ export function isSignal(candidate){
try{ return Reflect.has(candidate, mark); } try{ return Reflect.has(candidate, mark); }
@ -12,10 +12,9 @@ export function S(value, actions){
const out= create(); const out= create();
watch(()=> out(value())); watch(()=> out(value()));
return out; return out;
//TODO is auto remove if used for args, if external listener needs also S.clear
} }
S.action= function(signal, name, ...a){ S.action= function(signal, name, ...a){
if(!isSignal(signal))
throw new Error(`'${signal}' is not a signal!`);
const s= signal[mark], { actions }= s; const s= signal[mark], { actions }= s;
if(!actions || !Reflect.has(actions, name)) if(!actions || !Reflect.has(actions, name))
throw new Error(`'${signal}' has no action with name '${name}'!`); throw new Error(`'${signal}' has no action with name '${name}'!`);
@ -23,16 +22,25 @@ S.action= function(signal, name, ...a){
if(s.skip) return Reflect.deleteProperty(s, "skip"); if(s.skip) return Reflect.deleteProperty(s, "skip");
s.listeners.forEach(l=> l(s.value)); s.listeners.forEach(l=> l(s.value));
}; };
S.on= function on(signals, listener, options){ S.on= function on(signals, listener, options= {}){
const { signal: as }= options;
if(as && as.aborted) return;
if(Array.isArray(signals)) return signals.forEach(s=> on(s, listener, options)); if(Array.isArray(signals)) return signals.forEach(s=> on(s, listener, options));
addSignalListener(signals, listener); addSignalListener(signals, listener);
if(options && options.signal) if(as) as.addEventListener("abort", ()=> removeSignalListener(signals, listener));
options.signal.addEventListener("abort", ()=> removeSignalListener(signals, listener));
//TODO cleanup when signal removed (also TODO) //TODO cleanup when signal removed (also TODO)
}; };
S.symbols= {
signal: mark,
onclear: Symbol.for("Signal.onclear")
};
S.clear= function(...signals){ S.clear= function(...signals){
for(const signal of signals){ for(const signal of signals){
signal[mark].listeners.clear(); const s= signal[mark];
const { onclear }= S.symbols;
if(s.actions && s.actions[onclear])
s.actions[onclear].call(s);
s.listeners.clear();
Reflect.deleteProperty(signal, mark); Reflect.deleteProperty(signal, mark);
} }
}; };
@ -48,13 +56,13 @@ export const signals_config= {
return attrS(); return attrS();
}, },
reactiveElement(signal, map){ reactiveElement(signal, map){
const mark_start= document.createComment("<> #reactive"); const mark_start= document.createComment("<#reactive>");
const mark_end= document.createComment("</> #reactive"); const mark_end= document.createComment("</#reactive>");
const out= document.createDocumentFragment(); const out= document.createDocumentFragment();
out.append(mark_start, mark_end); out.append(mark_start, mark_end);
const toEls= v=> { const reRenderReactiveElement= v=> {
if(!mark_start.parentNode || !mark_end.parentNode) if(!mark_start.parentNode || !mark_end.parentNode)
return removeSignalListener(signal, toEls); return removeSignalListener(signal, reRenderReactiveElement);
let els= map(v); let els= map(v);
if(!Array.isArray(els)) if(!Array.isArray(els))
els= [ els ]; els= [ els ];
@ -63,8 +71,8 @@ export const signals_config= {
el_r.remove(); el_r.remove();
mark_start.after(...els); mark_start.after(...els);
}; };
addSignalListener(signal, toEls); addSignalListener(signal, reRenderReactiveElement);
toEls(signal()); reRenderReactiveElement(signal());
return out; return out;
} }
}; };

32
src/signals.d.ts vendored
View File

@ -1,8 +1,34 @@
export type Signal<V, A>= (set?: V)=> V & A; export type Signal<V, A>= (set?: V)=> V & A;
type Action<V>= (this: { value: V }, ...a: any[])=> typeof S._ | void; type Action<V>= (this: { value: V }, ...a: any[])=> typeof S._ | void;
type SymbolOnclear= Symbol;
type SymbolSignal= Symbol;
type Actions<V>= Record<string, Action<V>>; type Actions<V>= Record<string, Action<V>>;
interface S { interface S {
_: Symbol _: Symbol
/**
* Simple example:
* ```js
* const hello= S("Hello Signal");
* ```
* simple todo signal:
* ```js
* const todos= S([], {
* add(v){ this.value.push(S(v)); },
* remove(i){ this.value.splice(i, 1); },
* [S.symbols.onclear](){ S.clear(...this.value); },
* });
* ```
* computed signal:
* ```js
* const name= S("Jan");
* const surname= S("Andrle");
* const fullname= S(()=> name()+" "+surname());
* ```
* @param value Initial signal value. Or function computing value from other signals.
* @param actions Use to define actions on the signal. Such as add item to the array.
* There is also a reserved function `S.symbol.onclear` which is called when the signal is cleared
* by `S.clear`.
* */
<V, A extends Actions<V>>(value: V, actions?: A): Signal<V, A>; <V, A extends Actions<V>>(value: V, actions?: A): Signal<V, A>;
action<S extends Signal<any, Actions<any>>, A extends (S extends Signal<any, infer A> ? A : never), N extends keyof A>( action<S extends Signal<any, Actions<any>>, A extends (S extends Signal<any, infer A> ? A : never), N extends keyof A>(
signal: S, signal: S,
@ -10,6 +36,10 @@ interface S {
...params: A[N] extends (...args: infer P)=> any ? P : never ...params: A[N] extends (...args: infer P)=> any ? P : never
): void; ): void;
clear(...signals: Signal<any, any>): void; clear(...signals: Signal<any, any>): void;
on<T>(signal: Signal<T, any>, onchange: (a: T)=> void, options?: AddEventListenerOptions): void on<T>(signal: Signal<T, any>, onchange: (a: T)=> void, options?: AddEventListenerOptions): void;
symbols: {
signal: SymbolSignal;
onclear: SymbolOnclear;
}
} }
export const S: S; export const S: S;

View File

@ -12,13 +12,15 @@ const className= style.host(todosComponent).css`
} }
`; `;
/** @param {{ todos: string[] }} */ /** @param {{ todos: string[] }} */
export function todosComponent({ todos= [] }= {}){ export function todosComponent({ todos= [ "A" ] }= {}){
const todosS= S(todos.map(v=> S(v)), { const todosS= S([], {
/** @param {string} v */ /** @param {string} v */
add(v){ this.value.push(S(v)); }, add(v){ this.value.push(S(v)); },
/** @param {number} i */ /** @param {number} i */
remove(i){ this.value.splice(i, 1); } remove(i){ this.value.splice(i, 1); },
[S.symbols.onclear](){ S.clear(...this.value); },
}); });
todos.forEach(v=> S.action(todosS, "add", v));
console.log(todosS); //TODO console.log(todosS); //TODO
const name= "todoName"; const name= "todoName";
const onsubmitAdd= on("submit", event=> { const onsubmitAdd= on("submit", event=> {