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
*/
const 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 ensureNode = (n) => n?._isRuntime ? n.container : (n instanceof Node ? n : doc.createTextNode(String(n ?? "")));
// 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 ensureNode = (n) => n?._isRuntime ? n.container : (n instanceof Node ? n : doc.createTextNode(String(n ?? "")));
// --- STATE & SYSTEM VARIABLES ---
let activeEffect = null, currentOwner = null, currentCtx = null, isFlushing = false;
const effectQueue = new Set(), MOUNTED_NODES = new WeakMap();
// --- STATE & SYSTEM VARIABLES ---
let activeEffect = null, currentOwner = null, currentCtx = null, isFlushing = false;
const effectQueue = new Set(), MOUNTED_NODES = new WeakMap();
const runCleanups = (s) => { s?.forEach(f => f()); s?.clear(); };
const clearDeps = (e) => { e._deps.forEach(d => d.delete(e)); e._deps.clear(); };
const onUnmount = (fn) => currentOwner && currentOwner.cleanups.add(fn);
const runCleanups = (s) => { s?.forEach(f => f()); s?.clear(); };
const clearDeps = (e) => { e._deps.forEach(d => d.delete(e)); e._deps.clear(); };
const onUnmount = (fn) => currentOwner && currentOwner.cleanups.add(fn);
const cleanupNode = (node) => {
const cleanupNode = (node) => {
if (node._cleanups) runCleanups(node._cleanups);
node.childNodes?.forEach(cleanupNode);
};
};
const runWithContext = (e, cb) => {
const runWithContext = (e, cb) => {
const p = activeEffect; activeEffect = e;
try { return cb(); } finally { activeEffect = p; }
};
};
const flush = () => {
const flush = () => {
if (isFlushing) return; isFlushing = true;
const sorted = Array.from(effectQueue).sort((a, b) => (a.depth || 0) - (b.depth || 0));
effectQueue.clear();
sorted.forEach(e => !e._deleted && e());
isFlushing = false;
};
};
const trackUpdate = (subs, trigger = false) => {
const trackUpdate = (subs, trigger = false) => {
if (!trigger && activeEffect && !activeEffect._deleted) {
subs.add(activeEffect); activeEffect._deps.add(subs);
} else if (trigger) {
@@ -43,10 +40,10 @@ const SigPro = (() => {
});
if (!isFlushing) queueMicrotask(flush);
}
};
};
// --- THE ATOM (Internal Kernel) ---
const _sub = (fn, cb) => {
// --- THE ATOM (Internal Kernel) ---
const _sub = (fn, cb) => {
const e = () => {
if (e._deleted) return;
if (cb) {
@@ -61,28 +58,28 @@ const SigPro = (() => {
e();
onUnmount(() => { e._deleted = true; clearDeps(e); });
return e;
};
};
// --- PUBLIC API ---
const ignore = (fn) => {
// --- PUBLIC API ---
const ignore = (fn) => {
const p = activeEffect;
activeEffect = null;
try { return fn(); }
finally { activeEffect = p; }
};
};
const Watch = (target, cb) => {
const Watch = (target, cb) => {
const isA = isArr(target);
return _sub(
isA ? () => target.forEach(s => isFunc(s) ? s() : s) : target,
isA ? () => ignore(cb) : null
);
};
};
const Share = (k, v) => currentCtx && (currentCtx.p[k] = v);
const Use = (k, dft) => (currentCtx && k in currentCtx.p) ? currentCtx.p[k] : dft;
const Share = (k, v) => currentCtx && (currentCtx.p[k] = v);
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();
if (isFunc(val)) {
let cache, dirty = true;
@@ -103,9 +100,9 @@ const SigPro = (() => {
}
trackUpdate(subs); return val;
};
};
};
const $$ = (obj, cache = new WeakMap()) => {
const $$ = (obj, cache = new WeakMap()) => {
if (!isObj(obj)) return obj;
if (cache.has(obj)) return cache.get(obj);
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; }
});
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 = {}; }
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);
@@ -151,9 +148,9 @@ const SigPro = (() => {
} else el.appendChild(ensureNode(c));
};
append(children); return el;
};
};
const Render = (fn) => {
const Render = (fn) => {
const cleanups = new Set(), prevOwner = currentOwner, prevCtx = currentCtx;
currentCtx = { p: { ...(prevCtx?.p || {}) } };
const container = doc.createElement("div");
@@ -163,9 +160,9 @@ const SigPro = (() => {
(isArr(res) ? res : [res]).forEach(r => container.appendChild(ensureNode(r)));
currentCtx = prevCtx; currentOwner = prevOwner;
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" });
let view = null, last = null;
_sub(() => !!(isFunc(cond) ? cond() : cond), (s) => {
@@ -180,9 +177,9 @@ const SigPro = (() => {
}
});
return root;
};
};
const For = (src, itemFn, keyFn) => {
const For = (src, itemFn, keyFn) => {
const m = doc.createTextNode(""), root = Tag("div", { style: "display:contents" }, [m]);
let cache = new Map();
_sub(() => (isFunc(src) ? src() : src) || [], (items) => {
@@ -202,9 +199,9 @@ const SigPro = (() => {
cache = next;
});
return root;
};
};
const Router = (routes) => {
const Router = (routes) => {
const getHash = () => window.location.hash.replace(/^#/, "") || "/";
const path = $(getHash());
window.addEventListener("hashchange", () => path(getHash()));
@@ -229,26 +226,27 @@ const SigPro = (() => {
}
});
return outlet;
};
};
Router.params = $({});
Router.to = (p) => window.location.hash = p.replace(/^#?\/?/, "#/");
Router.back = () => window.history.back();
Router.path = () => window.location.hash.replace(/^#/, "") || "/";
Router.params = $({});
Router.to = (p) => window.location.hash = p.replace(/^#?\/?/, "#/");
Router.back = () => window.history.back();
Router.path = () => window.location.hash.replace(/^#/, "") || "/";
const Mount = (comp, target) => {
const Mount = (comp, target) => {
const t = typeof target === "string" ? doc.querySelector(target) : target;
if (!t) return; if (MOUNTED_NODES.has(t)) MOUNTED_NODES.get(t).destroy();
const inst = Render(isFunc(comp) ? comp : () => comp);
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") {
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"
.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;