quito IIFE

This commit is contained in:
2026-04-08 15:07:20 +02:00
parent d9cbbae40d
commit d586af52b6

View File

@@ -1,38 +1,35 @@
/** // sigpro
* SigPro const doc = typeof document !== "undefined" ? document : null;
*/ const isArr = Array.isArray, assign = Object.assign, isFunc = (f) => typeof f === "function", isObj = (o) => typeof o === "object" && o !== null;
const SigPro = (() => { const ensureNode = (n) => n?._isRuntime ? n.container : (n instanceof Node ? n : doc.createTextNode(String(n ?? "")));
const doc = typeof document !== "undefined" ? document : null;
const isArr = Array.isArray, assign = Object.assign, isFunc = (f) => typeof f === "function", isObj = (o) => typeof o === "object" && o !== null;
const ensureNode = (n) => n?._isRuntime ? n.container : (n instanceof Node ? n : doc.createTextNode(String(n ?? "")));
// --- STATE & SYSTEM VARIABLES --- // --- STATE & SYSTEM VARIABLES ---
let activeEffect = null, currentOwner = null, currentCtx = null, isFlushing = false; let activeEffect = null, currentOwner = null, currentCtx = null, isFlushing = false;
const effectQueue = new Set(), MOUNTED_NODES = new WeakMap(); const effectQueue = new Set(), MOUNTED_NODES = new WeakMap();
const runCleanups = (s) => { s?.forEach(f => f()); s?.clear(); }; const runCleanups = (s) => { s?.forEach(f => f()); s?.clear(); };
const clearDeps = (e) => { e._deps.forEach(d => d.delete(e)); e._deps.clear(); }; const clearDeps = (e) => { e._deps.forEach(d => d.delete(e)); e._deps.clear(); };
const onUnmount = (fn) => currentOwner && currentOwner.cleanups.add(fn); const onUnmount = (fn) => currentOwner && currentOwner.cleanups.add(fn);
const cleanupNode = (node) => { const cleanupNode = (node) => {
if (node._cleanups) runCleanups(node._cleanups); if (node._cleanups) runCleanups(node._cleanups);
node.childNodes?.forEach(cleanupNode); node.childNodes?.forEach(cleanupNode);
}; };
const runWithContext = (e, cb) => { const runWithContext = (e, cb) => {
const p = activeEffect; activeEffect = e; const p = activeEffect; activeEffect = e;
try { return cb(); } finally { activeEffect = p; } try { return cb(); } finally { activeEffect = p; }
}; };
const flush = () => { const flush = () => {
if (isFlushing) return; isFlushing = true; if (isFlushing) return; isFlushing = true;
const sorted = Array.from(effectQueue).sort((a, b) => (a.depth || 0) - (b.depth || 0)); const sorted = Array.from(effectQueue).sort((a, b) => (a.depth || 0) - (b.depth || 0));
effectQueue.clear(); effectQueue.clear();
sorted.forEach(e => !e._deleted && e()); sorted.forEach(e => !e._deleted && e());
isFlushing = false; isFlushing = false;
}; };
const trackUpdate = (subs, trigger = false) => { const trackUpdate = (subs, trigger = false) => {
if (!trigger && activeEffect && !activeEffect._deleted) { if (!trigger && activeEffect && !activeEffect._deleted) {
subs.add(activeEffect); activeEffect._deps.add(subs); subs.add(activeEffect); activeEffect._deps.add(subs);
} else if (trigger) { } else if (trigger) {
@@ -43,10 +40,10 @@ const SigPro = (() => {
}); });
if (!isFlushing) queueMicrotask(flush); if (!isFlushing) queueMicrotask(flush);
} }
}; };
// --- THE ATOM (Internal Kernel) --- // --- THE ATOM (Internal Kernel) ---
const _sub = (fn, cb) => { const _sub = (fn, cb) => {
const e = () => { const e = () => {
if (e._deleted) return; if (e._deleted) return;
if (cb) { if (cb) {
@@ -61,28 +58,28 @@ const SigPro = (() => {
e(); e();
onUnmount(() => { e._deleted = true; clearDeps(e); }); onUnmount(() => { e._deleted = true; clearDeps(e); });
return e; return e;
}; };
// --- PUBLIC API --- // --- PUBLIC API ---
const ignore = (fn) => { const ignore = (fn) => {
const p = activeEffect; const p = activeEffect;
activeEffect = null; activeEffect = null;
try { return fn(); } try { return fn(); }
finally { activeEffect = p; } finally { activeEffect = p; }
}; };
const Watch = (target, cb) => { const Watch = (target, cb) => {
const isA = isArr(target); const isA = isArr(target);
return _sub( return _sub(
isA ? () => target.forEach(s => isFunc(s) ? s() : s) : target, isA ? () => target.forEach(s => isFunc(s) ? s() : s) : target,
isA ? () => ignore(cb) : null isA ? () => ignore(cb) : null
); );
}; };
const Share = (k, v) => currentCtx && (currentCtx.p[k] = v); const Share = (k, v) => currentCtx && (currentCtx.p[k] = v);
const Use = (k, dft) => (currentCtx && k in currentCtx.p) ? currentCtx.p[k] : dft; const Use = (k, dft) => (currentCtx && k in currentCtx.p) ? currentCtx.p[k] : dft;
const $ = (val, key = null) => { const $ = (val, key = null) => {
const subs = new Set(); const subs = new Set();
if (isFunc(val)) { if (isFunc(val)) {
let cache, dirty = true; let cache, dirty = true;
@@ -103,9 +100,9 @@ const SigPro = (() => {
} }
trackUpdate(subs); return val; trackUpdate(subs); return val;
}; };
}; };
const $$ = (obj, cache = new WeakMap()) => { const $$ = (obj, cache = new WeakMap()) => {
if (!isObj(obj)) return obj; if (!isObj(obj)) return obj;
if (cache.has(obj)) return cache.get(obj); if (cache.has(obj)) return cache.get(obj);
const subs = {}; const subs = {};
@@ -114,9 +111,9 @@ const SigPro = (() => {
set: (t, k, v) => { if (!Object.is(t[k], v)) { t[k] = v; if (subs[k]) trackUpdate(subs[k], true); } return true; } set: (t, k, v) => { if (!Object.is(t[k], v)) { t[k] = v; if (subs[k]) trackUpdate(subs[k], true); } return true; }
}); });
cache.set(obj, proxy); return proxy; cache.set(obj, proxy); return proxy;
}; };
const Tag = (tag, props = {}, children = []) => { const Tag = (tag, props = {}, children = []) => {
if (props instanceof Node || isArr(props) || !isObj(props)) { children = props; props = {}; } if (props instanceof Node || isArr(props) || !isObj(props)) { children = props; props = {}; }
const isSVG = /^(svg|path|circle|rect|line|polyline|polygon|g|defs|text|tspan|use)$/.test(tag); const isSVG = /^(svg|path|circle|rect|line|polyline|polygon|g|defs|text|tspan|use)$/.test(tag);
const el = isSVG ? doc.createElementNS("http://www.w3.org/2000/svg", tag) : doc.createElement(tag); const el = isSVG ? doc.createElementNS("http://www.w3.org/2000/svg", tag) : doc.createElement(tag);
@@ -151,9 +148,9 @@ const SigPro = (() => {
} else el.appendChild(ensureNode(c)); } else el.appendChild(ensureNode(c));
}; };
append(children); return el; append(children); return el;
}; };
const Render = (fn) => { const Render = (fn) => {
const cleanups = new Set(), prevOwner = currentOwner, prevCtx = currentCtx; const cleanups = new Set(), prevOwner = currentOwner, prevCtx = currentCtx;
currentCtx = { p: { ...(prevCtx?.p || {}) } }; currentCtx = { p: { ...(prevCtx?.p || {}) } };
const container = doc.createElement("div"); const container = doc.createElement("div");
@@ -163,9 +160,9 @@ const SigPro = (() => {
(isArr(res) ? res : [res]).forEach(r => container.appendChild(ensureNode(r))); (isArr(res) ? res : [res]).forEach(r => container.appendChild(ensureNode(r)));
currentCtx = prevCtx; currentOwner = prevOwner; currentCtx = prevCtx; currentOwner = prevOwner;
return { _isRuntime: true, container, destroy: () => { runCleanups(cleanups); cleanupNode(container); container.remove(); } }; return { _isRuntime: true, container, destroy: () => { runCleanups(cleanups); cleanupNode(container); container.remove(); } };
}; };
const If = (cond, t, f = null, trans = null) => { const If = (cond, t, f = null, trans = null) => {
const root = Tag("div", { style: "display:contents" }); const root = Tag("div", { style: "display:contents" });
let view = null, last = null; let view = null, last = null;
_sub(() => !!(isFunc(cond) ? cond() : cond), (s) => { _sub(() => !!(isFunc(cond) ? cond() : cond), (s) => {
@@ -180,9 +177,9 @@ const SigPro = (() => {
} }
}); });
return root; return root;
}; };
const For = (src, itemFn, keyFn) => { const For = (src, itemFn, keyFn) => {
const m = doc.createTextNode(""), root = Tag("div", { style: "display:contents" }, [m]); const m = doc.createTextNode(""), root = Tag("div", { style: "display:contents" }, [m]);
let cache = new Map(); let cache = new Map();
_sub(() => (isFunc(src) ? src() : src) || [], (items) => { _sub(() => (isFunc(src) ? src() : src) || [], (items) => {
@@ -202,9 +199,9 @@ const SigPro = (() => {
cache = next; cache = next;
}); });
return root; return root;
}; };
const Router = (routes) => { const Router = (routes) => {
const getHash = () => window.location.hash.replace(/^#/, "") || "/"; const getHash = () => window.location.hash.replace(/^#/, "") || "/";
const path = $(getHash()); const path = $(getHash());
window.addEventListener("hashchange", () => path(getHash())); window.addEventListener("hashchange", () => path(getHash()));
@@ -229,26 +226,27 @@ const SigPro = (() => {
} }
}); });
return outlet; return outlet;
}; };
Router.params = $({}); Router.params = $({});
Router.to = (p) => window.location.hash = p.replace(/^#?\/?/, "#/"); Router.to = (p) => window.location.hash = p.replace(/^#?\/?/, "#/");
Router.back = () => window.history.back(); Router.back = () => window.history.back();
Router.path = () => window.location.hash.replace(/^#/, "") || "/"; Router.path = () => window.location.hash.replace(/^#/, "") || "/";
const Mount = (comp, target) => { const Mount = (comp, target) => {
const t = typeof target === "string" ? doc.querySelector(target) : target; const t = typeof target === "string" ? doc.querySelector(target) : target;
if (!t) return; if (MOUNTED_NODES.has(t)) MOUNTED_NODES.get(t).destroy(); if (!t) return; if (MOUNTED_NODES.has(t)) MOUNTED_NODES.get(t).destroy();
const inst = Render(isFunc(comp) ? comp : () => comp); const inst = Render(isFunc(comp) ? comp : () => comp);
t.replaceChildren(inst.container); MOUNTED_NODES.set(t, inst); return inst; t.replaceChildren(inst.container); MOUNTED_NODES.set(t, inst); return inst;
}; };
return { $, $$, Share, Use, Watch, Tag, Render, If, For, Router, Mount, ignore, onUnmount }; const SigPro = Object.freeze({ $, $$, Share, Use, Watch, Tag, Render, If, For, Router, Mount, ignore, onUnmount });
})();
if (typeof window !== "undefined") { if (typeof window !== "undefined") {
Object.assign(window, SigPro); Object.assign(window, SigPro);
"div span p h1 h2 h3 h4 h5 h6 br hr section article aside nav main header footer ul ol li a em strong pre code form label input textarea select button img svg" "div span p h1 h2 h3 h4 h5 h6 br hr section article aside nav main header footer ul ol li a em strong pre code form label input textarea select button img svg"
.split(" ").forEach(t => window[t[0].toUpperCase() + t.slice(1)] = (p, c) => SigPro.Tag(t, p, c)); .split(" ").forEach(t => window[t[0].toUpperCase() + t.slice(1)] = (p, c) => SigPro.Tag(t, p, c));
} }
export { $, $$, Share, Use, Watch, Tag, Render, If, For, Router, Mount, ignore, onUnmount };
export default SigPro; export default SigPro;