diff --git a/dist/sigpro.esm.js b/dist/sigpro.esm.js index e1a2cbf..5c3d07c 100644 --- a/dist/sigpro.esm.js +++ b/dist/sigpro.esm.js @@ -1,73 +1,399 @@ // sigpro.js -var activeEffect = null; -var currentOwner = null; -var effectQueue = new Set; -var isFlushing = false; -var MOUNTED_NODES = new WeakMap; -var doc = document; -var isArr = Array.isArray; -var assign = Object.assign; -var createEl = (t) => doc.createElement(t); -var createText = (t) => doc.createTextNode(String(t ?? "")); var isFunc = (f) => typeof f === "function"; -var isObj = (o) => typeof o === "object" && o !== null; -var runWithContext = (effect, callback) => { - const previousEffect = activeEffect; - activeEffect = effect; +var isObj = (o) => o && typeof o === "object"; +var isArr = Array.isArray; +var doc = typeof document !== "undefined" ? document : null; +var ensureNode = (n) => n?._isRuntime ? n.container : n instanceof Node ? n : doc.createTextNode(n == null ? "" : String(n)); +var activeEffect = null; +var activeOwner = null; +var isFlushing = false; +var batchDepth = 0; +var effectQueue = new Set; +var proxyCache = new WeakMap; +var ITER = Symbol("iter"); +var MOUNTED_NODES = new WeakMap; +var dispose = (eff) => { + if (!eff || eff._disposed) + return; + eff._disposed = true; + const stack = [eff]; + while (stack.length) { + const e = stack.pop(); + if (e._cleanups) { + e._cleanups.forEach((fn) => fn()); + e._cleanups.clear(); + } + if (e._children) { + e._children.forEach((child) => stack.push(child)); + e._children.clear(); + } + if (e._deps) { + e._deps.forEach((depSet) => depSet.delete(e)); + e._deps.clear(); + } + } +}; +var onMount = (fn) => { + if (activeOwner) + (activeOwner._mounts ||= []).push(fn); +}; +var onUnmount = (fn) => { + if (activeOwner) + (activeOwner._cleanups ||= new Set).add(fn); +}; +var untrack = (fn) => { + const p = activeEffect; + activeEffect = null; try { - return callback(); + return fn(); } finally { - activeEffect = previousEffect; + activeEffect = p; } }; -var cleanupNode = (node) => { - if (node._cleanups) { - node._cleanups.forEach((dispose) => dispose()); - node._cleanups.clear(); - } - node.childNodes?.forEach(cleanupNode); +var createEffect = (fn, isComputed = false) => { + const effect = () => { + if (effect._disposed) + return; + if (effect._deps) + effect._deps.forEach((s) => s.delete(effect)); + if (effect._cleanups) { + effect._cleanups.forEach((c) => c()); + effect._cleanups.clear(); + } + const prevEffect = activeEffect; + const prevOwner = activeOwner; + activeEffect = activeOwner = effect; + try { + return effect._result = fn(); + } catch (e) { + console.error("[SigPro]", e); + } finally { + activeEffect = prevEffect; + activeOwner = prevOwner; + } + }; + effect._deps = effect._cleanups = effect._children = null; + effect._disposed = false; + effect._isComputed = isComputed; + effect._depth = activeEffect ? activeEffect._depth + 1 : 0; + effect._mounts = []; + effect._parent = activeOwner; + if (activeOwner) + (activeOwner._children ||= new Set).add(effect); + return effect; }; -var flushEffects = () => { +var flush = () => { if (isFlushing) return; isFlushing = true; - while (effectQueue.size > 0) { - const sortedEffects = Array.from(effectQueue).sort((a, b) => (a.depth || 0) - (b.depth || 0)); - effectQueue.clear(); - for (const effect of sortedEffects) { - if (!effect._deleted) - effect(); - } - } + const sorted = Array.from(effectQueue).sort((a, b) => a._depth - b._depth); + effectQueue.clear(); + for (const e of sorted) + if (!e._disposed) + e(); isFlushing = false; }; -var trackSubscription = (subscribers) => { - if (activeEffect && !activeEffect._deleted) { - subscribers.add(activeEffect); - activeEffect._deps.add(subscribers); +var Batch = (fn) => { + batchDepth++; + try { + return fn(); + } finally { + batchDepth--; + if (batchDepth === 0 && effectQueue.size > 0 && !isFlushing) { + flush(); + } } }; -var triggerUpdate = (subscribers) => { - subscribers.forEach((effect) => { - if (effect === activeEffect || effect._deleted) - return; - if (effect._isComputed) { - effect.markDirty(); - if (effect._subs) - triggerUpdate(effect._subs); - } else { - effectQueue.add(effect); +var trackUpdate = (subs, trigger = false) => { + if (!trigger && activeEffect && !activeEffect._disposed) { + subs.add(activeEffect); + (activeEffect._deps ||= new Set).add(subs); + } else if (trigger) { + let hasQueue = false; + subs.forEach((e) => { + if (e === activeEffect || e._disposed) + return; + if (e._isComputed) { + e._dirty = true; + if (e._subs) + trackUpdate(e._subs, true); + } else { + effectQueue.add(e); + hasQueue = true; + } + }); + if (hasQueue && !isFlushing && batchDepth === 0) + queueMicrotask(flush); + } +}; +var $ = (val, key = null) => { + const subs = new Set; + if (isFunc(val)) { + let cache, dirty = true; + const computed = () => { + if (dirty) { + const prev = activeEffect; + activeEffect = computed; + try { + const next = val(); + if (!Object.is(cache, next)) { + cache = next; + dirty = false; + trackUpdate(subs, true); + } + } finally { + activeEffect = prev; + } + } + trackUpdate(subs); + return cache; + }; + computed._isComputed = true; + computed._subs = subs; + computed._dirty = true; + computed._deps = null; + computed._disposed = false; + computed.markDirty = () => { + dirty = true; + }; + computed.stop = () => { + computed._disposed = true; + if (computed._deps) { + computed._deps.forEach((depSet) => depSet.delete(computed)); + computed._deps.clear(); + } + subs.clear(); + }; + if (activeOwner) + onUnmount(computed.stop); + return computed; + } + if (key) + try { + val = JSON.parse(localStorage.getItem(key)) ?? val; + } catch (e) {} + return (...args) => { + if (args.length) { + const next = isFunc(args[0]) ? args[0](val) : args[0]; + if (!Object.is(val, next)) { + val = next; + if (key) + localStorage.setItem(key, JSON.stringify(val)); + trackUpdate(subs, true); + } + } + trackUpdate(subs); + return val; + }; +}; +var $$ = (target) => { + if (!isObj(target)) + return target; + if (proxyCache.has(target)) + return proxyCache.get(target); + const subsMap = new Map; + const getSubs = (k) => { + let s = subsMap.get(k); + if (!s) + subsMap.set(k, s = new Set); + return s; + }; + const proxy = new Proxy(target, { + get(t, k) { + trackUpdate(getSubs(k)); + return $$(t[k]); + }, + set(t, k, v) { + const isNew = !(k in t); + if (!Object.is(t[k], v)) { + t[k] = v; + trackUpdate(getSubs(k), true); + if (isNew) + trackUpdate(getSubs(ITER), true); + } + return true; + }, + deleteProperty(t, k) { + const res = Reflect.deleteProperty(t, k); + if (res) { + trackUpdate(getSubs(k), true); + trackUpdate(getSubs(ITER), true); + } + return res; + }, + ownKeys(t) { + trackUpdate(getSubs(ITER)); + return Reflect.ownKeys(t); } }); - if (!isFlushing) - queueMicrotask(flushEffects); + proxyCache.set(target, proxy); + return proxy; +}; +var Watch = (sources, cb) => { + if (cb === undefined) { + const effect2 = createEffect(sources); + effect2(); + return () => dispose(effect2); + } + const effect = createEffect(() => { + const vals = Array.isArray(sources) ? sources.map((s) => s()) : sources(); + untrack(() => cb(vals)); + }); + effect(); + return () => dispose(effect); +}; +var cleanupNode = (node) => { + if (node._cleanups) { + node._cleanups.forEach((fn) => fn()); + node._cleanups.clear(); + } + if (node._ownerEffect) + dispose(node._ownerEffect); + if (node.childNodes) + node.childNodes.forEach(cleanupNode); +}; +var DANGEROUS_PROTOCOL = /^\s*(javascript|data|vbscript):/i; +var isDangerousAttr = (key) => key === "src" || key === "href" || key.startsWith("on"); +var validateAttr = (key, val) => { + if (val == null || val === false) + return null; + if (isDangerousAttr(key)) { + const sVal = String(val); + if (DANGEROUS_PROTOCOL.test(sVal)) { + console.warn(`[SigPro] Bloqueado protocolo peligroso en ${key}`); + return "#"; + } + } + return val; +}; +var Tag = (tag, props = {}, children = []) => { + if (props instanceof Node || isArr(props) || !isObj(props)) { + children = props; + props = {}; + } + if (isFunc(tag)) { + const ctx = { _mounts: [], _cleanups: new Set }; + const effect = createEffect(() => { + const result2 = tag(props, { + children, + emit: (ev, ...args) => props[`on${ev[0].toUpperCase()}${ev.slice(1)}`]?.(...args) + }); + effect._result = result2; + return result2; + }); + effect(); + const result = effect._result; + if (result == null) + return null; + const node = result instanceof Node || isArr(result) && result.every((n) => n instanceof Node) ? result : doc.createTextNode(String(result)); + const attach = (n) => { + if (isObj(n) && !n._isRuntime) { + n._mounts = effect._mounts || []; + n._cleanups = effect._cleanups || new Set; + n._ownerEffect = effect; + } + }; + isArr(node) ? node.forEach(attach) : attach(node); + return node; + } + 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); + el._cleanups = new Set; + for (let k in props) { + if (!props.hasOwnProperty(k)) + continue; + let v = props[k]; + if (k === "ref") { + isFunc(v) ? v(el) : v.current = el; + continue; + } + if (k.startsWith("on")) { + const ev = k.slice(2).toLowerCase(); + el.addEventListener(ev, v); + const off = () => el.removeEventListener(ev, v); + el._cleanups.add(off); + onUnmount(off); + } else if (isFunc(v)) { + const effect = createEffect(() => { + const val = validateAttr(k, v()); + if (k === "class") + el.className = val || ""; + else if (val == null) + el.removeAttribute(k); + else if (k in el && !isSVG) + el[k] = val; + else + el.setAttribute(k, val === true ? "" : val); + }); + effect(); + el._cleanups.add(() => dispose(effect)); + onUnmount(() => dispose(effect)); + if (/^(INPUT|TEXTAREA|SELECT)$/.test(el.tagName) && (k === "value" || k === "checked")) { + const evType = k === "checked" ? "change" : "input"; + el.addEventListener(evType, (ev) => v(ev.target[k])); + } + } else { + const val = validateAttr(k, v); + if (val != null) { + if (k in el && !isSVG) + el[k] = val; + else + el.setAttribute(k, val === true ? "" : val); + } + } + } + const append = (c) => { + if (isArr(c)) + return c.forEach(append); + if (isFunc(c)) { + const anchor = doc.createTextNode(""); + el.appendChild(anchor); + let currentNodes = []; + const effect = createEffect(() => { + const res = c(); + const next = (isArr(res) ? res : [res]).map(ensureNode); + currentNodes.forEach((n) => { + if (n._isRuntime) + n.destroy(); + else + cleanupNode(n); + if (n.parentNode) + n.remove(); + }); + let ref = anchor; + for (let i = next.length - 1;i >= 0; i--) { + const node = next[i]; + if (node.parentNode !== ref.parentNode) + ref.parentNode?.insertBefore(node, ref); + if (node._mounts) + node._mounts.forEach((fn) => fn()); + ref = node; + } + currentNodes = next; + }); + effect(); + el._cleanups.add(() => dispose(effect)); + onUnmount(() => dispose(effect)); + } else { + const node = ensureNode(c); + el.appendChild(node); + if (node._mounts) + node._mounts.forEach((fn) => fn()); + } + }; + append(children); + return el; }; var Render = (renderFn) => { const cleanups = new Set; - const previousOwner = currentOwner; - const container = createEl("div"); + const mounts = []; + const previousOwner = activeOwner; + const previousEffect = activeEffect; + const container = doc.createElement("div"); container.style.display = "contents"; - currentOwner = { cleanups }; + container.setAttribute("role", "presentation"); + activeOwner = { _cleanups: cleanups, _mounts: mounts }; + activeEffect = null; const processResult = (result) => { if (!result) return; @@ -77,14 +403,16 @@ var Render = (renderFn) => { } else if (isArr(result)) { result.forEach(processResult); } else { - container.appendChild(result instanceof Node ? result : createText(result)); + container.appendChild(result instanceof Node ? result : doc.createTextNode(String(result == null ? "" : result))); } }; try { processResult(renderFn({ onCleanup: (fn) => cleanups.add(fn) })); } finally { - currentOwner = previousOwner; + activeOwner = previousOwner; + activeEffect = previousEffect; } + mounts.forEach((fn) => fn()); return { _isRuntime: true, container, @@ -95,338 +423,108 @@ var Render = (renderFn) => { } }; }; -var $ = (initialValue, storageKey = null) => { - const subscribers = new Set; - if (isFunc(initialValue)) { - let cachedValue, isDirty = true; - const effect = () => { - if (effect._deleted) - return; - effect._deps.forEach((dep) => dep.delete(effect)); - effect._deps.clear(); - runWithContext(effect, () => { - const newValue = initialValue(); - if (!Object.is(cachedValue, newValue) || isDirty) { - cachedValue = newValue; - isDirty = false; - triggerUpdate(subscribers); - } - }); - }; - assign(effect, { - _deps: new Set, - _isComputed: true, - _subs: subscribers, - _deleted: false, - markDirty: () => isDirty = true, - stop: () => { - effect._deleted = true; - effect._deps.forEach((dep) => dep.delete(effect)); - subscribers.clear(); - } - }); - if (currentOwner) - currentOwner.cleanups.add(effect.stop); - return () => { - if (isDirty) - effect(); - trackSubscription(subscribers); - return cachedValue; - }; - } - let value = initialValue; - if (storageKey) { - try { - const saved = localStorage.getItem(storageKey); - if (saved !== null) - value = JSON.parse(saved); - } catch (e) { - console.warn("SigPro Storage Lock", e); - } - } - return (...args) => { - if (args.length) { - const nextValue = isFunc(args[0]) ? args[0](value) : args[0]; - if (!Object.is(value, nextValue)) { - value = nextValue; - if (storageKey) - localStorage.setItem(storageKey, JSON.stringify(value)); - triggerUpdate(subscribers); - } - } - trackSubscription(subscribers); - return value; - }; -}; -var $$ = (object, cache = new WeakMap) => { - if (!isObj(object)) - return object; - if (cache.has(object)) - return cache.get(object); - const keySubscribers = {}; - const proxy = new Proxy(object, { - get(target, key) { - if (activeEffect) - trackSubscription(keySubscribers[key] ??= new Set); - const value = Reflect.get(target, key); - return isObj(value) ? $$(value, cache) : value; - }, - set(target, key, value) { - if (Object.is(target[key], value)) - return true; - const success = Reflect.set(target, key, value); - if (keySubscribers[key]) - triggerUpdate(keySubscribers[key]); - return success; - } - }); - cache.set(object, proxy); - return proxy; -}; -var Watch = (target, callbackFn) => { - const isExplicit = isArr(target); - const callback = isExplicit ? callbackFn : target; - if (!isFunc(callback)) - return () => {}; - const owner = currentOwner; - const runner = () => { - if (runner._deleted) - return; - runner._deps.forEach((dep) => dep.delete(runner)); - runner._deps.clear(); - runner._cleanups.forEach((cleanup) => cleanup()); - runner._cleanups.clear(); - const previousOwner = currentOwner; - runner.depth = activeEffect ? activeEffect.depth + 1 : 0; - runWithContext(runner, () => { - currentOwner = { cleanups: runner._cleanups }; - if (isExplicit) { - runWithContext(null, callback); - target.forEach((dep) => isFunc(dep) && dep()); - } else { - callback(); - } - currentOwner = previousOwner; - }); - }; - assign(runner, { - _deps: new Set, - _cleanups: new Set, - _deleted: false, - stop: () => { - if (runner._deleted) - return; - runner._deleted = true; - effectQueue.delete(runner); - runner._deps.forEach((dep) => dep.delete(runner)); - runner._cleanups.forEach((cleanup) => cleanup()); - if (owner) - owner.cleanups.delete(runner.stop); - } - }); - if (owner) - owner.cleanups.add(runner.stop); - runner(); - return runner.stop; -}; -var 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 element = isSVG ? doc.createElementNS("http://www.w3.org/2000/svg", tag) : createEl(tag); - element._cleanups = new Set; - element.onUnmount = (fn) => element._cleanups.add(fn); - const booleanAttributes = ["disabled", "checked", "required", "readonly", "selected", "multiple", "autofocus"]; - const updateAttribute = (name, value) => { - const sanitized = (name === "src" || name === "href") && String(value).toLowerCase().includes("javascript:") ? "#" : value; - if (booleanAttributes.includes(name)) { - element[name] = !!sanitized; - sanitized ? element.setAttribute(name, "") : element.removeAttribute(name); - } else { - sanitized == null ? element.removeAttribute(name) : element.setAttribute(name, sanitized); - } - }; - for (let [key, value] of Object.entries(props)) { - if (key === "ref") { - isFunc(value) ? value(element) : value.current = element; - continue; - } - const isSignal = isFunc(value); - if (key.startsWith("on")) { - const eventName = key.slice(2).toLowerCase().split(".")[0]; - element.addEventListener(eventName, value); - element._cleanups.add(() => element.removeEventListener(eventName, value)); - } else if (isSignal) { - element._cleanups.add(Watch(() => { - const currentVal = value(); - key === "class" ? element.className = currentVal || "" : updateAttribute(key, currentVal); - })); - if (["INPUT", "TEXTAREA", "SELECT"].includes(element.tagName) && (key === "value" || key === "checked")) { - const event = key === "checked" ? "change" : "input"; - const handler = (e) => value(e.target[key]); - element.addEventListener(event, handler); - element._cleanups.add(() => element.removeEventListener(event, handler)); - } - } else { - updateAttribute(key, value); - } - } - const appendChildNode = (child) => { - if (isArr(child)) - return child.forEach(appendChildNode); - if (isFunc(child)) { - const marker = createText(""); - element.appendChild(marker); - let currentNodes = []; - element._cleanups.add(Watch(() => { - const result = child(); - const nextNodes = (isArr(result) ? result : [result]).map((node) => node?._isRuntime ? node.container : node instanceof Node ? node : createText(node)); - currentNodes.forEach((node) => { - cleanupNode(node); - node.remove(); - }); - nextNodes.forEach((node) => marker.parentNode?.insertBefore(node, marker)); - currentNodes = nextNodes; - })); - } else { - element.appendChild(child instanceof Node ? child : createText(child)); - } - }; - appendChildNode(children); - return element; -}; -var If = (condition, thenVal, otherwiseVal = null, transition = null) => { - const marker = createText(""); - const container = Tag("div", { style: "display:contents" }, [marker]); - let currentView = null, lastState = null; - Watch(() => { - const state = !!(isFunc(condition) ? condition() : condition); - if (state === lastState) - return; - lastState = state; - const dispose = () => { - if (currentView) - currentView.destroy(); +var If = (cond, ifYes, ifNot = null) => { + const anchor = doc.createTextNode(""); + const root = Tag("div", { style: "display:contents" }, [anchor]); + let currentView = null; + Watch(() => !!(isFunc(cond) ? cond() : cond), (show) => { + if (currentView) { + currentView.destroy(); currentView = null; - }; - if (currentView && !state && transition?.out) { - transition.out(currentView.container, dispose); - } else { - dispose(); } - const branch = state ? thenVal : otherwiseVal; - if (branch) { - currentView = Render(() => isFunc(branch) ? branch() : branch); - container.insertBefore(currentView.container, marker); - if (state && transition?.in) - transition.in(currentView.container); + const content = show ? ifYes : ifNot; + if (content) { + currentView = Render(() => isFunc(content) ? content() : content); + root.insertBefore(currentView.container, anchor); } }); - return container; + onUnmount(() => currentView?.destroy()); + return root; }; -var For = (source, renderFn, keyFn, tag = "div", props = { style: "display:contents" }) => { - const marker = createText(""); - const container = Tag(tag, props, [marker]); - let viewCache = new Map; - Watch(() => { - const items = (isFunc(source) ? source() : source) || []; +var For = (src, itemFn, keyFn) => { + const anchor = doc.createTextNode(""); + const root = Tag("div", { style: "display:contents" }, [anchor]); + let cache = new Map; + Watch(() => (isFunc(src) ? src() : src) || [], (items) => { const nextCache = new Map; - const order = []; - for (let i = 0;i < items.length; i++) { - const item = items[i]; - const key = keyFn ? keyFn(item, i) : i; - let view = viewCache.get(key); - if (!view) { - const result = renderFn(item, i); - view = result instanceof Node ? { container: result, destroy: () => { - cleanupNode(result); - result.remove(); - } } : Render(() => result); - } - viewCache.delete(key); + const nextOrder = []; + const newItems = items || []; + for (let i = 0;i < newItems.length; i++) { + const item = newItems[i]; + const key = keyFn ? keyFn(item, i) : item?.id ?? i; + let view = cache.get(key); + if (!view) + view = Render(() => itemFn(item, i)); + else + cache.delete(key); nextCache.set(key, view); - order.push(key); + nextOrder.push(view); } - viewCache.forEach((v) => v.destroy()); - let anchor = marker; - for (let i = order.length - 1;i >= 0; i--) { - const view = nextCache.get(order[i]); - if (view.container.nextSibling !== anchor) { - container.insertBefore(view.container, anchor); - } - anchor = view.container; + cache.forEach((view) => view.destroy()); + let lastRef = anchor; + for (let i = nextOrder.length - 1;i >= 0; i--) { + const view = nextOrder[i]; + const node = view.container; + if (node.nextSibling !== lastRef) + root.insertBefore(node, lastRef); + lastRef = node; } - viewCache = nextCache; + cache = nextCache; }); - return container; + return root; }; var Router = (routes) => { - const currentPath = $(window.location.hash.replace(/^#/, "") || "/"); - window.addEventListener("hashchange", () => currentPath(window.location.hash.replace(/^#/, "") || "/")); - const outlet = Tag("div", { class: "router-transition" }); + const getHash = () => window.location.hash.slice(1) || "/"; + const path = $(getHash()); + const handler = () => path(getHash()); + window.addEventListener("hashchange", handler); + onUnmount(() => window.removeEventListener("hashchange", handler)); + const hook = Tag("div", { class: "router-hook" }); let currentView = null; - Watch([currentPath], async () => { - const path = currentPath(); + Watch([path], () => { + const cur = path(); const route = routes.find((r) => { - const routeParts = r.path.split("/").filter(Boolean); - const pathParts = path.split("/").filter(Boolean); - return routeParts.length === pathParts.length && routeParts.every((part, i) => part.startsWith(":") || part === pathParts[i]); + const p1 = r.path.split("/").filter(Boolean); + const p2 = cur.split("/").filter(Boolean); + return p1.length === p2.length && p1.every((p, i) => p[0] === ":" || p === p2[i]); }) || routes.find((r) => r.path === "*"); if (route) { - let component = route.component; - if (isFunc(component) && component.toString().includes("import")) { - component = (await component()).default || await component(); - } + currentView?.destroy(); const params = {}; - route.path.split("/").filter(Boolean).forEach((part, i) => { - if (part.startsWith(":")) - params[part.slice(1)] = path.split("/").filter(Boolean)[i]; + route.path.split("/").filter(Boolean).forEach((p, i) => { + if (p[0] === ":") + params[p.slice(1)] = cur.split("/").filter(Boolean)[i]; }); - if (currentView) - currentView.destroy(); - if (Router.params) - Router.params(params); - currentView = Render(() => { - try { - return isFunc(component) ? component(params) : component; - } catch (e) { - return Tag("div", { class: "p-4 text-error" }, "Error loading view"); - } - }); - outlet.appendChild(currentView.container); + Router.params(params); + currentView = Render(() => isFunc(route.component) ? route.component(params) : route.component); + hook.replaceChildren(currentView.container); } }); - return outlet; + return hook; }; Router.params = $({}); -Router.to = (path) => window.location.hash = path.replace(/^#?\/?/, "#/"); +Router.to = (p) => window.location.hash = p.replace(/^#?\/?/, "#/"); Router.back = () => window.history.back(); Router.path = () => window.location.hash.replace(/^#/, "") || "/"; -var Mount = (component, target) => { - const targetEl = typeof target === "string" ? doc.querySelector(target) : target; - if (!targetEl) +var Mount = (comp, target) => { + const t = typeof target === "string" ? doc.querySelector(target) : target; + if (!t) return; - if (MOUNTED_NODES.has(targetEl)) - MOUNTED_NODES.get(targetEl).destroy(); - const instance = Render(isFunc(component) ? component : () => component); - targetEl.replaceChildren(instance.container); - MOUNTED_NODES.set(targetEl, instance); - return instance; + 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; }; -var SigPro = { $, $$, Render, Watch, Tag, If, For, Router, Mount }; +var SigPro = Object.freeze({ $, $$, Watch, Tag, Render, If, For, Router, Mount, onMount, onUnmount, Batch }); if (typeof window !== "undefined") { - assign(window, SigPro); - const tags = `div span p h1 h2 h3 h4 h5 h6 br hr section article aside nav main header footer address ul ol li dl dt dd a em strong small i b u mark time sub sup pre code blockquote details summary dialog form label input textarea select button option fieldset legend table thead tbody tfoot tr th td caption img video audio canvas svg iframe picture source progress meter`.split(" "); - tags.forEach((tag) => { - const helper = tag[0].toUpperCase() + tag.slice(1); - if (!(helper in window)) - window[helper] = (p, c) => Tag(tag, p, c); - }); - window.SigPro = Object.freeze(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".split(" ").forEach((t) => window[t[0].toUpperCase() + t.slice(1)] = (p, c) => SigPro.Tag(t, p, c)); } export { + onUnmount, + onMount, Watch, Tag, Router, @@ -434,6 +532,7 @@ export { Mount, If, For, + Batch, $$, $ }; diff --git a/dist/sigpro.esm.min.js b/dist/sigpro.esm.min.js index da7601a..042c3cc 100644 --- a/dist/sigpro.esm.min.js +++ b/dist/sigpro.esm.min.js @@ -1 +1 @@ -var w=null,m=null,b=new Set,C=!1,R=new WeakMap,O=document,S=Array.isArray,M=Object.assign,j=(t)=>O.createElement(t),v=(t)=>O.createTextNode(String(t??"")),h=(t)=>typeof t==="function",P=(t)=>typeof t==="object"&&t!==null,T=(t,s)=>{let c=w;w=t;try{return s()}finally{w=c}},A=(t)=>{if(t._cleanups)t._cleanups.forEach((s)=>s()),t._cleanups.clear();t.childNodes?.forEach(A)},U=()=>{if(C)return;C=!0;while(b.size>0){let t=Array.from(b).sort((s,c)=>(s.depth||0)-(c.depth||0));b.clear();for(let s of t)if(!s._deleted)s()}C=!1},B=(t)=>{if(w&&!w._deleted)t.add(w),w._deps.add(t)},k=(t)=>{if(t.forEach((s)=>{if(s===w||s._deleted)return;if(s._isComputed){if(s.markDirty(),s._subs)k(s._subs)}else b.add(s)}),!C)queueMicrotask(U)},N=(t)=>{let s=new Set,c=m,i=j("div");i.style.display="contents",m={cleanups:s};let n=(e)=>{if(!e)return;if(e._isRuntime)s.add(e.destroy),i.appendChild(e.container);else if(S(e))e.forEach(n);else i.appendChild(e instanceof Node?e:v(e))};try{n(t({onCleanup:(e)=>s.add(e)}))}finally{m=c}return{_isRuntime:!0,container:i,destroy:()=>{s.forEach((e)=>e()),A(i),i.remove()}}},W=(t,s=null)=>{let c=new Set;if(h(t)){let n,e=!0,r=()=>{if(r._deleted)return;r._deps.forEach((a)=>a.delete(r)),r._deps.clear(),T(r,()=>{let a=t();if(!Object.is(n,a)||e)n=a,e=!1,k(c)})};if(M(r,{_deps:new Set,_isComputed:!0,_subs:c,_deleted:!1,markDirty:()=>e=!0,stop:()=>{r._deleted=!0,r._deps.forEach((a)=>a.delete(r)),c.clear()}}),m)m.cleanups.add(r.stop);return()=>{if(e)r();return B(c),n}}let i=t;if(s)try{let n=localStorage.getItem(s);if(n!==null)i=JSON.parse(n)}catch(n){console.warn("SigPro Storage Lock",n)}return(...n)=>{if(n.length){let e=h(n[0])?n[0](i):n[0];if(!Object.is(i,e)){if(i=e,s)localStorage.setItem(s,JSON.stringify(i));k(c)}}return B(c),i}},D=(t,s=new WeakMap)=>{if(!P(t))return t;if(s.has(t))return s.get(t);let c={},i=new Proxy(t,{get(n,e){if(w)B(c[e]??=new Set);let r=Reflect.get(n,e);return P(r)?D(r,s):r},set(n,e,r){if(Object.is(n[e],r))return!0;let a=Reflect.set(n,e,r);if(c[e])k(c[e]);return a}});return s.set(t,i),i},E=(t,s)=>{let c=S(t),i=c?s:t;if(!h(i))return()=>{};let n=m,e=()=>{if(e._deleted)return;e._deps.forEach((a)=>a.delete(e)),e._deps.clear(),e._cleanups.forEach((a)=>a()),e._cleanups.clear();let r=m;e.depth=w?w.depth+1:0,T(e,()=>{if(m={cleanups:e._cleanups},c)T(null,i),t.forEach((a)=>h(a)&&a());else i();m=r})};if(M(e,{_deps:new Set,_cleanups:new Set,_deleted:!1,stop:()=>{if(e._deleted)return;if(e._deleted=!0,b.delete(e),e._deps.forEach((r)=>r.delete(e)),e._cleanups.forEach((r)=>r()),n)n.cleanups.delete(e.stop)}}),n)n.cleanups.add(e.stop);return e(),e.stop},y=(t,s={},c=[])=>{if(s instanceof Node||S(s)||!P(s))c=s,s={};let n=/^(svg|path|circle|rect|line|polyline|polygon|g|defs|text|tspan|use)$/.test(t)?O.createElementNS("http://www.w3.org/2000/svg",t):j(t);n._cleanups=new Set,n.onUnmount=(o)=>n._cleanups.add(o);let e=["disabled","checked","required","readonly","selected","multiple","autofocus"],r=(o,l)=>{let d=(o==="src"||o==="href")&&String(l).toLowerCase().includes("javascript:")?"#":l;if(e.includes(o))n[o]=!!d,d?n.setAttribute(o,""):n.removeAttribute(o);else d==null?n.removeAttribute(o):n.setAttribute(o,d)};for(let[o,l]of Object.entries(s)){if(o==="ref"){h(l)?l(n):l.current=n;continue}let d=h(l);if(o.startsWith("on")){let p=o.slice(2).toLowerCase().split(".")[0];n.addEventListener(p,l),n._cleanups.add(()=>n.removeEventListener(p,l))}else if(d){if(n._cleanups.add(E(()=>{let p=l();o==="class"?n.className=p||"":r(o,p)})),["INPUT","TEXTAREA","SELECT"].includes(n.tagName)&&(o==="value"||o==="checked")){let p=o==="checked"?"change":"input",f=(u)=>l(u.target[o]);n.addEventListener(p,f),n._cleanups.add(()=>n.removeEventListener(p,f))}}else r(o,l)}let a=(o)=>{if(S(o))return o.forEach(a);if(h(o)){let l=v("");n.appendChild(l);let d=[];n._cleanups.add(E(()=>{let p=o(),f=(S(p)?p:[p]).map((u)=>u?._isRuntime?u.container:u instanceof Node?u:v(u));d.forEach((u)=>{A(u),u.remove()}),f.forEach((u)=>l.parentNode?.insertBefore(u,l)),d=f}))}else n.appendChild(o instanceof Node?o:v(o))};return a(c),n},q=(t,s,c=null,i=null)=>{let n=v(""),e=y("div",{style:"display:contents"},[n]),r=null,a=null;return E(()=>{let o=!!(h(t)?t():t);if(o===a)return;a=o;let l=()=>{if(r)r.destroy();r=null};if(r&&!o&&i?.out)i.out(r.container,l);else l();let d=o?s:c;if(d){if(r=N(()=>h(d)?d():d),e.insertBefore(r.container,n),o&&i?.in)i.in(r.container)}}),e},I=(t,s,c,i="div",n={style:"display:contents"})=>{let e=v(""),r=y(i,n,[e]),a=new Map;return E(()=>{let o=(h(t)?t():t)||[],l=new Map,d=[];for(let f=0;f{A(g),g.remove()}}:N(()=>g)}a.delete(x),l.set(x,L),d.push(x)}a.forEach((f)=>f.destroy());let p=e;for(let f=d.length-1;f>=0;f--){let u=l.get(d[f]);if(u.container.nextSibling!==p)r.insertBefore(u.container,p);p=u.container}a=l}),r},_=(t)=>{let s=W(window.location.hash.replace(/^#/,"")||"/");window.addEventListener("hashchange",()=>s(window.location.hash.replace(/^#/,"")||"/"));let c=y("div",{class:"router-transition"}),i=null;return E([s],async()=>{let n=s(),e=t.find((r)=>{let a=r.path.split("/").filter(Boolean),o=n.split("/").filter(Boolean);return a.length===o.length&&a.every((l,d)=>l.startsWith(":")||l===o[d])})||t.find((r)=>r.path==="*");if(e){let r=e.component;if(h(r)&&r.toString().includes("import"))r=(await r()).default||await r();let a={};if(e.path.split("/").filter(Boolean).forEach((o,l)=>{if(o.startsWith(":"))a[o.slice(1)]=n.split("/").filter(Boolean)[l]}),i)i.destroy();if(_.params)_.params(a);i=N(()=>{try{return h(r)?r(a):r}catch(o){return y("div",{class:"p-4 text-error"},"Error loading view")}}),c.appendChild(i.container)}}),c};_.params=W({});_.to=(t)=>window.location.hash=t.replace(/^#?\/?/,"#/");_.back=()=>window.history.back();_.path=()=>window.location.hash.replace(/^#/,"")||"/";var $=(t,s)=>{let c=typeof s==="string"?O.querySelector(s):s;if(!c)return;if(R.has(c))R.get(c).destroy();let i=N(h(t)?t:()=>t);return c.replaceChildren(i.container),R.set(c,i),i},V={$:W,$$:D,Render:N,Watch:E,Tag:y,If:q,For:I,Router:_,Mount:$};if(typeof window<"u")M(window,V),"div span p h1 h2 h3 h4 h5 h6 br hr section article aside nav main header footer address ul ol li dl dt dd a em strong small i b u mark time sub sup pre code blockquote details summary dialog form label input textarea select button option fieldset legend table thead tbody tfoot tr th td caption img video audio canvas svg iframe picture source progress meter".split(" ").forEach((s)=>{let c=s[0].toUpperCase()+s.slice(1);if(!(c in window))window[c]=(i,n)=>y(s,i,n)}),window.SigPro=Object.freeze(V);export{E as Watch,y as Tag,_ as Router,N as Render,$ as Mount,q as If,I as For,D as $$,W as $}; +var m=(e)=>typeof e==="function",P=(e)=>e&&typeof e==="object",S=Array.isArray,E=typeof document<"u"?document:null,U=(e)=>e?._isRuntime?e.container:e instanceof Node?e:E.createTextNode(e==null?"":String(e)),d=null,_=null,b=!1,C=0,T=new Set,M=new WeakMap,j=Symbol("iter"),B=new WeakMap,g=(e)=>{if(!e||e._disposed)return;e._disposed=!0;let s=[e];while(s.length){let t=s.pop();if(t._cleanups)t._cleanups.forEach((c)=>c()),t._cleanups.clear();if(t._children)t._children.forEach((c)=>s.push(c)),t._children.clear();if(t._deps)t._deps.forEach((c)=>c.delete(t)),t._deps.clear()}},k=(e)=>{if(_)(_._mounts||=[]).push(e)},N=(e)=>{if(_)(_._cleanups||=new Set).add(e)},z=(e)=>{let s=d;d=null;try{return e()}finally{d=s}},x=(e,s=!1)=>{let t=()=>{if(t._disposed)return;if(t._deps)t._deps.forEach((n)=>n.delete(t));if(t._cleanups)t._cleanups.forEach((n)=>n()),t._cleanups.clear();let c=d,r=_;d=_=t;try{return t._result=e()}catch(n){console.error("[SigPro]",n)}finally{d=c,_=r}};if(t._deps=t._cleanups=t._children=null,t._disposed=!1,t._isComputed=s,t._depth=d?d._depth+1:0,t._mounts=[],t._parent=_,_)(_._children||=new Set).add(t);return t},W=()=>{if(b)return;b=!0;let e=Array.from(T).sort((s,t)=>s._depth-t._depth);T.clear();for(let s of e)if(!s._disposed)s();b=!1},F=(e)=>{C++;try{return e()}finally{if(C--,C===0&&T.size>0&&!b)W()}},w=(e,s=!1)=>{if(!s&&d&&!d._disposed)e.add(d),(d._deps||=new Set).add(e);else if(s){let t=!1;if(e.forEach((c)=>{if(c===d||c._disposed)return;if(c._isComputed){if(c._dirty=!0,c._subs)w(c._subs,!0)}else T.add(c),t=!0}),t&&!b&&C===0)queueMicrotask(W)}},L=(e,s=null)=>{let t=new Set;if(m(e)){let c,r=!0,n=()=>{if(r){let i=d;d=n;try{let o=e();if(!Object.is(c,o))c=o,r=!1,w(t,!0)}finally{d=i}}return w(t),c};if(n._isComputed=!0,n._subs=t,n._dirty=!0,n._deps=null,n._disposed=!1,n.markDirty=()=>{r=!0},n.stop=()=>{if(n._disposed=!0,n._deps)n._deps.forEach((i)=>i.delete(n)),n._deps.clear();t.clear()},_)N(n.stop);return n}if(s)try{e=JSON.parse(localStorage.getItem(s))??e}catch(c){}return(...c)=>{if(c.length){let r=m(c[0])?c[0](e):c[0];if(!Object.is(e,r)){if(e=r,s)localStorage.setItem(s,JSON.stringify(e));w(t,!0)}}return w(t),e}},V=(e)=>{if(!P(e))return e;if(M.has(e))return M.get(e);let s=new Map,t=(r)=>{let n=s.get(r);if(!n)s.set(r,n=new Set);return n},c=new Proxy(e,{get(r,n){return w(t(n)),V(r[n])},set(r,n,i){let o=!(n in r);if(!Object.is(r[n],i)){if(r[n]=i,w(t(n),!0),o)w(t(j),!0)}return!0},deleteProperty(r,n){let i=Reflect.deleteProperty(r,n);if(i)w(t(n),!0),w(t(j),!0);return i},ownKeys(r){return w(t(j)),Reflect.ownKeys(r)}});return M.set(e,c),c},A=(e,s)=>{if(s===void 0){let c=x(e);return c(),()=>g(c)}let t=x(()=>{let c=Array.isArray(e)?e.map((r)=>r()):e();z(()=>s(c))});return t(),()=>g(t)},$=(e)=>{if(e._cleanups)e._cleanups.forEach((s)=>s()),e._cleanups.clear();if(e._ownerEffect)g(e._ownerEffect);if(e.childNodes)e.childNodes.forEach($)},G=/^\s*(javascript|data|vbscript):/i,J=(e)=>e==="src"||e==="href"||e.startsWith("on"),D=(e,s)=>{if(s==null||s===!1)return null;if(J(e)){let t=String(s);if(G.test(t))return console.warn(`[SigPro] Bloqueado protocolo peligroso en ${e}`),"#"}return s},R=(e,s={},t=[])=>{if(s instanceof Node||S(s)||!P(s))t=s,s={};if(m(e)){let i={_mounts:[],_cleanups:new Set},o=x(()=>{let l=e(s,{children:t,emit:(h,...u)=>s[`on${h[0].toUpperCase()}${h.slice(1)}`]?.(...u)});return o._result=l,l});o();let a=o._result;if(a==null)return null;let f=a instanceof Node||S(a)&&a.every((l)=>l instanceof Node)?a:E.createTextNode(String(a)),p=(l)=>{if(P(l)&&!l._isRuntime)l._mounts=o._mounts||[],l._cleanups=o._cleanups||new Set,l._ownerEffect=o};return S(f)?f.forEach(p):p(f),f}let c=/^(svg|path|circle|rect|line|polyline|polygon|g|defs|text|tspan|use)$/.test(e),r=c?E.createElementNS("http://www.w3.org/2000/svg",e):E.createElement(e);r._cleanups=new Set;for(let i in s){if(!s.hasOwnProperty(i))continue;let o=s[i];if(i==="ref"){m(o)?o(r):o.current=r;continue}if(i.startsWith("on")){let a=i.slice(2).toLowerCase();r.addEventListener(a,o);let f=()=>r.removeEventListener(a,o);r._cleanups.add(f),N(f)}else if(m(o)){let a=x(()=>{let f=D(i,o());if(i==="class")r.className=f||"";else if(f==null)r.removeAttribute(i);else if(i in r&&!c)r[i]=f;else r.setAttribute(i,f===!0?"":f)});if(a(),r._cleanups.add(()=>g(a)),N(()=>g(a)),/^(INPUT|TEXTAREA|SELECT)$/.test(r.tagName)&&(i==="value"||i==="checked")){let f=i==="checked"?"change":"input";r.addEventListener(f,(p)=>o(p.target[i]))}}else{let a=D(i,o);if(a!=null)if(i in r&&!c)r[i]=a;else r.setAttribute(i,a===!0?"":a)}}let n=(i)=>{if(S(i))return i.forEach(n);if(m(i)){let o=E.createTextNode("");r.appendChild(o);let a=[],f=x(()=>{let p=i(),l=(S(p)?p:[p]).map(U);a.forEach((u)=>{if(u._isRuntime)u.destroy();else $(u);if(u.parentNode)u.remove()});let h=o;for(let u=l.length-1;u>=0;u--){let y=l[u];if(y.parentNode!==h.parentNode)h.parentNode?.insertBefore(y,h);if(y._mounts)y._mounts.forEach((q)=>q());h=y}a=l});f(),r._cleanups.add(()=>g(f)),N(()=>g(f))}else{let o=U(i);if(r.appendChild(o),o._mounts)o._mounts.forEach((a)=>a())}};return n(t),r},O=(e)=>{let s=new Set,t=[],c=_,r=d,n=E.createElement("div");n.style.display="contents",n.setAttribute("role","presentation"),_={_cleanups:s,_mounts:t},d=null;let i=(o)=>{if(!o)return;if(o._isRuntime)s.add(o.destroy),n.appendChild(o.container);else if(S(o))o.forEach(i);else n.appendChild(o instanceof Node?o:E.createTextNode(String(o==null?"":o)))};try{i(e({onCleanup:(o)=>s.add(o)}))}finally{_=c,d=r}return t.forEach((o)=>o()),{_isRuntime:!0,container:n,destroy:()=>{s.forEach((o)=>o()),$(n),n.remove()}}},K=(e,s,t=null)=>{let c=E.createTextNode(""),r=R("div",{style:"display:contents"},[c]),n=null;return A(()=>!!(m(e)?e():e),(i)=>{if(n)n.destroy(),n=null;let o=i?s:t;if(o)n=O(()=>m(o)?o():o),r.insertBefore(n.container,c)}),N(()=>n?.destroy()),r},Q=(e,s,t)=>{let c=E.createTextNode(""),r=R("div",{style:"display:contents"},[c]),n=new Map;return A(()=>(m(e)?e():e)||[],(i)=>{let o=new Map,a=[],f=i||[];for(let l=0;ls(h,l));else n.delete(u);o.set(u,y),a.push(y)}n.forEach((l)=>l.destroy());let p=c;for(let l=a.length-1;l>=0;l--){let u=a[l].container;if(u.nextSibling!==p)r.insertBefore(u,p);p=u}n=o}),r},v=(e)=>{let s=()=>window.location.hash.slice(1)||"/",t=L(s()),c=()=>t(s());window.addEventListener("hashchange",c),N(()=>window.removeEventListener("hashchange",c));let r=R("div",{class:"router-hook"}),n=null;return A([t],()=>{let i=t(),o=e.find((a)=>{let f=a.path.split("/").filter(Boolean),p=i.split("/").filter(Boolean);return f.length===p.length&&f.every((l,h)=>l[0]===":"||l===p[h])})||e.find((a)=>a.path==="*");if(o){n?.destroy();let a={};o.path.split("/").filter(Boolean).forEach((f,p)=>{if(f[0]===":")a[f.slice(1)]=i.split("/").filter(Boolean)[p]}),v.params(a),n=O(()=>m(o.component)?o.component(a):o.component),r.replaceChildren(n.container)}}),r};v.params=L({});v.to=(e)=>window.location.hash=e.replace(/^#?\/?/,"#/");v.back=()=>window.history.back();v.path=()=>window.location.hash.replace(/^#/,"")||"/";var H=(e,s)=>{let t=typeof s==="string"?E.querySelector(s):s;if(!t)return;if(B.has(t))B.get(t).destroy();let c=O(m(e)?e:()=>e);return t.replaceChildren(c.container),B.set(t,c),c},I=Object.freeze({$:L,$$:V,Watch:A,Tag:R,Render:O,If:K,For:Q,Router:v,Mount:H,onMount:k,onUnmount:N,Batch:F});if(typeof window<"u")Object.assign(window,I),"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((e)=>window[e[0].toUpperCase()+e.slice(1)]=(s,t)=>I.Tag(e,s,t));export{N as onUnmount,k as onMount,A as Watch,R as Tag,v as Router,O as Render,H as Mount,K as If,Q as For,F as Batch,V as $$,L as $}; diff --git a/dist/sigpro.js b/dist/sigpro.js index d7b2776..2eaf487 100644 --- a/dist/sigpro.js +++ b/dist/sigpro.js @@ -30,6 +30,8 @@ // index.js var exports_sigpro = {}; __export(exports_sigpro, { + onUnmount: () => onUnmount, + onMount: () => onMount, Watch: () => Watch, Tag: () => Tag, Router: () => Router, @@ -37,80 +39,407 @@ Mount: () => Mount, If: () => If, For: () => For, + Batch: () => Batch, $$: () => $$, $: () => $ }); // sigpro.js - var activeEffect = null; - var currentOwner = null; - var effectQueue = new Set; - var isFlushing = false; - var MOUNTED_NODES = new WeakMap; - var doc = document; - var isArr = Array.isArray; - var assign = Object.assign; - var createEl = (t) => doc.createElement(t); - var createText = (t) => doc.createTextNode(String(t ?? "")); var isFunc = (f) => typeof f === "function"; - var isObj = (o) => typeof o === "object" && o !== null; - var runWithContext = (effect, callback) => { - const previousEffect = activeEffect; - activeEffect = effect; + var isObj = (o) => o && typeof o === "object"; + var isArr = Array.isArray; + var doc = typeof document !== "undefined" ? document : null; + var ensureNode = (n) => n?._isRuntime ? n.container : n instanceof Node ? n : doc.createTextNode(n == null ? "" : String(n)); + var activeEffect = null; + var activeOwner = null; + var isFlushing = false; + var batchDepth = 0; + var effectQueue = new Set; + var proxyCache = new WeakMap; + var ITER = Symbol("iter"); + var MOUNTED_NODES = new WeakMap; + var dispose = (eff) => { + if (!eff || eff._disposed) + return; + eff._disposed = true; + const stack = [eff]; + while (stack.length) { + const e = stack.pop(); + if (e._cleanups) { + e._cleanups.forEach((fn) => fn()); + e._cleanups.clear(); + } + if (e._children) { + e._children.forEach((child) => stack.push(child)); + e._children.clear(); + } + if (e._deps) { + e._deps.forEach((depSet) => depSet.delete(e)); + e._deps.clear(); + } + } + }; + var onMount = (fn) => { + if (activeOwner) + (activeOwner._mounts ||= []).push(fn); + }; + var onUnmount = (fn) => { + if (activeOwner) + (activeOwner._cleanups ||= new Set).add(fn); + }; + var untrack = (fn) => { + const p = activeEffect; + activeEffect = null; try { - return callback(); + return fn(); } finally { - activeEffect = previousEffect; + activeEffect = p; } }; - var cleanupNode = (node) => { - if (node._cleanups) { - node._cleanups.forEach((dispose) => dispose()); - node._cleanups.clear(); - } - node.childNodes?.forEach(cleanupNode); + var createEffect = (fn, isComputed = false) => { + const effect = () => { + if (effect._disposed) + return; + if (effect._deps) + effect._deps.forEach((s) => s.delete(effect)); + if (effect._cleanups) { + effect._cleanups.forEach((c) => c()); + effect._cleanups.clear(); + } + const prevEffect = activeEffect; + const prevOwner = activeOwner; + activeEffect = activeOwner = effect; + try { + return effect._result = fn(); + } catch (e) { + console.error("[SigPro]", e); + } finally { + activeEffect = prevEffect; + activeOwner = prevOwner; + } + }; + effect._deps = effect._cleanups = effect._children = null; + effect._disposed = false; + effect._isComputed = isComputed; + effect._depth = activeEffect ? activeEffect._depth + 1 : 0; + effect._mounts = []; + effect._parent = activeOwner; + if (activeOwner) + (activeOwner._children ||= new Set).add(effect); + return effect; }; - var flushEffects = () => { + var flush = () => { if (isFlushing) return; isFlushing = true; - while (effectQueue.size > 0) { - const sortedEffects = Array.from(effectQueue).sort((a, b) => (a.depth || 0) - (b.depth || 0)); - effectQueue.clear(); - for (const effect of sortedEffects) { - if (!effect._deleted) - effect(); - } - } + const sorted = Array.from(effectQueue).sort((a, b) => a._depth - b._depth); + effectQueue.clear(); + for (const e of sorted) + if (!e._disposed) + e(); isFlushing = false; }; - var trackSubscription = (subscribers) => { - if (activeEffect && !activeEffect._deleted) { - subscribers.add(activeEffect); - activeEffect._deps.add(subscribers); + var Batch = (fn) => { + batchDepth++; + try { + return fn(); + } finally { + batchDepth--; + if (batchDepth === 0 && effectQueue.size > 0 && !isFlushing) { + flush(); + } } }; - var triggerUpdate = (subscribers) => { - subscribers.forEach((effect) => { - if (effect === activeEffect || effect._deleted) - return; - if (effect._isComputed) { - effect.markDirty(); - if (effect._subs) - triggerUpdate(effect._subs); - } else { - effectQueue.add(effect); + var trackUpdate = (subs, trigger = false) => { + if (!trigger && activeEffect && !activeEffect._disposed) { + subs.add(activeEffect); + (activeEffect._deps ||= new Set).add(subs); + } else if (trigger) { + let hasQueue = false; + subs.forEach((e) => { + if (e === activeEffect || e._disposed) + return; + if (e._isComputed) { + e._dirty = true; + if (e._subs) + trackUpdate(e._subs, true); + } else { + effectQueue.add(e); + hasQueue = true; + } + }); + if (hasQueue && !isFlushing && batchDepth === 0) + queueMicrotask(flush); + } + }; + var $ = (val, key = null) => { + const subs = new Set; + if (isFunc(val)) { + let cache, dirty = true; + const computed = () => { + if (dirty) { + const prev = activeEffect; + activeEffect = computed; + try { + const next = val(); + if (!Object.is(cache, next)) { + cache = next; + dirty = false; + trackUpdate(subs, true); + } + } finally { + activeEffect = prev; + } + } + trackUpdate(subs); + return cache; + }; + computed._isComputed = true; + computed._subs = subs; + computed._dirty = true; + computed._deps = null; + computed._disposed = false; + computed.markDirty = () => { + dirty = true; + }; + computed.stop = () => { + computed._disposed = true; + if (computed._deps) { + computed._deps.forEach((depSet) => depSet.delete(computed)); + computed._deps.clear(); + } + subs.clear(); + }; + if (activeOwner) + onUnmount(computed.stop); + return computed; + } + if (key) + try { + val = JSON.parse(localStorage.getItem(key)) ?? val; + } catch (e) {} + return (...args) => { + if (args.length) { + const next = isFunc(args[0]) ? args[0](val) : args[0]; + if (!Object.is(val, next)) { + val = next; + if (key) + localStorage.setItem(key, JSON.stringify(val)); + trackUpdate(subs, true); + } + } + trackUpdate(subs); + return val; + }; + }; + var $$ = (target) => { + if (!isObj(target)) + return target; + if (proxyCache.has(target)) + return proxyCache.get(target); + const subsMap = new Map; + const getSubs = (k) => { + let s = subsMap.get(k); + if (!s) + subsMap.set(k, s = new Set); + return s; + }; + const proxy = new Proxy(target, { + get(t, k) { + trackUpdate(getSubs(k)); + return $$(t[k]); + }, + set(t, k, v) { + const isNew = !(k in t); + if (!Object.is(t[k], v)) { + t[k] = v; + trackUpdate(getSubs(k), true); + if (isNew) + trackUpdate(getSubs(ITER), true); + } + return true; + }, + deleteProperty(t, k) { + const res = Reflect.deleteProperty(t, k); + if (res) { + trackUpdate(getSubs(k), true); + trackUpdate(getSubs(ITER), true); + } + return res; + }, + ownKeys(t) { + trackUpdate(getSubs(ITER)); + return Reflect.ownKeys(t); } }); - if (!isFlushing) - queueMicrotask(flushEffects); + proxyCache.set(target, proxy); + return proxy; + }; + var Watch = (sources, cb) => { + if (cb === undefined) { + const effect2 = createEffect(sources); + effect2(); + return () => dispose(effect2); + } + const effect = createEffect(() => { + const vals = Array.isArray(sources) ? sources.map((s) => s()) : sources(); + untrack(() => cb(vals)); + }); + effect(); + return () => dispose(effect); + }; + var cleanupNode = (node) => { + if (node._cleanups) { + node._cleanups.forEach((fn) => fn()); + node._cleanups.clear(); + } + if (node._ownerEffect) + dispose(node._ownerEffect); + if (node.childNodes) + node.childNodes.forEach(cleanupNode); + }; + var DANGEROUS_PROTOCOL = /^\s*(javascript|data|vbscript):/i; + var isDangerousAttr = (key) => key === "src" || key === "href" || key.startsWith("on"); + var validateAttr = (key, val) => { + if (val == null || val === false) + return null; + if (isDangerousAttr(key)) { + const sVal = String(val); + if (DANGEROUS_PROTOCOL.test(sVal)) { + console.warn(`[SigPro] Bloqueado protocolo peligroso en ${key}`); + return "#"; + } + } + return val; + }; + var Tag = (tag, props = {}, children = []) => { + if (props instanceof Node || isArr(props) || !isObj(props)) { + children = props; + props = {}; + } + if (isFunc(tag)) { + const ctx = { _mounts: [], _cleanups: new Set }; + const effect = createEffect(() => { + const result2 = tag(props, { + children, + emit: (ev, ...args) => props[`on${ev[0].toUpperCase()}${ev.slice(1)}`]?.(...args) + }); + effect._result = result2; + return result2; + }); + effect(); + const result = effect._result; + if (result == null) + return null; + const node = result instanceof Node || isArr(result) && result.every((n) => n instanceof Node) ? result : doc.createTextNode(String(result)); + const attach = (n) => { + if (isObj(n) && !n._isRuntime) { + n._mounts = effect._mounts || []; + n._cleanups = effect._cleanups || new Set; + n._ownerEffect = effect; + } + }; + isArr(node) ? node.forEach(attach) : attach(node); + return node; + } + 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); + el._cleanups = new Set; + for (let k in props) { + if (!props.hasOwnProperty(k)) + continue; + let v = props[k]; + if (k === "ref") { + isFunc(v) ? v(el) : v.current = el; + continue; + } + if (k.startsWith("on")) { + const ev = k.slice(2).toLowerCase(); + el.addEventListener(ev, v); + const off = () => el.removeEventListener(ev, v); + el._cleanups.add(off); + onUnmount(off); + } else if (isFunc(v)) { + const effect = createEffect(() => { + const val = validateAttr(k, v()); + if (k === "class") + el.className = val || ""; + else if (val == null) + el.removeAttribute(k); + else if (k in el && !isSVG) + el[k] = val; + else + el.setAttribute(k, val === true ? "" : val); + }); + effect(); + el._cleanups.add(() => dispose(effect)); + onUnmount(() => dispose(effect)); + if (/^(INPUT|TEXTAREA|SELECT)$/.test(el.tagName) && (k === "value" || k === "checked")) { + const evType = k === "checked" ? "change" : "input"; + el.addEventListener(evType, (ev) => v(ev.target[k])); + } + } else { + const val = validateAttr(k, v); + if (val != null) { + if (k in el && !isSVG) + el[k] = val; + else + el.setAttribute(k, val === true ? "" : val); + } + } + } + const append = (c) => { + if (isArr(c)) + return c.forEach(append); + if (isFunc(c)) { + const anchor = doc.createTextNode(""); + el.appendChild(anchor); + let currentNodes = []; + const effect = createEffect(() => { + const res = c(); + const next = (isArr(res) ? res : [res]).map(ensureNode); + currentNodes.forEach((n) => { + if (n._isRuntime) + n.destroy(); + else + cleanupNode(n); + if (n.parentNode) + n.remove(); + }); + let ref = anchor; + for (let i = next.length - 1;i >= 0; i--) { + const node = next[i]; + if (node.parentNode !== ref.parentNode) + ref.parentNode?.insertBefore(node, ref); + if (node._mounts) + node._mounts.forEach((fn) => fn()); + ref = node; + } + currentNodes = next; + }); + effect(); + el._cleanups.add(() => dispose(effect)); + onUnmount(() => dispose(effect)); + } else { + const node = ensureNode(c); + el.appendChild(node); + if (node._mounts) + node._mounts.forEach((fn) => fn()); + } + }; + append(children); + return el; }; var Render = (renderFn) => { const cleanups = new Set; - const previousOwner = currentOwner; - const container = createEl("div"); + const mounts = []; + const previousOwner = activeOwner; + const previousEffect = activeEffect; + const container = doc.createElement("div"); container.style.display = "contents"; - currentOwner = { cleanups }; + container.setAttribute("role", "presentation"); + activeOwner = { _cleanups: cleanups, _mounts: mounts }; + activeEffect = null; const processResult = (result) => { if (!result) return; @@ -120,14 +449,16 @@ } else if (isArr(result)) { result.forEach(processResult); } else { - container.appendChild(result instanceof Node ? result : createText(result)); + container.appendChild(result instanceof Node ? result : doc.createTextNode(String(result == null ? "" : result))); } }; try { processResult(renderFn({ onCleanup: (fn) => cleanups.add(fn) })); } finally { - currentOwner = previousOwner; + activeOwner = previousOwner; + activeEffect = previousEffect; } + mounts.forEach((fn) => fn()); return { _isRuntime: true, container, @@ -138,335 +469,103 @@ } }; }; - var $ = (initialValue, storageKey = null) => { - const subscribers = new Set; - if (isFunc(initialValue)) { - let cachedValue, isDirty = true; - const effect = () => { - if (effect._deleted) - return; - effect._deps.forEach((dep) => dep.delete(effect)); - effect._deps.clear(); - runWithContext(effect, () => { - const newValue = initialValue(); - if (!Object.is(cachedValue, newValue) || isDirty) { - cachedValue = newValue; - isDirty = false; - triggerUpdate(subscribers); - } - }); - }; - assign(effect, { - _deps: new Set, - _isComputed: true, - _subs: subscribers, - _deleted: false, - markDirty: () => isDirty = true, - stop: () => { - effect._deleted = true; - effect._deps.forEach((dep) => dep.delete(effect)); - subscribers.clear(); - } - }); - if (currentOwner) - currentOwner.cleanups.add(effect.stop); - return () => { - if (isDirty) - effect(); - trackSubscription(subscribers); - return cachedValue; - }; - } - let value = initialValue; - if (storageKey) { - try { - const saved = localStorage.getItem(storageKey); - if (saved !== null) - value = JSON.parse(saved); - } catch (e) { - console.warn("SigPro Storage Lock", e); - } - } - return (...args) => { - if (args.length) { - const nextValue = isFunc(args[0]) ? args[0](value) : args[0]; - if (!Object.is(value, nextValue)) { - value = nextValue; - if (storageKey) - localStorage.setItem(storageKey, JSON.stringify(value)); - triggerUpdate(subscribers); - } - } - trackSubscription(subscribers); - return value; - }; - }; - var $$ = (object, cache = new WeakMap) => { - if (!isObj(object)) - return object; - if (cache.has(object)) - return cache.get(object); - const keySubscribers = {}; - const proxy = new Proxy(object, { - get(target, key) { - if (activeEffect) - trackSubscription(keySubscribers[key] ??= new Set); - const value = Reflect.get(target, key); - return isObj(value) ? $$(value, cache) : value; - }, - set(target, key, value) { - if (Object.is(target[key], value)) - return true; - const success = Reflect.set(target, key, value); - if (keySubscribers[key]) - triggerUpdate(keySubscribers[key]); - return success; - } - }); - cache.set(object, proxy); - return proxy; - }; - var Watch = (target, callbackFn) => { - const isExplicit = isArr(target); - const callback = isExplicit ? callbackFn : target; - if (!isFunc(callback)) - return () => {}; - const owner = currentOwner; - const runner = () => { - if (runner._deleted) - return; - runner._deps.forEach((dep) => dep.delete(runner)); - runner._deps.clear(); - runner._cleanups.forEach((cleanup) => cleanup()); - runner._cleanups.clear(); - const previousOwner = currentOwner; - runner.depth = activeEffect ? activeEffect.depth + 1 : 0; - runWithContext(runner, () => { - currentOwner = { cleanups: runner._cleanups }; - if (isExplicit) { - runWithContext(null, callback); - target.forEach((dep) => isFunc(dep) && dep()); - } else { - callback(); - } - currentOwner = previousOwner; - }); - }; - assign(runner, { - _deps: new Set, - _cleanups: new Set, - _deleted: false, - stop: () => { - if (runner._deleted) - return; - runner._deleted = true; - effectQueue.delete(runner); - runner._deps.forEach((dep) => dep.delete(runner)); - runner._cleanups.forEach((cleanup) => cleanup()); - if (owner) - owner.cleanups.delete(runner.stop); - } - }); - if (owner) - owner.cleanups.add(runner.stop); - runner(); - return runner.stop; - }; - var 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 element = isSVG ? doc.createElementNS("http://www.w3.org/2000/svg", tag) : createEl(tag); - element._cleanups = new Set; - element.onUnmount = (fn) => element._cleanups.add(fn); - const booleanAttributes = ["disabled", "checked", "required", "readonly", "selected", "multiple", "autofocus"]; - const updateAttribute = (name, value) => { - const sanitized = (name === "src" || name === "href") && String(value).toLowerCase().includes("javascript:") ? "#" : value; - if (booleanAttributes.includes(name)) { - element[name] = !!sanitized; - sanitized ? element.setAttribute(name, "") : element.removeAttribute(name); - } else { - sanitized == null ? element.removeAttribute(name) : element.setAttribute(name, sanitized); - } - }; - for (let [key, value] of Object.entries(props)) { - if (key === "ref") { - isFunc(value) ? value(element) : value.current = element; - continue; - } - const isSignal = isFunc(value); - if (key.startsWith("on")) { - const eventName = key.slice(2).toLowerCase().split(".")[0]; - element.addEventListener(eventName, value); - element._cleanups.add(() => element.removeEventListener(eventName, value)); - } else if (isSignal) { - element._cleanups.add(Watch(() => { - const currentVal = value(); - key === "class" ? element.className = currentVal || "" : updateAttribute(key, currentVal); - })); - if (["INPUT", "TEXTAREA", "SELECT"].includes(element.tagName) && (key === "value" || key === "checked")) { - const event = key === "checked" ? "change" : "input"; - const handler = (e) => value(e.target[key]); - element.addEventListener(event, handler); - element._cleanups.add(() => element.removeEventListener(event, handler)); - } - } else { - updateAttribute(key, value); - } - } - const appendChildNode = (child) => { - if (isArr(child)) - return child.forEach(appendChildNode); - if (isFunc(child)) { - const marker = createText(""); - element.appendChild(marker); - let currentNodes = []; - element._cleanups.add(Watch(() => { - const result = child(); - const nextNodes = (isArr(result) ? result : [result]).map((node) => node?._isRuntime ? node.container : node instanceof Node ? node : createText(node)); - currentNodes.forEach((node) => { - cleanupNode(node); - node.remove(); - }); - nextNodes.forEach((node) => marker.parentNode?.insertBefore(node, marker)); - currentNodes = nextNodes; - })); - } else { - element.appendChild(child instanceof Node ? child : createText(child)); - } - }; - appendChildNode(children); - return element; - }; - var If = (condition, thenVal, otherwiseVal = null, transition = null) => { - const marker = createText(""); - const container = Tag("div", { style: "display:contents" }, [marker]); - let currentView = null, lastState = null; - Watch(() => { - const state = !!(isFunc(condition) ? condition() : condition); - if (state === lastState) - return; - lastState = state; - const dispose = () => { - if (currentView) - currentView.destroy(); + var If = (cond, ifYes, ifNot = null) => { + const anchor = doc.createTextNode(""); + const root = Tag("div", { style: "display:contents" }, [anchor]); + let currentView = null; + Watch(() => !!(isFunc(cond) ? cond() : cond), (show) => { + if (currentView) { + currentView.destroy(); currentView = null; - }; - if (currentView && !state && transition?.out) { - transition.out(currentView.container, dispose); - } else { - dispose(); } - const branch = state ? thenVal : otherwiseVal; - if (branch) { - currentView = Render(() => isFunc(branch) ? branch() : branch); - container.insertBefore(currentView.container, marker); - if (state && transition?.in) - transition.in(currentView.container); + const content = show ? ifYes : ifNot; + if (content) { + currentView = Render(() => isFunc(content) ? content() : content); + root.insertBefore(currentView.container, anchor); } }); - return container; + onUnmount(() => currentView?.destroy()); + return root; }; - var For = (source, renderFn, keyFn, tag = "div", props = { style: "display:contents" }) => { - const marker = createText(""); - const container = Tag(tag, props, [marker]); - let viewCache = new Map; - Watch(() => { - const items = (isFunc(source) ? source() : source) || []; + var For = (src, itemFn, keyFn) => { + const anchor = doc.createTextNode(""); + const root = Tag("div", { style: "display:contents" }, [anchor]); + let cache = new Map; + Watch(() => (isFunc(src) ? src() : src) || [], (items) => { const nextCache = new Map; - const order = []; - for (let i = 0;i < items.length; i++) { - const item = items[i]; - const key = keyFn ? keyFn(item, i) : i; - let view = viewCache.get(key); - if (!view) { - const result = renderFn(item, i); - view = result instanceof Node ? { container: result, destroy: () => { - cleanupNode(result); - result.remove(); - } } : Render(() => result); - } - viewCache.delete(key); + const nextOrder = []; + const newItems = items || []; + for (let i = 0;i < newItems.length; i++) { + const item = newItems[i]; + const key = keyFn ? keyFn(item, i) : item?.id ?? i; + let view = cache.get(key); + if (!view) + view = Render(() => itemFn(item, i)); + else + cache.delete(key); nextCache.set(key, view); - order.push(key); + nextOrder.push(view); } - viewCache.forEach((v) => v.destroy()); - let anchor = marker; - for (let i = order.length - 1;i >= 0; i--) { - const view = nextCache.get(order[i]); - if (view.container.nextSibling !== anchor) { - container.insertBefore(view.container, anchor); - } - anchor = view.container; + cache.forEach((view) => view.destroy()); + let lastRef = anchor; + for (let i = nextOrder.length - 1;i >= 0; i--) { + const view = nextOrder[i]; + const node = view.container; + if (node.nextSibling !== lastRef) + root.insertBefore(node, lastRef); + lastRef = node; } - viewCache = nextCache; + cache = nextCache; }); - return container; + return root; }; var Router = (routes) => { - const currentPath = $(window.location.hash.replace(/^#/, "") || "/"); - window.addEventListener("hashchange", () => currentPath(window.location.hash.replace(/^#/, "") || "/")); - const outlet = Tag("div", { class: "router-transition" }); + const getHash = () => window.location.hash.slice(1) || "/"; + const path = $(getHash()); + const handler = () => path(getHash()); + window.addEventListener("hashchange", handler); + onUnmount(() => window.removeEventListener("hashchange", handler)); + const hook = Tag("div", { class: "router-hook" }); let currentView = null; - Watch([currentPath], async () => { - const path = currentPath(); + Watch([path], () => { + const cur = path(); const route = routes.find((r) => { - const routeParts = r.path.split("/").filter(Boolean); - const pathParts = path.split("/").filter(Boolean); - return routeParts.length === pathParts.length && routeParts.every((part, i) => part.startsWith(":") || part === pathParts[i]); + const p1 = r.path.split("/").filter(Boolean); + const p2 = cur.split("/").filter(Boolean); + return p1.length === p2.length && p1.every((p, i) => p[0] === ":" || p === p2[i]); }) || routes.find((r) => r.path === "*"); if (route) { - let component = route.component; - if (isFunc(component) && component.toString().includes("import")) { - component = (await component()).default || await component(); - } + currentView?.destroy(); const params = {}; - route.path.split("/").filter(Boolean).forEach((part, i) => { - if (part.startsWith(":")) - params[part.slice(1)] = path.split("/").filter(Boolean)[i]; + route.path.split("/").filter(Boolean).forEach((p, i) => { + if (p[0] === ":") + params[p.slice(1)] = cur.split("/").filter(Boolean)[i]; }); - if (currentView) - currentView.destroy(); - if (Router.params) - Router.params(params); - currentView = Render(() => { - try { - return isFunc(component) ? component(params) : component; - } catch (e) { - return Tag("div", { class: "p-4 text-error" }, "Error loading view"); - } - }); - outlet.appendChild(currentView.container); + Router.params(params); + currentView = Render(() => isFunc(route.component) ? route.component(params) : route.component); + hook.replaceChildren(currentView.container); } }); - return outlet; + return hook; }; Router.params = $({}); - Router.to = (path) => window.location.hash = path.replace(/^#?\/?/, "#/"); + Router.to = (p) => window.location.hash = p.replace(/^#?\/?/, "#/"); Router.back = () => window.history.back(); Router.path = () => window.location.hash.replace(/^#/, "") || "/"; - var Mount = (component, target) => { - const targetEl = typeof target === "string" ? doc.querySelector(target) : target; - if (!targetEl) + var Mount = (comp, target) => { + const t = typeof target === "string" ? doc.querySelector(target) : target; + if (!t) return; - if (MOUNTED_NODES.has(targetEl)) - MOUNTED_NODES.get(targetEl).destroy(); - const instance = Render(isFunc(component) ? component : () => component); - targetEl.replaceChildren(instance.container); - MOUNTED_NODES.set(targetEl, instance); - return instance; + 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; }; - var SigPro = { $, $$, Render, Watch, Tag, If, For, Router, Mount }; + var SigPro = Object.freeze({ $, $$, Watch, Tag, Render, If, For, Router, Mount, onMount, onUnmount, Batch }); if (typeof window !== "undefined") { - assign(window, SigPro); - const tags = `div span p h1 h2 h3 h4 h5 h6 br hr section article aside nav main header footer address ul ol li dl dt dd a em strong small i b u mark time sub sup pre code blockquote details summary dialog form label input textarea select button option fieldset legend table thead tbody tfoot tr th td caption img video audio canvas svg iframe picture source progress meter`.split(" "); - tags.forEach((tag) => { - const helper = tag[0].toUpperCase() + tag.slice(1); - if (!(helper in window)) - window[helper] = (p, c) => Tag(tag, p, c); - }); - window.SigPro = Object.freeze(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".split(" ").forEach((t) => window[t[0].toUpperCase() + t.slice(1)] = (p, c) => SigPro.Tag(t, p, c)); } })(); diff --git a/dist/sigpro.min.js b/dist/sigpro.min.js index c5918ba..c16bf71 100644 --- a/dist/sigpro.min.js +++ b/dist/sigpro.min.js @@ -1 +1 @@ -(()=>{var{defineProperty:P,getOwnPropertyNames:F,getOwnPropertyDescriptor:G}=Object,J=Object.prototype.hasOwnProperty;var D=new WeakMap,Q=(e)=>{var t=D.get(e),o;if(t)return t;if(t=P({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function")F(e).map((c)=>!J.call(t,c)&&P(t,c,{get:()=>e[c],enumerable:!(o=G(e,c))||o.enumerable}));return D.set(e,t),t};var X=(e,t)=>{for(var o in t)P(e,o,{get:t[o],enumerable:!0,configurable:!0,set:(c)=>t[o]=()=>c})};var Y={};X(Y,{Watch:()=>v,Tag:()=>E,Router:()=>_,Render:()=>g,Mount:()=>z,If:()=>I,For:()=>$,$$:()=>j,$:()=>L});var w=null,m=null,S=new Set,C=!1,T=new WeakMap,O=document,N=Array.isArray,V=Object.assign,q=(e)=>O.createElement(e),y=(e)=>O.createTextNode(String(e??"")),h=(e)=>typeof e==="function",B=(e)=>typeof e==="object"&&e!==null,M=(e,t)=>{let o=w;w=e;try{return t()}finally{w=o}},A=(e)=>{if(e._cleanups)e._cleanups.forEach((t)=>t()),e._cleanups.clear();e.childNodes?.forEach(A)},H=()=>{if(C)return;C=!0;while(S.size>0){let e=Array.from(S).sort((t,o)=>(t.depth||0)-(o.depth||0));S.clear();for(let t of e)if(!t._deleted)t()}C=!1},W=(e)=>{if(w&&!w._deleted)e.add(w),w._deps.add(e)},k=(e)=>{if(e.forEach((t)=>{if(t===w||t._deleted)return;if(t._isComputed){if(t.markDirty(),t._subs)k(t._subs)}else S.add(t)}),!C)queueMicrotask(H)},g=(e)=>{let t=new Set,o=m,c=q("div");c.style.display="contents",m={cleanups:t};let s=(n)=>{if(!n)return;if(n._isRuntime)t.add(n.destroy),c.appendChild(n.container);else if(N(n))n.forEach(s);else c.appendChild(n instanceof Node?n:y(n))};try{s(e({onCleanup:(n)=>t.add(n)}))}finally{m=o}return{_isRuntime:!0,container:c,destroy:()=>{t.forEach((n)=>n()),A(c),c.remove()}}},L=(e,t=null)=>{let o=new Set;if(h(e)){let s,n=!0,r=()=>{if(r._deleted)return;r._deps.forEach((a)=>a.delete(r)),r._deps.clear(),M(r,()=>{let a=e();if(!Object.is(s,a)||n)s=a,n=!1,k(o)})};if(V(r,{_deps:new Set,_isComputed:!0,_subs:o,_deleted:!1,markDirty:()=>n=!0,stop:()=>{r._deleted=!0,r._deps.forEach((a)=>a.delete(r)),o.clear()}}),m)m.cleanups.add(r.stop);return()=>{if(n)r();return W(o),s}}let c=e;if(t)try{let s=localStorage.getItem(t);if(s!==null)c=JSON.parse(s)}catch(s){console.warn("SigPro Storage Lock",s)}return(...s)=>{if(s.length){let n=h(s[0])?s[0](c):s[0];if(!Object.is(c,n)){if(c=n,t)localStorage.setItem(t,JSON.stringify(c));k(o)}}return W(o),c}},j=(e,t=new WeakMap)=>{if(!B(e))return e;if(t.has(e))return t.get(e);let o={},c=new Proxy(e,{get(s,n){if(w)W(o[n]??=new Set);let r=Reflect.get(s,n);return B(r)?j(r,t):r},set(s,n,r){if(Object.is(s[n],r))return!0;let a=Reflect.set(s,n,r);if(o[n])k(o[n]);return a}});return t.set(e,c),c},v=(e,t)=>{let o=N(e),c=o?t:e;if(!h(c))return()=>{};let s=m,n=()=>{if(n._deleted)return;n._deps.forEach((a)=>a.delete(n)),n._deps.clear(),n._cleanups.forEach((a)=>a()),n._cleanups.clear();let r=m;n.depth=w?w.depth+1:0,M(n,()=>{if(m={cleanups:n._cleanups},o)M(null,c),e.forEach((a)=>h(a)&&a());else c();m=r})};if(V(n,{_deps:new Set,_cleanups:new Set,_deleted:!1,stop:()=>{if(n._deleted)return;if(n._deleted=!0,S.delete(n),n._deps.forEach((r)=>r.delete(n)),n._cleanups.forEach((r)=>r()),s)s.cleanups.delete(n.stop)}}),s)s.cleanups.add(n.stop);return n(),n.stop},E=(e,t={},o=[])=>{if(t instanceof Node||N(t)||!B(t))o=t,t={};let s=/^(svg|path|circle|rect|line|polyline|polygon|g|defs|text|tspan|use)$/.test(e)?O.createElementNS("http://www.w3.org/2000/svg",e):q(e);s._cleanups=new Set,s.onUnmount=(i)=>s._cleanups.add(i);let n=["disabled","checked","required","readonly","selected","multiple","autofocus"],r=(i,l)=>{let d=(i==="src"||i==="href")&&String(l).toLowerCase().includes("javascript:")?"#":l;if(n.includes(i))s[i]=!!d,d?s.setAttribute(i,""):s.removeAttribute(i);else d==null?s.removeAttribute(i):s.setAttribute(i,d)};for(let[i,l]of Object.entries(t)){if(i==="ref"){h(l)?l(s):l.current=s;continue}let d=h(l);if(i.startsWith("on")){let p=i.slice(2).toLowerCase().split(".")[0];s.addEventListener(p,l),s._cleanups.add(()=>s.removeEventListener(p,l))}else if(d){if(s._cleanups.add(v(()=>{let p=l();i==="class"?s.className=p||"":r(i,p)})),["INPUT","TEXTAREA","SELECT"].includes(s.tagName)&&(i==="value"||i==="checked")){let p=i==="checked"?"change":"input",f=(u)=>l(u.target[i]);s.addEventListener(p,f),s._cleanups.add(()=>s.removeEventListener(p,f))}}else r(i,l)}let a=(i)=>{if(N(i))return i.forEach(a);if(h(i)){let l=y("");s.appendChild(l);let d=[];s._cleanups.add(v(()=>{let p=i(),f=(N(p)?p:[p]).map((u)=>u?._isRuntime?u.container:u instanceof Node?u:y(u));d.forEach((u)=>{A(u),u.remove()}),f.forEach((u)=>l.parentNode?.insertBefore(u,l)),d=f}))}else s.appendChild(i instanceof Node?i:y(i))};return a(o),s},I=(e,t,o=null,c=null)=>{let s=y(""),n=E("div",{style:"display:contents"},[s]),r=null,a=null;return v(()=>{let i=!!(h(e)?e():e);if(i===a)return;a=i;let l=()=>{if(r)r.destroy();r=null};if(r&&!i&&c?.out)c.out(r.container,l);else l();let d=i?t:o;if(d){if(r=g(()=>h(d)?d():d),n.insertBefore(r.container,s),i&&c?.in)c.in(r.container)}}),n},$=(e,t,o,c="div",s={style:"display:contents"})=>{let n=y(""),r=E(c,s,[n]),a=new Map;return v(()=>{let i=(h(e)?e():e)||[],l=new Map,d=[];for(let f=0;f{A(b),b.remove()}}:g(()=>b)}a.delete(x),l.set(x,R),d.push(x)}a.forEach((f)=>f.destroy());let p=n;for(let f=d.length-1;f>=0;f--){let u=l.get(d[f]);if(u.container.nextSibling!==p)r.insertBefore(u.container,p);p=u.container}a=l}),r},_=(e)=>{let t=L(window.location.hash.replace(/^#/,"")||"/");window.addEventListener("hashchange",()=>t(window.location.hash.replace(/^#/,"")||"/"));let o=E("div",{class:"router-transition"}),c=null;return v([t],async()=>{let s=t(),n=e.find((r)=>{let a=r.path.split("/").filter(Boolean),i=s.split("/").filter(Boolean);return a.length===i.length&&a.every((l,d)=>l.startsWith(":")||l===i[d])})||e.find((r)=>r.path==="*");if(n){let r=n.component;if(h(r)&&r.toString().includes("import"))r=(await r()).default||await r();let a={};if(n.path.split("/").filter(Boolean).forEach((i,l)=>{if(i.startsWith(":"))a[i.slice(1)]=s.split("/").filter(Boolean)[l]}),c)c.destroy();if(_.params)_.params(a);c=g(()=>{try{return h(r)?r(a):r}catch(i){return E("div",{class:"p-4 text-error"},"Error loading view")}}),o.appendChild(c.container)}}),o};_.params=L({});_.to=(e)=>window.location.hash=e.replace(/^#?\/?/,"#/");_.back=()=>window.history.back();_.path=()=>window.location.hash.replace(/^#/,"")||"/";var z=(e,t)=>{let o=typeof t==="string"?O.querySelector(t):t;if(!o)return;if(T.has(o))T.get(o).destroy();let c=g(h(e)?e:()=>e);return o.replaceChildren(c.container),T.set(o,c),c},U={$:L,$$:j,Render:g,Watch:v,Tag:E,If:I,For:$,Router:_,Mount:z};if(typeof window<"u")V(window,U),"div span p h1 h2 h3 h4 h5 h6 br hr section article aside nav main header footer address ul ol li dl dt dd a em strong small i b u mark time sub sup pre code blockquote details summary dialog form label input textarea select button option fieldset legend table thead tbody tfoot tr th td caption img video audio canvas svg iframe picture source progress meter".split(" ").forEach((t)=>{let o=t[0].toUpperCase()+t.slice(1);if(!(o in window))window[o]=(c,s)=>E(t,c,s)}),window.SigPro=Object.freeze(U);})(); +(()=>{var{defineProperty:j,getOwnPropertyNames:H,getOwnPropertyDescriptor:X}=Object,Y=Object.prototype.hasOwnProperty;var I=new WeakMap,Z=(e)=>{var n=I.get(e),t;if(n)return n;if(n=j({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function")H(e).map((o)=>!Y.call(n,o)&&j(n,o,{get:()=>e[o],enumerable:!(t=X(e,o))||t.enumerable}));return I.set(e,n),n};var ee=(e,n)=>{for(var t in n)j(e,t,{get:n[t],enumerable:!0,configurable:!0,set:(o)=>n[t]=()=>o})};var oe={};ee(oe,{onUnmount:()=>g,onMount:()=>k,Watch:()=>C,Tag:()=>T,Router:()=>S,Render:()=>b,Mount:()=>K,If:()=>G,For:()=>J,Batch:()=>F,$$:()=>U,$:()=>M});var m=(e)=>typeof e==="function",$=(e)=>e&&typeof e==="object",v=Array.isArray,E=typeof document<"u"?document:null,W=(e)=>e?._isRuntime?e.container:e instanceof Node?e:E.createTextNode(e==null?"":String(e)),d=null,_=null,x=!1,A=0,R=new Set,B=new WeakMap,P=Symbol("iter"),L=new WeakMap,N=(e)=>{if(!e||e._disposed)return;e._disposed=!0;let n=[e];while(n.length){let t=n.pop();if(t._cleanups)t._cleanups.forEach((o)=>o()),t._cleanups.clear();if(t._children)t._children.forEach((o)=>n.push(o)),t._children.clear();if(t._deps)t._deps.forEach((o)=>o.delete(t)),t._deps.clear()}},k=(e)=>{if(_)(_._mounts||=[]).push(e)},g=(e)=>{if(_)(_._cleanups||=new Set).add(e)},te=(e)=>{let n=d;d=null;try{return e()}finally{d=n}},O=(e,n=!1)=>{let t=()=>{if(t._disposed)return;if(t._deps)t._deps.forEach((s)=>s.delete(t));if(t._cleanups)t._cleanups.forEach((s)=>s()),t._cleanups.clear();let o=d,c=_;d=_=t;try{return t._result=e()}catch(s){console.error("[SigPro]",s)}finally{d=o,_=c}};if(t._deps=t._cleanups=t._children=null,t._disposed=!1,t._isComputed=n,t._depth=d?d._depth+1:0,t._mounts=[],t._parent=_,_)(_._children||=new Set).add(t);return t},z=()=>{if(x)return;x=!0;let e=Array.from(R).sort((n,t)=>n._depth-t._depth);R.clear();for(let n of e)if(!n._disposed)n();x=!1},F=(e)=>{A++;try{return e()}finally{if(A--,A===0&&R.size>0&&!x)z()}},w=(e,n=!1)=>{if(!n&&d&&!d._disposed)e.add(d),(d._deps||=new Set).add(e);else if(n){let t=!1;if(e.forEach((o)=>{if(o===d||o._disposed)return;if(o._isComputed){if(o._dirty=!0,o._subs)w(o._subs,!0)}else R.add(o),t=!0}),t&&!x&&A===0)queueMicrotask(z)}},M=(e,n=null)=>{let t=new Set;if(m(e)){let o,c=!0,s=()=>{if(c){let i=d;d=s;try{let r=e();if(!Object.is(o,r))o=r,c=!1,w(t,!0)}finally{d=i}}return w(t),o};if(s._isComputed=!0,s._subs=t,s._dirty=!0,s._deps=null,s._disposed=!1,s.markDirty=()=>{c=!0},s.stop=()=>{if(s._disposed=!0,s._deps)s._deps.forEach((i)=>i.delete(s)),s._deps.clear();t.clear()},_)g(s.stop);return s}if(n)try{e=JSON.parse(localStorage.getItem(n))??e}catch(o){}return(...o)=>{if(o.length){let c=m(o[0])?o[0](e):o[0];if(!Object.is(e,c)){if(e=c,n)localStorage.setItem(n,JSON.stringify(e));w(t,!0)}}return w(t),e}},U=(e)=>{if(!$(e))return e;if(B.has(e))return B.get(e);let n=new Map,t=(c)=>{let s=n.get(c);if(!s)n.set(c,s=new Set);return s},o=new Proxy(e,{get(c,s){return w(t(s)),U(c[s])},set(c,s,i){let r=!(s in c);if(!Object.is(c[s],i)){if(c[s]=i,w(t(s),!0),r)w(t(P),!0)}return!0},deleteProperty(c,s){let i=Reflect.deleteProperty(c,s);if(i)w(t(s),!0),w(t(P),!0);return i},ownKeys(c){return w(t(P)),Reflect.ownKeys(c)}});return B.set(e,o),o},C=(e,n)=>{if(n===void 0){let o=O(e);return o(),()=>N(o)}let t=O(()=>{let o=Array.isArray(e)?e.map((c)=>c()):e();te(()=>n(o))});return t(),()=>N(t)},D=(e)=>{if(e._cleanups)e._cleanups.forEach((n)=>n()),e._cleanups.clear();if(e._ownerEffect)N(e._ownerEffect);if(e.childNodes)e.childNodes.forEach(D)},ne=/^\s*(javascript|data|vbscript):/i,se=(e)=>e==="src"||e==="href"||e.startsWith("on"),V=(e,n)=>{if(n==null||n===!1)return null;if(se(e)){let t=String(n);if(ne.test(t))return console.warn(`[SigPro] Bloqueado protocolo peligroso en ${e}`),"#"}return n},T=(e,n={},t=[])=>{if(n instanceof Node||v(n)||!$(n))t=n,n={};if(m(e)){let i={_mounts:[],_cleanups:new Set},r=O(()=>{let l=e(n,{children:t,emit:(h,...u)=>n[`on${h[0].toUpperCase()}${h.slice(1)}`]?.(...u)});return r._result=l,l});r();let a=r._result;if(a==null)return null;let f=a instanceof Node||v(a)&&a.every((l)=>l instanceof Node)?a:E.createTextNode(String(a)),p=(l)=>{if($(l)&&!l._isRuntime)l._mounts=r._mounts||[],l._cleanups=r._cleanups||new Set,l._ownerEffect=r};return v(f)?f.forEach(p):p(f),f}let o=/^(svg|path|circle|rect|line|polyline|polygon|g|defs|text|tspan|use)$/.test(e),c=o?E.createElementNS("http://www.w3.org/2000/svg",e):E.createElement(e);c._cleanups=new Set;for(let i in n){if(!n.hasOwnProperty(i))continue;let r=n[i];if(i==="ref"){m(r)?r(c):r.current=c;continue}if(i.startsWith("on")){let a=i.slice(2).toLowerCase();c.addEventListener(a,r);let f=()=>c.removeEventListener(a,r);c._cleanups.add(f),g(f)}else if(m(r)){let a=O(()=>{let f=V(i,r());if(i==="class")c.className=f||"";else if(f==null)c.removeAttribute(i);else if(i in c&&!o)c[i]=f;else c.setAttribute(i,f===!0?"":f)});if(a(),c._cleanups.add(()=>N(a)),g(()=>N(a)),/^(INPUT|TEXTAREA|SELECT)$/.test(c.tagName)&&(i==="value"||i==="checked")){let f=i==="checked"?"change":"input";c.addEventListener(f,(p)=>r(p.target[i]))}}else{let a=V(i,r);if(a!=null)if(i in c&&!o)c[i]=a;else c.setAttribute(i,a===!0?"":a)}}let s=(i)=>{if(v(i))return i.forEach(s);if(m(i)){let r=E.createTextNode("");c.appendChild(r);let a=[],f=O(()=>{let p=i(),l=(v(p)?p:[p]).map(W);a.forEach((u)=>{if(u._isRuntime)u.destroy();else D(u);if(u.parentNode)u.remove()});let h=r;for(let u=l.length-1;u>=0;u--){let y=l[u];if(y.parentNode!==h.parentNode)h.parentNode?.insertBefore(y,h);if(y._mounts)y._mounts.forEach((Q)=>Q());h=y}a=l});f(),c._cleanups.add(()=>N(f)),g(()=>N(f))}else{let r=W(i);if(c.appendChild(r),r._mounts)r._mounts.forEach((a)=>a())}};return s(t),c},b=(e)=>{let n=new Set,t=[],o=_,c=d,s=E.createElement("div");s.style.display="contents",s.setAttribute("role","presentation"),_={_cleanups:n,_mounts:t},d=null;let i=(r)=>{if(!r)return;if(r._isRuntime)n.add(r.destroy),s.appendChild(r.container);else if(v(r))r.forEach(i);else s.appendChild(r instanceof Node?r:E.createTextNode(String(r==null?"":r)))};try{i(e({onCleanup:(r)=>n.add(r)}))}finally{_=o,d=c}return t.forEach((r)=>r()),{_isRuntime:!0,container:s,destroy:()=>{n.forEach((r)=>r()),D(s),s.remove()}}},G=(e,n,t=null)=>{let o=E.createTextNode(""),c=T("div",{style:"display:contents"},[o]),s=null;return C(()=>!!(m(e)?e():e),(i)=>{if(s)s.destroy(),s=null;let r=i?n:t;if(r)s=b(()=>m(r)?r():r),c.insertBefore(s.container,o)}),g(()=>s?.destroy()),c},J=(e,n,t)=>{let o=E.createTextNode(""),c=T("div",{style:"display:contents"},[o]),s=new Map;return C(()=>(m(e)?e():e)||[],(i)=>{let r=new Map,a=[],f=i||[];for(let l=0;ln(h,l));else s.delete(u);r.set(u,y),a.push(y)}s.forEach((l)=>l.destroy());let p=o;for(let l=a.length-1;l>=0;l--){let u=a[l].container;if(u.nextSibling!==p)c.insertBefore(u,p);p=u}s=r}),c},S=(e)=>{let n=()=>window.location.hash.slice(1)||"/",t=M(n()),o=()=>t(n());window.addEventListener("hashchange",o),g(()=>window.removeEventListener("hashchange",o));let c=T("div",{class:"router-hook"}),s=null;return C([t],()=>{let i=t(),r=e.find((a)=>{let f=a.path.split("/").filter(Boolean),p=i.split("/").filter(Boolean);return f.length===p.length&&f.every((l,h)=>l[0]===":"||l===p[h])})||e.find((a)=>a.path==="*");if(r){s?.destroy();let a={};r.path.split("/").filter(Boolean).forEach((f,p)=>{if(f[0]===":")a[f.slice(1)]=i.split("/").filter(Boolean)[p]}),S.params(a),s=b(()=>m(r.component)?r.component(a):r.component),c.replaceChildren(s.container)}}),c};S.params=M({});S.to=(e)=>window.location.hash=e.replace(/^#?\/?/,"#/");S.back=()=>window.history.back();S.path=()=>window.location.hash.replace(/^#/,"")||"/";var K=(e,n)=>{let t=typeof n==="string"?E.querySelector(n):n;if(!t)return;if(L.has(t))L.get(t).destroy();let o=b(m(e)?e:()=>e);return t.replaceChildren(o.container),L.set(t,o),o},q=Object.freeze({$:M,$$:U,Watch:C,Tag:T,Render:b,If:G,For:J,Router:S,Mount:K,onMount:k,onUnmount:g,Batch:F});if(typeof window<"u")Object.assign(window,q),"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((e)=>window[e[0].toUpperCase()+e.slice(1)]=(n,t)=>q.Tag(e,n,t));})(); diff --git a/docs/sigpro.js b/docs/sigpro.js index d7b2776..2eaf487 100644 --- a/docs/sigpro.js +++ b/docs/sigpro.js @@ -30,6 +30,8 @@ // index.js var exports_sigpro = {}; __export(exports_sigpro, { + onUnmount: () => onUnmount, + onMount: () => onMount, Watch: () => Watch, Tag: () => Tag, Router: () => Router, @@ -37,80 +39,407 @@ Mount: () => Mount, If: () => If, For: () => For, + Batch: () => Batch, $$: () => $$, $: () => $ }); // sigpro.js - var activeEffect = null; - var currentOwner = null; - var effectQueue = new Set; - var isFlushing = false; - var MOUNTED_NODES = new WeakMap; - var doc = document; - var isArr = Array.isArray; - var assign = Object.assign; - var createEl = (t) => doc.createElement(t); - var createText = (t) => doc.createTextNode(String(t ?? "")); var isFunc = (f) => typeof f === "function"; - var isObj = (o) => typeof o === "object" && o !== null; - var runWithContext = (effect, callback) => { - const previousEffect = activeEffect; - activeEffect = effect; + var isObj = (o) => o && typeof o === "object"; + var isArr = Array.isArray; + var doc = typeof document !== "undefined" ? document : null; + var ensureNode = (n) => n?._isRuntime ? n.container : n instanceof Node ? n : doc.createTextNode(n == null ? "" : String(n)); + var activeEffect = null; + var activeOwner = null; + var isFlushing = false; + var batchDepth = 0; + var effectQueue = new Set; + var proxyCache = new WeakMap; + var ITER = Symbol("iter"); + var MOUNTED_NODES = new WeakMap; + var dispose = (eff) => { + if (!eff || eff._disposed) + return; + eff._disposed = true; + const stack = [eff]; + while (stack.length) { + const e = stack.pop(); + if (e._cleanups) { + e._cleanups.forEach((fn) => fn()); + e._cleanups.clear(); + } + if (e._children) { + e._children.forEach((child) => stack.push(child)); + e._children.clear(); + } + if (e._deps) { + e._deps.forEach((depSet) => depSet.delete(e)); + e._deps.clear(); + } + } + }; + var onMount = (fn) => { + if (activeOwner) + (activeOwner._mounts ||= []).push(fn); + }; + var onUnmount = (fn) => { + if (activeOwner) + (activeOwner._cleanups ||= new Set).add(fn); + }; + var untrack = (fn) => { + const p = activeEffect; + activeEffect = null; try { - return callback(); + return fn(); } finally { - activeEffect = previousEffect; + activeEffect = p; } }; - var cleanupNode = (node) => { - if (node._cleanups) { - node._cleanups.forEach((dispose) => dispose()); - node._cleanups.clear(); - } - node.childNodes?.forEach(cleanupNode); + var createEffect = (fn, isComputed = false) => { + const effect = () => { + if (effect._disposed) + return; + if (effect._deps) + effect._deps.forEach((s) => s.delete(effect)); + if (effect._cleanups) { + effect._cleanups.forEach((c) => c()); + effect._cleanups.clear(); + } + const prevEffect = activeEffect; + const prevOwner = activeOwner; + activeEffect = activeOwner = effect; + try { + return effect._result = fn(); + } catch (e) { + console.error("[SigPro]", e); + } finally { + activeEffect = prevEffect; + activeOwner = prevOwner; + } + }; + effect._deps = effect._cleanups = effect._children = null; + effect._disposed = false; + effect._isComputed = isComputed; + effect._depth = activeEffect ? activeEffect._depth + 1 : 0; + effect._mounts = []; + effect._parent = activeOwner; + if (activeOwner) + (activeOwner._children ||= new Set).add(effect); + return effect; }; - var flushEffects = () => { + var flush = () => { if (isFlushing) return; isFlushing = true; - while (effectQueue.size > 0) { - const sortedEffects = Array.from(effectQueue).sort((a, b) => (a.depth || 0) - (b.depth || 0)); - effectQueue.clear(); - for (const effect of sortedEffects) { - if (!effect._deleted) - effect(); - } - } + const sorted = Array.from(effectQueue).sort((a, b) => a._depth - b._depth); + effectQueue.clear(); + for (const e of sorted) + if (!e._disposed) + e(); isFlushing = false; }; - var trackSubscription = (subscribers) => { - if (activeEffect && !activeEffect._deleted) { - subscribers.add(activeEffect); - activeEffect._deps.add(subscribers); + var Batch = (fn) => { + batchDepth++; + try { + return fn(); + } finally { + batchDepth--; + if (batchDepth === 0 && effectQueue.size > 0 && !isFlushing) { + flush(); + } } }; - var triggerUpdate = (subscribers) => { - subscribers.forEach((effect) => { - if (effect === activeEffect || effect._deleted) - return; - if (effect._isComputed) { - effect.markDirty(); - if (effect._subs) - triggerUpdate(effect._subs); - } else { - effectQueue.add(effect); + var trackUpdate = (subs, trigger = false) => { + if (!trigger && activeEffect && !activeEffect._disposed) { + subs.add(activeEffect); + (activeEffect._deps ||= new Set).add(subs); + } else if (trigger) { + let hasQueue = false; + subs.forEach((e) => { + if (e === activeEffect || e._disposed) + return; + if (e._isComputed) { + e._dirty = true; + if (e._subs) + trackUpdate(e._subs, true); + } else { + effectQueue.add(e); + hasQueue = true; + } + }); + if (hasQueue && !isFlushing && batchDepth === 0) + queueMicrotask(flush); + } + }; + var $ = (val, key = null) => { + const subs = new Set; + if (isFunc(val)) { + let cache, dirty = true; + const computed = () => { + if (dirty) { + const prev = activeEffect; + activeEffect = computed; + try { + const next = val(); + if (!Object.is(cache, next)) { + cache = next; + dirty = false; + trackUpdate(subs, true); + } + } finally { + activeEffect = prev; + } + } + trackUpdate(subs); + return cache; + }; + computed._isComputed = true; + computed._subs = subs; + computed._dirty = true; + computed._deps = null; + computed._disposed = false; + computed.markDirty = () => { + dirty = true; + }; + computed.stop = () => { + computed._disposed = true; + if (computed._deps) { + computed._deps.forEach((depSet) => depSet.delete(computed)); + computed._deps.clear(); + } + subs.clear(); + }; + if (activeOwner) + onUnmount(computed.stop); + return computed; + } + if (key) + try { + val = JSON.parse(localStorage.getItem(key)) ?? val; + } catch (e) {} + return (...args) => { + if (args.length) { + const next = isFunc(args[0]) ? args[0](val) : args[0]; + if (!Object.is(val, next)) { + val = next; + if (key) + localStorage.setItem(key, JSON.stringify(val)); + trackUpdate(subs, true); + } + } + trackUpdate(subs); + return val; + }; + }; + var $$ = (target) => { + if (!isObj(target)) + return target; + if (proxyCache.has(target)) + return proxyCache.get(target); + const subsMap = new Map; + const getSubs = (k) => { + let s = subsMap.get(k); + if (!s) + subsMap.set(k, s = new Set); + return s; + }; + const proxy = new Proxy(target, { + get(t, k) { + trackUpdate(getSubs(k)); + return $$(t[k]); + }, + set(t, k, v) { + const isNew = !(k in t); + if (!Object.is(t[k], v)) { + t[k] = v; + trackUpdate(getSubs(k), true); + if (isNew) + trackUpdate(getSubs(ITER), true); + } + return true; + }, + deleteProperty(t, k) { + const res = Reflect.deleteProperty(t, k); + if (res) { + trackUpdate(getSubs(k), true); + trackUpdate(getSubs(ITER), true); + } + return res; + }, + ownKeys(t) { + trackUpdate(getSubs(ITER)); + return Reflect.ownKeys(t); } }); - if (!isFlushing) - queueMicrotask(flushEffects); + proxyCache.set(target, proxy); + return proxy; + }; + var Watch = (sources, cb) => { + if (cb === undefined) { + const effect2 = createEffect(sources); + effect2(); + return () => dispose(effect2); + } + const effect = createEffect(() => { + const vals = Array.isArray(sources) ? sources.map((s) => s()) : sources(); + untrack(() => cb(vals)); + }); + effect(); + return () => dispose(effect); + }; + var cleanupNode = (node) => { + if (node._cleanups) { + node._cleanups.forEach((fn) => fn()); + node._cleanups.clear(); + } + if (node._ownerEffect) + dispose(node._ownerEffect); + if (node.childNodes) + node.childNodes.forEach(cleanupNode); + }; + var DANGEROUS_PROTOCOL = /^\s*(javascript|data|vbscript):/i; + var isDangerousAttr = (key) => key === "src" || key === "href" || key.startsWith("on"); + var validateAttr = (key, val) => { + if (val == null || val === false) + return null; + if (isDangerousAttr(key)) { + const sVal = String(val); + if (DANGEROUS_PROTOCOL.test(sVal)) { + console.warn(`[SigPro] Bloqueado protocolo peligroso en ${key}`); + return "#"; + } + } + return val; + }; + var Tag = (tag, props = {}, children = []) => { + if (props instanceof Node || isArr(props) || !isObj(props)) { + children = props; + props = {}; + } + if (isFunc(tag)) { + const ctx = { _mounts: [], _cleanups: new Set }; + const effect = createEffect(() => { + const result2 = tag(props, { + children, + emit: (ev, ...args) => props[`on${ev[0].toUpperCase()}${ev.slice(1)}`]?.(...args) + }); + effect._result = result2; + return result2; + }); + effect(); + const result = effect._result; + if (result == null) + return null; + const node = result instanceof Node || isArr(result) && result.every((n) => n instanceof Node) ? result : doc.createTextNode(String(result)); + const attach = (n) => { + if (isObj(n) && !n._isRuntime) { + n._mounts = effect._mounts || []; + n._cleanups = effect._cleanups || new Set; + n._ownerEffect = effect; + } + }; + isArr(node) ? node.forEach(attach) : attach(node); + return node; + } + 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); + el._cleanups = new Set; + for (let k in props) { + if (!props.hasOwnProperty(k)) + continue; + let v = props[k]; + if (k === "ref") { + isFunc(v) ? v(el) : v.current = el; + continue; + } + if (k.startsWith("on")) { + const ev = k.slice(2).toLowerCase(); + el.addEventListener(ev, v); + const off = () => el.removeEventListener(ev, v); + el._cleanups.add(off); + onUnmount(off); + } else if (isFunc(v)) { + const effect = createEffect(() => { + const val = validateAttr(k, v()); + if (k === "class") + el.className = val || ""; + else if (val == null) + el.removeAttribute(k); + else if (k in el && !isSVG) + el[k] = val; + else + el.setAttribute(k, val === true ? "" : val); + }); + effect(); + el._cleanups.add(() => dispose(effect)); + onUnmount(() => dispose(effect)); + if (/^(INPUT|TEXTAREA|SELECT)$/.test(el.tagName) && (k === "value" || k === "checked")) { + const evType = k === "checked" ? "change" : "input"; + el.addEventListener(evType, (ev) => v(ev.target[k])); + } + } else { + const val = validateAttr(k, v); + if (val != null) { + if (k in el && !isSVG) + el[k] = val; + else + el.setAttribute(k, val === true ? "" : val); + } + } + } + const append = (c) => { + if (isArr(c)) + return c.forEach(append); + if (isFunc(c)) { + const anchor = doc.createTextNode(""); + el.appendChild(anchor); + let currentNodes = []; + const effect = createEffect(() => { + const res = c(); + const next = (isArr(res) ? res : [res]).map(ensureNode); + currentNodes.forEach((n) => { + if (n._isRuntime) + n.destroy(); + else + cleanupNode(n); + if (n.parentNode) + n.remove(); + }); + let ref = anchor; + for (let i = next.length - 1;i >= 0; i--) { + const node = next[i]; + if (node.parentNode !== ref.parentNode) + ref.parentNode?.insertBefore(node, ref); + if (node._mounts) + node._mounts.forEach((fn) => fn()); + ref = node; + } + currentNodes = next; + }); + effect(); + el._cleanups.add(() => dispose(effect)); + onUnmount(() => dispose(effect)); + } else { + const node = ensureNode(c); + el.appendChild(node); + if (node._mounts) + node._mounts.forEach((fn) => fn()); + } + }; + append(children); + return el; }; var Render = (renderFn) => { const cleanups = new Set; - const previousOwner = currentOwner; - const container = createEl("div"); + const mounts = []; + const previousOwner = activeOwner; + const previousEffect = activeEffect; + const container = doc.createElement("div"); container.style.display = "contents"; - currentOwner = { cleanups }; + container.setAttribute("role", "presentation"); + activeOwner = { _cleanups: cleanups, _mounts: mounts }; + activeEffect = null; const processResult = (result) => { if (!result) return; @@ -120,14 +449,16 @@ } else if (isArr(result)) { result.forEach(processResult); } else { - container.appendChild(result instanceof Node ? result : createText(result)); + container.appendChild(result instanceof Node ? result : doc.createTextNode(String(result == null ? "" : result))); } }; try { processResult(renderFn({ onCleanup: (fn) => cleanups.add(fn) })); } finally { - currentOwner = previousOwner; + activeOwner = previousOwner; + activeEffect = previousEffect; } + mounts.forEach((fn) => fn()); return { _isRuntime: true, container, @@ -138,335 +469,103 @@ } }; }; - var $ = (initialValue, storageKey = null) => { - const subscribers = new Set; - if (isFunc(initialValue)) { - let cachedValue, isDirty = true; - const effect = () => { - if (effect._deleted) - return; - effect._deps.forEach((dep) => dep.delete(effect)); - effect._deps.clear(); - runWithContext(effect, () => { - const newValue = initialValue(); - if (!Object.is(cachedValue, newValue) || isDirty) { - cachedValue = newValue; - isDirty = false; - triggerUpdate(subscribers); - } - }); - }; - assign(effect, { - _deps: new Set, - _isComputed: true, - _subs: subscribers, - _deleted: false, - markDirty: () => isDirty = true, - stop: () => { - effect._deleted = true; - effect._deps.forEach((dep) => dep.delete(effect)); - subscribers.clear(); - } - }); - if (currentOwner) - currentOwner.cleanups.add(effect.stop); - return () => { - if (isDirty) - effect(); - trackSubscription(subscribers); - return cachedValue; - }; - } - let value = initialValue; - if (storageKey) { - try { - const saved = localStorage.getItem(storageKey); - if (saved !== null) - value = JSON.parse(saved); - } catch (e) { - console.warn("SigPro Storage Lock", e); - } - } - return (...args) => { - if (args.length) { - const nextValue = isFunc(args[0]) ? args[0](value) : args[0]; - if (!Object.is(value, nextValue)) { - value = nextValue; - if (storageKey) - localStorage.setItem(storageKey, JSON.stringify(value)); - triggerUpdate(subscribers); - } - } - trackSubscription(subscribers); - return value; - }; - }; - var $$ = (object, cache = new WeakMap) => { - if (!isObj(object)) - return object; - if (cache.has(object)) - return cache.get(object); - const keySubscribers = {}; - const proxy = new Proxy(object, { - get(target, key) { - if (activeEffect) - trackSubscription(keySubscribers[key] ??= new Set); - const value = Reflect.get(target, key); - return isObj(value) ? $$(value, cache) : value; - }, - set(target, key, value) { - if (Object.is(target[key], value)) - return true; - const success = Reflect.set(target, key, value); - if (keySubscribers[key]) - triggerUpdate(keySubscribers[key]); - return success; - } - }); - cache.set(object, proxy); - return proxy; - }; - var Watch = (target, callbackFn) => { - const isExplicit = isArr(target); - const callback = isExplicit ? callbackFn : target; - if (!isFunc(callback)) - return () => {}; - const owner = currentOwner; - const runner = () => { - if (runner._deleted) - return; - runner._deps.forEach((dep) => dep.delete(runner)); - runner._deps.clear(); - runner._cleanups.forEach((cleanup) => cleanup()); - runner._cleanups.clear(); - const previousOwner = currentOwner; - runner.depth = activeEffect ? activeEffect.depth + 1 : 0; - runWithContext(runner, () => { - currentOwner = { cleanups: runner._cleanups }; - if (isExplicit) { - runWithContext(null, callback); - target.forEach((dep) => isFunc(dep) && dep()); - } else { - callback(); - } - currentOwner = previousOwner; - }); - }; - assign(runner, { - _deps: new Set, - _cleanups: new Set, - _deleted: false, - stop: () => { - if (runner._deleted) - return; - runner._deleted = true; - effectQueue.delete(runner); - runner._deps.forEach((dep) => dep.delete(runner)); - runner._cleanups.forEach((cleanup) => cleanup()); - if (owner) - owner.cleanups.delete(runner.stop); - } - }); - if (owner) - owner.cleanups.add(runner.stop); - runner(); - return runner.stop; - }; - var 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 element = isSVG ? doc.createElementNS("http://www.w3.org/2000/svg", tag) : createEl(tag); - element._cleanups = new Set; - element.onUnmount = (fn) => element._cleanups.add(fn); - const booleanAttributes = ["disabled", "checked", "required", "readonly", "selected", "multiple", "autofocus"]; - const updateAttribute = (name, value) => { - const sanitized = (name === "src" || name === "href") && String(value).toLowerCase().includes("javascript:") ? "#" : value; - if (booleanAttributes.includes(name)) { - element[name] = !!sanitized; - sanitized ? element.setAttribute(name, "") : element.removeAttribute(name); - } else { - sanitized == null ? element.removeAttribute(name) : element.setAttribute(name, sanitized); - } - }; - for (let [key, value] of Object.entries(props)) { - if (key === "ref") { - isFunc(value) ? value(element) : value.current = element; - continue; - } - const isSignal = isFunc(value); - if (key.startsWith("on")) { - const eventName = key.slice(2).toLowerCase().split(".")[0]; - element.addEventListener(eventName, value); - element._cleanups.add(() => element.removeEventListener(eventName, value)); - } else if (isSignal) { - element._cleanups.add(Watch(() => { - const currentVal = value(); - key === "class" ? element.className = currentVal || "" : updateAttribute(key, currentVal); - })); - if (["INPUT", "TEXTAREA", "SELECT"].includes(element.tagName) && (key === "value" || key === "checked")) { - const event = key === "checked" ? "change" : "input"; - const handler = (e) => value(e.target[key]); - element.addEventListener(event, handler); - element._cleanups.add(() => element.removeEventListener(event, handler)); - } - } else { - updateAttribute(key, value); - } - } - const appendChildNode = (child) => { - if (isArr(child)) - return child.forEach(appendChildNode); - if (isFunc(child)) { - const marker = createText(""); - element.appendChild(marker); - let currentNodes = []; - element._cleanups.add(Watch(() => { - const result = child(); - const nextNodes = (isArr(result) ? result : [result]).map((node) => node?._isRuntime ? node.container : node instanceof Node ? node : createText(node)); - currentNodes.forEach((node) => { - cleanupNode(node); - node.remove(); - }); - nextNodes.forEach((node) => marker.parentNode?.insertBefore(node, marker)); - currentNodes = nextNodes; - })); - } else { - element.appendChild(child instanceof Node ? child : createText(child)); - } - }; - appendChildNode(children); - return element; - }; - var If = (condition, thenVal, otherwiseVal = null, transition = null) => { - const marker = createText(""); - const container = Tag("div", { style: "display:contents" }, [marker]); - let currentView = null, lastState = null; - Watch(() => { - const state = !!(isFunc(condition) ? condition() : condition); - if (state === lastState) - return; - lastState = state; - const dispose = () => { - if (currentView) - currentView.destroy(); + var If = (cond, ifYes, ifNot = null) => { + const anchor = doc.createTextNode(""); + const root = Tag("div", { style: "display:contents" }, [anchor]); + let currentView = null; + Watch(() => !!(isFunc(cond) ? cond() : cond), (show) => { + if (currentView) { + currentView.destroy(); currentView = null; - }; - if (currentView && !state && transition?.out) { - transition.out(currentView.container, dispose); - } else { - dispose(); } - const branch = state ? thenVal : otherwiseVal; - if (branch) { - currentView = Render(() => isFunc(branch) ? branch() : branch); - container.insertBefore(currentView.container, marker); - if (state && transition?.in) - transition.in(currentView.container); + const content = show ? ifYes : ifNot; + if (content) { + currentView = Render(() => isFunc(content) ? content() : content); + root.insertBefore(currentView.container, anchor); } }); - return container; + onUnmount(() => currentView?.destroy()); + return root; }; - var For = (source, renderFn, keyFn, tag = "div", props = { style: "display:contents" }) => { - const marker = createText(""); - const container = Tag(tag, props, [marker]); - let viewCache = new Map; - Watch(() => { - const items = (isFunc(source) ? source() : source) || []; + var For = (src, itemFn, keyFn) => { + const anchor = doc.createTextNode(""); + const root = Tag("div", { style: "display:contents" }, [anchor]); + let cache = new Map; + Watch(() => (isFunc(src) ? src() : src) || [], (items) => { const nextCache = new Map; - const order = []; - for (let i = 0;i < items.length; i++) { - const item = items[i]; - const key = keyFn ? keyFn(item, i) : i; - let view = viewCache.get(key); - if (!view) { - const result = renderFn(item, i); - view = result instanceof Node ? { container: result, destroy: () => { - cleanupNode(result); - result.remove(); - } } : Render(() => result); - } - viewCache.delete(key); + const nextOrder = []; + const newItems = items || []; + for (let i = 0;i < newItems.length; i++) { + const item = newItems[i]; + const key = keyFn ? keyFn(item, i) : item?.id ?? i; + let view = cache.get(key); + if (!view) + view = Render(() => itemFn(item, i)); + else + cache.delete(key); nextCache.set(key, view); - order.push(key); + nextOrder.push(view); } - viewCache.forEach((v) => v.destroy()); - let anchor = marker; - for (let i = order.length - 1;i >= 0; i--) { - const view = nextCache.get(order[i]); - if (view.container.nextSibling !== anchor) { - container.insertBefore(view.container, anchor); - } - anchor = view.container; + cache.forEach((view) => view.destroy()); + let lastRef = anchor; + for (let i = nextOrder.length - 1;i >= 0; i--) { + const view = nextOrder[i]; + const node = view.container; + if (node.nextSibling !== lastRef) + root.insertBefore(node, lastRef); + lastRef = node; } - viewCache = nextCache; + cache = nextCache; }); - return container; + return root; }; var Router = (routes) => { - const currentPath = $(window.location.hash.replace(/^#/, "") || "/"); - window.addEventListener("hashchange", () => currentPath(window.location.hash.replace(/^#/, "") || "/")); - const outlet = Tag("div", { class: "router-transition" }); + const getHash = () => window.location.hash.slice(1) || "/"; + const path = $(getHash()); + const handler = () => path(getHash()); + window.addEventListener("hashchange", handler); + onUnmount(() => window.removeEventListener("hashchange", handler)); + const hook = Tag("div", { class: "router-hook" }); let currentView = null; - Watch([currentPath], async () => { - const path = currentPath(); + Watch([path], () => { + const cur = path(); const route = routes.find((r) => { - const routeParts = r.path.split("/").filter(Boolean); - const pathParts = path.split("/").filter(Boolean); - return routeParts.length === pathParts.length && routeParts.every((part, i) => part.startsWith(":") || part === pathParts[i]); + const p1 = r.path.split("/").filter(Boolean); + const p2 = cur.split("/").filter(Boolean); + return p1.length === p2.length && p1.every((p, i) => p[0] === ":" || p === p2[i]); }) || routes.find((r) => r.path === "*"); if (route) { - let component = route.component; - if (isFunc(component) && component.toString().includes("import")) { - component = (await component()).default || await component(); - } + currentView?.destroy(); const params = {}; - route.path.split("/").filter(Boolean).forEach((part, i) => { - if (part.startsWith(":")) - params[part.slice(1)] = path.split("/").filter(Boolean)[i]; + route.path.split("/").filter(Boolean).forEach((p, i) => { + if (p[0] === ":") + params[p.slice(1)] = cur.split("/").filter(Boolean)[i]; }); - if (currentView) - currentView.destroy(); - if (Router.params) - Router.params(params); - currentView = Render(() => { - try { - return isFunc(component) ? component(params) : component; - } catch (e) { - return Tag("div", { class: "p-4 text-error" }, "Error loading view"); - } - }); - outlet.appendChild(currentView.container); + Router.params(params); + currentView = Render(() => isFunc(route.component) ? route.component(params) : route.component); + hook.replaceChildren(currentView.container); } }); - return outlet; + return hook; }; Router.params = $({}); - Router.to = (path) => window.location.hash = path.replace(/^#?\/?/, "#/"); + Router.to = (p) => window.location.hash = p.replace(/^#?\/?/, "#/"); Router.back = () => window.history.back(); Router.path = () => window.location.hash.replace(/^#/, "") || "/"; - var Mount = (component, target) => { - const targetEl = typeof target === "string" ? doc.querySelector(target) : target; - if (!targetEl) + var Mount = (comp, target) => { + const t = typeof target === "string" ? doc.querySelector(target) : target; + if (!t) return; - if (MOUNTED_NODES.has(targetEl)) - MOUNTED_NODES.get(targetEl).destroy(); - const instance = Render(isFunc(component) ? component : () => component); - targetEl.replaceChildren(instance.container); - MOUNTED_NODES.set(targetEl, instance); - return instance; + 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; }; - var SigPro = { $, $$, Render, Watch, Tag, If, For, Router, Mount }; + var SigPro = Object.freeze({ $, $$, Watch, Tag, Render, If, For, Router, Mount, onMount, onUnmount, Batch }); if (typeof window !== "undefined") { - assign(window, SigPro); - const tags = `div span p h1 h2 h3 h4 h5 h6 br hr section article aside nav main header footer address ul ol li dl dt dd a em strong small i b u mark time sub sup pre code blockquote details summary dialog form label input textarea select button option fieldset legend table thead tbody tfoot tr th td caption img video audio canvas svg iframe picture source progress meter`.split(" "); - tags.forEach((tag) => { - const helper = tag[0].toUpperCase() + tag.slice(1); - if (!(helper in window)) - window[helper] = (p, c) => Tag(tag, p, c); - }); - window.SigPro = Object.freeze(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".split(" ").forEach((t) => window[t[0].toUpperCase() + t.slice(1)] = (p, c) => SigPro.Tag(t, p, c)); } })(); diff --git a/package.json b/package.json index b5b6365..ab1d813 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sigpro", - "version": "1.1.22", + "version": "1.2.0", "type": "module", "license": "MIT", "main": "./dist/sigpro.esm.min.js", diff --git a/sigpro-lite.js b/sigpro-lite.js deleted file mode 100644 index 6a0140e..0000000 --- a/sigpro-lite.js +++ /dev/null @@ -1,232 +0,0 @@ -// ============================================================ -// SIGPRO CORE · Simple, Fast, Memory‑Safe -// ============================================================ -let currentContext = null - -const getContext = () => currentContext -const runInContext = (ctx, fn) => { - const prev = currentContext - currentContext = ctx - try { return fn() } - finally { currentContext = prev } -} - -const onCleanup = (fn) => { - const ctx = getContext() - if (!ctx) throw new Error('onCleanup must be called within a reactive root') - ctx.cleanups.add(fn) -} - -const onMount = (fn) => { - const ctx = getContext() - if (!ctx) throw new Error('onMount must be called within a reactive root') - queueMicrotask(() => runInContext(ctx, fn)) -} - -const createRoot = (fn) => { - const ctx = { cleanups: new Set(), parent: currentContext } - return runInContext(ctx, () => { - const result = fn(ctx) - const destroy = () => { ctx.cleanups.forEach(c => c()); ctx.cleanups.clear() } - if (result instanceof Node) result._destroy = destroy - return result - }) -} - -const createComponent = (fn) => (...args) => { - const parent = getContext() - const ctx = { cleanups: new Set(), parent } - if (parent) parent.cleanups.add(() => { ctx.cleanups.forEach(c => c()); ctx.cleanups.clear() }) - return runInContext(ctx, () => fn(...args)) -} - -const createSignal = (initialValue) => { - const subscribers = new Set() - let value = initialValue - const signal = (...args) => { - if (args.length === 0) { - const ctx = getContext() - if (ctx?.activeEffect) { - subscribers.add(ctx.activeEffect) - ctx.activeEffect.deps.add(subscribers) - } - return value - } - const next = typeof args[0] === 'function' ? args[0](value) : args[0] - if (!Object.is(value, next)) { - value = next - ;[...subscribers].forEach(e => e.run()) - } - return value - } - return signal -} - -const createEffect = (fn) => { - const ctx = getContext() - if (!ctx) throw new Error('createEffect must be called within a reactive root') - const effect = { - deps: new Set(), - run: () => { - effect.deps.forEach(d => d.delete(effect)) - effect.deps.clear() - const prev = ctx.activeEffect - ctx.activeEffect = effect - try { fn() } - finally { ctx.activeEffect = prev } - } - } - onCleanup(() => { - effect.deps.forEach(d => d.delete(effect)) - effect.deps.clear() - }) - effect.run() -} - -const mount = (component, selector) => { - const root = document.querySelector(selector) - if (!root) throw new Error(`Selector "${selector}" not found`) - if (root._destroy) root._destroy() - root.innerHTML = '' - const app = createRoot(component) - root.appendChild(app) - root._destroy = app._destroy - return app -} - -// Helper para crear elementos (con soporte SVG) -const h = (tag, props, ...children) => { - const isSVG = /^(svg|path|circle|rect|line|polyline|polygon|g|defs|text|tspan|use)$/i.test(tag) - const el = isSVG - ? document.createElementNS('http://www.w3.org/2000/svg', tag) - : document.createElement(tag) - - if (props) { - Object.entries(props).forEach(([k, v]) => { - if (k.startsWith('on')) { - const event = k.slice(2).toLowerCase() - el.addEventListener(event, v) - onCleanup(() => el.removeEventListener(event, v)) - } else if (k === 'style' && typeof v === 'object') { - Object.assign(el.style, v) - } else { - el.setAttribute(k, v) - } - }) - } - - children.flat().forEach(c => { - if (c instanceof Node) el.appendChild(c) - else el.appendChild(document.createTextNode(String(c ?? ''))) - }) - return el -} - -// ===== IF / FOR / ROUTER (API IDÉNTICA A B) ===== -const If = createComponent((cond, thenFn) => { - const anchor = document.createTextNode('') - const container = h('div', { style: 'display:contents' }, anchor) - let current = null - createEffect(() => { - const show = !!cond() - if (show && !current) { - current = createRoot(() => thenFn()) - container.insertBefore(current, anchor) - } else if (!show && current) { - if (current._destroy) current._destroy() - else current.remove() - current = null - } - }) - onCleanup(() => current?._destroy?.()) - return container -}) - -const For = createComponent((source, itemFn) => { - const container = h('div', { style: 'display:contents' }) - const marker = document.createTextNode('') - container.appendChild(marker) - let cache = new Map() - createEffect(() => { - const items = source() || [] - const newCache = new Map() - const order = [] - for (let i = 0; i < items.length; i++) { - const item = items[i] - const key = (item && typeof item === 'object' && 'id' in item) ? item.id : i - let view = cache.get(key) - if (!view) view = createRoot(() => itemFn(item, i)) - newCache.set(key, view) - order.push(key) - cache.delete(key) - } - cache.forEach(v => v._destroy ? v._destroy() : v.remove?.()) - cache = newCache - let anchor = marker - for (let i = order.length - 1; i >= 0; i--) { - const view = newCache.get(order[i]) - if (view instanceof Node && view.nextSibling !== anchor) - container.insertBefore(view, anchor) - anchor = view - } - }) - onCleanup(() => cache.forEach(v => v._destroy ? v._destroy() : v.remove?.())) - return container -}) - -const Router = createComponent(({ routes }) => { - const getHash = () => window.location.hash.slice(1) || '/' - const path = createSignal(getHash()) - const handler = () => path(getHash()) - window.addEventListener('hashchange', handler) - onCleanup(() => window.removeEventListener('hashchange', handler)) - const outlet = h('div', {}) - let currentView = null - createEffect(() => { - const cur = path() - const match = routes.find(r => { - const rParts = r.path.split('/').filter(Boolean) - const pParts = cur.split('/').filter(Boolean) - return rParts.length === pParts.length && rParts.every((p, i) => p.startsWith(':') || p === pParts[i]) - }) || routes.find(r => r.path === '*') - if (match) { - if (currentView?._destroy) currentView._destroy() - else if (currentView) currentView.remove() - const params = {} - match.path.split('/').filter(Boolean).forEach((p, i) => { - if (p.startsWith(':')) params[p.slice(1)] = cur.split('/').filter(Boolean)[i] - }) - currentView = createRoot(() => match.component(params)) - outlet.appendChild(currentView) - } - }) - onCleanup(() => currentView?._destroy?.()) - return outlet -}) -Router.to = (path) => window.location.hash = path.replace(/^#?\/?/, '#/') -Router.back = () => window.history.back() -Router.path = () => window.location.hash.slice(1) || '/' - -// ===== API PÚBLICA ===== -const SigPro = { - createRoot, createComponent, createSignal, createEffect, - onCleanup, onMount, mount, h, - If, For, Router -} - -// ===== EXPOSICIÓN GLOBAL (OPCIONAL, COMO EN B) ===== -if (typeof window !== 'undefined') { - Object.assign(window, SigPro) - const tags = '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(' ') - tags.forEach(t => { - const helper = t[0].toUpperCase() + t.slice(1) - window[helper] = (props, ...children) => h(t, props, children) - }) -} - -export { - createRoot, createComponent, createSignal, createEffect, - onCleanup, onMount, mount, h, - If, For, Router -} -export default SigPro \ No newline at end of file diff --git a/sigpro.d.ts b/sigpro.d.ts index fe9edc9..2547c40 100644 --- a/sigpro.d.ts +++ b/sigpro.d.ts @@ -1,188 +1,581 @@ -// sigpro.d.ts +/** + * SigPro 1.2.0 + * A minimalistic reactive UI library with fine-grained reactivity, + * deep reactive proxies, and intuitive component composition. + */ -declare const SIG_BRAND: unique symbol; +// ============================================================================ +// Core Reactivity +// ============================================================================ -export interface Signal { - readonly [SIG_BRAND]: true; - (): T; - (value: T): T; - (updater: (prev: T) => T): T; +/** + * A reactive signal that holds a value and automatically tracks dependencies. + * Signals are the foundation of SigPro's reactivity system. + * + * @typeParam T - The type of the value stored in the signal + * + * @example + * // Basic usage + * const count = $(0) + * console.log(count()) // 0 + * count(5) + * console.log(count()) // 5 + * count(c => c + 1) + * + * @example + * // Computed signal + * const double = $(() => count() * 2) + * + * @example + * // Persistent signal (synced with localStorage) + * const name = $("Guest", "user-name") + */ +export function $(value: T, persistentKey?: string): Signal + +/** + * A deeply reactive proxy that wraps an object or array, tracking property access + * and mutations with fine-grained precision. Only effects that depend on changed + * properties will re-run. + * + * @typeParam T - The type of the object/array being wrapped + * + * @example + * const state = $$({ user: { name: 'Ana', age: 30 }, items: [1, 2, 3] }) + * + * // Reading a property (reactive) + * Watch(() => console.log(state.user.name)) // logs 'Ana' + * + * // Mutating a property (triggers dependent effects) + * state.user.name = 'María' + * + * // Adding/deleting properties also notifies iteration dependencies + * state.newProp = true + * delete state.items + * + * // Arrays work with iteration tracking + * Object.keys(state) // tracked via internal symbol + */ +export function $$(target: T): DeepReactive + +/** + * A reactive signal type. Calling the signal returns its current value. + * Passing an argument updates the value. + * + * @typeParam T - The type of the value + */ +export type Signal = { + (): T + (value: T | ((prev: T) => T)): void + + // Internal properties (not meant for direct use) + _isComputed?: boolean + _subs?: Set + _dirty?: boolean + _deps?: Set> + _disposed?: boolean + markDirty?: () => void + stop?: () => void } -export interface Computed { - readonly [SIG_BRAND]: true; - (): T; +/** + * A deeply reactive object where all property access and mutations are tracked. + * Works recursively on nested objects and arrays. + */ +export type DeepReactive = T extends object + ? { + [K in keyof T]: T[K] extends object ? DeepReactive : T[K] + } & { + [Symbol.iterator]?: T extends Iterable ? () => Iterator> : never + } + : T + +/** + * Internal effect representation. + */ +interface Effect { + (): any + _deps: Set> | null + _cleanups: Set<() => void> | null + _children: Set | null + _disposed: boolean + _isComputed: boolean + _depth: number + _mounts: Array<() => void> + _parent: Effect | null + _result?: any + _dirty?: boolean + _subs?: Set } +// ============================================================================ +// Effects and Watching +// ============================================================================ + +/** + * Creates a reactive effect that tracks signal dependencies and re-runs when they change. + * Returns a cleanup function to stop the effect. + * + * @param sources - A signal, array of signals, or a function that reads from signals + * @param callback - Optional callback that receives the current values + * @returns A cleanup function that stops the effect + * + * @example + * // Auto-tracking with a function + * const stop = Watch(() => { + * console.log(`Count is: ${count()}`) + * }) + * + * @example + * // Explicit sources with callback + * Watch([count, name], ([c, n]) => { + * console.log(`Count: ${c}, Name: ${n}`) + * }) + * + * @example + * // Cleanup + * stop() // or call the returned function + */ +export function Watch( + sources: (() => void) | Signal | Array> +): () => void +export function Watch( + sources: Signal | Array>, + callback: (values: T | any[]) => void +): () => void + +/** + * Batches multiple signal updates into a single reactive update cycle. + * Use this when performing many updates in sequence to avoid unnecessary re-renders. + * + * @param fn - Function containing batched updates + * @returns The return value of the batched function + * + * @example + * Batch(() => { + * for (let i = 0; i < 1000; i++) { + * items(prev => [...prev, i]) + * } + * }) + * // Effects will run only once after the batch completes + */ +export function Batch(fn: () => T): T + +/** + * Registers a callback to run when the current component mounts. + * Must be called within a component function or Render context. + * + * @param fn - Function to execute on mount + * + * @example + * const MyComponent = () => { + * onMount(() => console.log('Component mounted')) + * return Div("Hello") + * } + */ +export function onMount(fn: () => void): void + +/** + * Registers a callback to run when the current component unmounts. + * Useful for cleanup (event listeners, intervals, etc.). + * Must be called within a component function or Render context. + * + * @param fn - Function to execute on unmount + * + * @example + * const MyComponent = () => { + * onUnmount(() => console.log('Component unmounted')) + * return Div("Hello") + * } + */ +export function onUnmount(fn: () => void): void + +// ============================================================================ +// Component & Rendering +// ============================================================================ + +/** + * Creates a DOM element or component. The Swiss Army knife of SigPro templating. + * + * @param tag - HTML tag name, SVG tag name, or a component function + * @param props - Element properties/attributes (optional) + * @param children - Child elements (optional) + * @returns A DOM Node or DocumentFragment + * + * @example + * // HTML element + * Tag("div", { class: "container" }, [ + * Tag("h1", "Hello World"), + * Tag("button", { onclick: () => alert('clicked') }, "Click me") + * ]) + * + * @example + * // Component + * const Greeting = ({ name }) => Tag("p", `Hello, ${name}`) + * Tag(Greeting, { name: "Ana" }) + * + * @example + * // Reactive attributes + * Tag("div", { class: () => isActive() ? "active" : "" }) + * + * @example + * // SVG + * Tag("svg", { width: 100, height: 100 }, [ + * Tag("circle", { cx: 50, cy: 50, r: 40, fill: "red" }) + * ]) + */ +export function Tag( + tag: string | ((props: any, ctx: ComponentContext) => any), + props?: Record | Node | Array, + children?: any +): Node + +/** + * Context object passed to component functions. + */ +export interface ComponentContext { + /** Child elements passed to the component */ + children: any + /** Emit an event to the parent component */ + emit: (event: string, ...args: any[]) => void +} + +/** + * Renders a component or template function and returns a runtime instance + * that can be mounted and destroyed. + * + * @param renderFn - Function that returns DOM nodes or components + * @returns A runtime instance with container and destroy method + * + * @example + * const app = Render(() => Div({ class: "app" }, "Hello")) + * document.body.appendChild(app.container) + * + * // Later: app.destroy() + */ +export function Render( + renderFn: (ctx: { onCleanup: (fn: () => void) => void }) => any +): RuntimeInstance + +/** + * A runtime instance returned by Render. + */ export interface RuntimeInstance { - readonly _isRuntime: true; - readonly container: HTMLElement; - destroy(): void; -} - -export interface TransitionOptions { - on?: (el: HTMLElement) => void; - off?: (el: HTMLElement, destroy: () => void) => void; -} - -export interface Route { - path: string; - component: (params: Record) => any; -} - -export interface RenderContext { - onCleanup: (fn: () => void) => void; -} - -export interface TagProps extends Record { - ref?: ((el: HTMLElement) => void) | { current: HTMLElement | null }; - class?: string | (() => string); - style?: string | (() => string); -} - -export type ReactiveObject = { - [K in keyof T]: T[K] extends object ? ReactiveObject : T[K]; -}; - -export function $(val: T, key?: string | null): Signal; -export function $(val: () => T): Computed; - -export function $$(fn: () => T): Computed; - -export function $_(obj: T): ReactiveObject; - -export function untrack(fn: () => T): T; - -export function Watch(cb: () => void): () => void; -export function Watch(cb: () => () => void): () => void; - -export function Render(fn: (ctx: RenderContext) => T): RuntimeInstance; - -export function Tag(tag: string, props?: TagProps | null, children?: any[]): HTMLElement; -export function Tag(tag: string, children?: any[]): HTMLElement; - -export function If( - cond: boolean | (() => boolean), - a: any | (() => any), - b?: any | (() => any) | null, - options?: TransitionOptions -): HTMLElement; - -export function For( - source: T[] | (() => T[]), - renderFn: (item: T, index: number) => any, - keyFn?: (item: T, index: number) => any, - tag?: string, - props?: TagProps -): HTMLElement; - -export function Router(routes: Route[]): HTMLElement; - -export namespace Router { - const params: Signal>; - function to(path: string): void; - function back(): void; - function path(): string; + _isRuntime: true + /** The container element that holds the rendered content */ + container: HTMLDivElement + /** Destroys the instance and cleans up all reactive effects */ + destroy: () => void } +/** + * Mounts a component to a DOM element. + * + * @param component - Component function or element to mount + * @param target - CSS selector string or DOM element + * @returns The runtime instance, or undefined if target not found + * + * @example + * // Mount to element with ID 'app' + * Mount(() => Div("Hello SigPro"), "#app") + * + * @example + * // Mount to existing element + * Mount(MyComponent, document.body) + */ export function Mount( component: (() => any) | any, - target: string | HTMLElement -): RuntimeInstance; + target: string | Element +): RuntimeInstance | undefined -export function Share(key: string, value: T): void; +// ============================================================================ +// Control Flow Components +// ============================================================================ -export function Use(key: string, defaultValue?: T): T | undefined; +/** + * Conditionally renders content based on a reactive condition. + * + * @param cond - Boolean value or signal returning boolean + * @param ifYes - Content to render when condition is true + * @param ifNot - Content to render when condition is false (optional) + * @returns A container element that manages the conditional content + * + * @example + * If($show, + * () => Div("Visible content"), + * () => Div("Hidden state placeholder") + * ) + */ +export function If( + cond: boolean | (() => boolean) | Signal, + ifYes: any | (() => any), + ifNot?: any | (() => any) +): HTMLDivElement -// Funciones JSX (etiquetas globales) -export const Div: (props?: TagProps, children?: any[]) => HTMLElement; -export const Span: (props?: TagProps, children?: any[]) => HTMLElement; -export const P: (props?: TagProps, children?: any[]) => HTMLElement; -export const H1: (props?: TagProps, children?: any[]) => HTMLElement; -export const H2: (props?: TagProps, children?: any[]) => HTMLElement; -export const H3: (props?: TagProps, children?: any[]) => HTMLElement; -export const H4: (props?: TagProps, children?: any[]) => HTMLElement; -export const H5: (props?: TagProps, children?: any[]) => HTMLElement; -export const H6: (props?: TagProps, children?: any[]) => HTMLElement; -export const Button: (props?: TagProps, children?: any[]) => HTMLElement; -export const A: (props?: TagProps, children?: any[]) => HTMLElement; -export const Img: (props?: TagProps, children?: any[]) => HTMLElement; -export const Input: (props?: TagProps, children?: any[]) => HTMLElement; -export const Textarea: (props?: TagProps, children?: any[]) => HTMLElement; -export const Select: (props?: TagProps, children?: any[]) => HTMLElement; -export const Option: (props?: TagProps, children?: any[]) => HTMLElement; -export const Form: (props?: TagProps, children?: any[]) => HTMLElement; -export const Label: (props?: TagProps, children?: any[]) => HTMLElement; -export const Ul: (props?: TagProps, children?: any[]) => HTMLElement; -export const Ol: (props?: TagProps, children?: any[]) => HTMLElement; -export const Li: (props?: TagProps, children?: any[]) => HTMLElement; -export const Table: (props?: TagProps, children?: any[]) => HTMLElement; -export const Tr: (props?: TagProps, children?: any[]) => HTMLElement; -export const Td: (props?: TagProps, children?: any[]) => HTMLElement; -export const Th: (props?: TagProps, children?: any[]) => HTMLElement; -export const Section: (props?: TagProps, children?: any[]) => HTMLElement; -export const Article: (props?: TagProps, children?: any[]) => HTMLElement; -export const Aside: (props?: TagProps, children?: any[]) => HTMLElement; -export const Nav: (props?: TagProps, children?: any[]) => HTMLElement; -export const Header: (props?: TagProps, children?: any[]) => HTMLElement; -export const Footer: (props?: TagProps, children?: any[]) => HTMLElement; -export const Main: (props?: TagProps, children?: any[]) => HTMLElement; +/** + * Renders a list of items efficiently, updating only changed items. + * + * @param src - Array or signal returning array of items + * @param itemFn - Function that renders each item + * @param keyFn - Optional function to generate stable keys for items + * @returns A container element that manages the list + * + * @example + * const items = $([1, 2, 3]) + * For(items, (item, index) => Li(`Item ${item}`), item => item) + */ +export function For( + src: T[] | (() => T[]) | Signal, + itemFn: (item: T, index: number) => any, + keyFn?: (item: T, index: number) => string | number +): HTMLDivElement -export interface SigProAPI { - $: typeof $; - $$: typeof $$; - $_: typeof $_; - untrack: typeof untrack; - Render: typeof Render; - Watch: typeof Watch; - Tag: typeof Tag; - If: typeof If; - For: typeof For; - Router: typeof Router; - Mount: typeof Mount; - Share: typeof Share; - Use: typeof Use; +// ============================================================================ +// Router +// ============================================================================ + +/** + * Hash-based router component for single-page applications. + * + * @param routes - Array of route definitions + * @returns A router container element + * + * @example + * Router([ + * { path: "/", component: HomePage }, + * { path: "/about", component: AboutPage }, + * { path: "/user/:id", component: UserPage }, + * { path: "*", component: NotFoundPage } + * ]) + */ +export function Router(routes: RouteDefinition[]): HTMLDivElement + +export namespace Router { + /** + * Reactive signal containing current route parameters. + * @example + * const params = Router.params() + * console.log(params.id) // from "/user/:id" + */ + export const params: Signal> + + /** + * Navigate to a path. + * @example + * Router.to("/about") + */ + export function to(path: string): void + + /** + * Go back in browser history. + */ + export function back(): void + + /** + * Get the current path. + */ + export function path(): string } -declare const SigPro: SigProAPI; -export default SigPro; +/** + * Route definition for the Router. + */ +export interface RouteDefinition { + /** Path pattern with optional :param placeholders. "*" for catch-all. */ + path: string + /** Component to render when route matches */ + component: any | ((params: Record) => any) +} +// ============================================================================ +// HTML Tag Helpers +// ============================================================================ + +/** + * Convenience functions for creating HTML elements. + * Available globally when the library is loaded in a browser. + * + * @example + * Div({ class: "container" }, [ + * H1("Title"), + * P("Paragraph text"), + * Button({ onclick: handleClick }, "Click me") + * ]) + */ + +/** Creates a `
` element */ +export function Div(props?: Record, ...children: any[]): HTMLDivElement + +/** Creates a `` element */ +export function Span(props?: Record, ...children: any[]): HTMLSpanElement + +/** Creates a `

` element */ +export function P(props?: Record, ...children: any[]): HTMLParagraphElement + +/** Creates an `

` element */ +export function H1(props?: Record, ...children: any[]): HTMLHeadingElement + +/** Creates an `

` element */ +export function H2(props?: Record, ...children: any[]): HTMLHeadingElement + +/** Creates an `

` element */ +export function H3(props?: Record, ...children: any[]): HTMLHeadingElement + +/** Creates an `

` element */ +export function H4(props?: Record, ...children: any[]): HTMLHeadingElement + +/** Creates an `
` element */ +export function H5(props?: Record, ...children: any[]): HTMLHeadingElement + +/** Creates an `
` element */ +export function H6(props?: Record, ...children: any[]): HTMLHeadingElement + +/** Creates a `
` element */ +export function Br(props?: Record): HTMLBRElement + +/** Creates an `
` element */ +export function Hr(props?: Record): HTMLHRElement + +/** Creates a `
` element */ +export function Section(props?: Record, ...children: any[]): HTMLElement + +/** Creates an `
` element */ +export function Article(props?: Record, ...children: any[]): HTMLElement + +/** Creates an `