Actualizar sigpro_work.js

This commit is contained in:
2026-04-07 17:30:26 +02:00
parent 7483700681
commit 1716b0c80c

View File

@@ -34,7 +34,7 @@ const $ = (init, key = null) => {
try { try {
const saved = localStorage.getItem(key); const saved = localStorage.getItem(key);
if (saved != null) val = JSON.parse(saved); if (saved != null) val = JSON.parse(saved);
} catch {} } catch { }
} }
const sig = (...args) => { const sig = (...args) => {
if (args.length) { if (args.length) {
@@ -99,7 +99,7 @@ const $$ = (obj) => {
}; };
const Watch = (cb) => { const Watch = (cb) => {
if (typeof cb !== "function") return () => {}; if (typeof cb !== "function") return () => { };
const owner = currentOwner; const owner = currentOwner;
const runner = () => { const runner = () => {
if (runner._deleted) return; if (runner._deleted) return;
@@ -111,7 +111,7 @@ const Watch = (cb) => {
const prevEffect = activeEffect; const prevEffect = activeEffect;
currentOwner = { cleanups: runner._cleanups, parent: owner }; currentOwner = { cleanups: runner._cleanups, parent: owner };
activeEffect = runner; activeEffect = runner;
try { cb(); } try { cb(); }
finally { finally {
currentOwner = prevOwner; currentOwner = prevOwner;
activeEffect = prevEffect; activeEffect = prevEffect;
@@ -143,7 +143,7 @@ const Tag = (tag, props = {}, children = []) => {
: createEl(tag); : createEl(tag);
el._cleanups = new Set(); el._cleanups = new Set();
el.onUnmount = (fn) => el._cleanups.add(fn); el.onUnmount = (fn) => el._cleanups.add(fn);
const booleanAttrs = ["disabled","checked","required","readonly","selected","multiple","autofocus"]; const booleanAttrs = ["disabled", "checked", "required", "readonly", "selected", "multiple", "autofocus"];
for (let [k, v] of Object.entries(props)) { for (let [k, v] of Object.entries(props)) {
if (k === "ref") { if (k === "ref") {
typeof v === "function" ? v(el) : (v.current = el); typeof v === "function" ? v(el) : (v.current = el);
@@ -199,10 +199,10 @@ const Render = (fn) => {
const container = createEl("div"); const container = createEl("div");
container.style.display = "contents"; container.style.display = "contents";
currentOwner = { currentOwner = {
cleanups, cleanups,
parent: prevOwner, parent: prevOwner,
context: null context: null
}; };
const process = (res) => { const process = (res) => {
@@ -217,10 +217,10 @@ const Render = (fn) => {
} }
}; };
try { try {
process(fn({ onCleanup: (f) => cleanups.add(f) })); process(fn({ onCleanup: (f) => cleanups.add(f) }));
} finally { } finally {
currentOwner = prevOwner; currentOwner = prevOwner;
} }
return { return {
@@ -251,19 +251,43 @@ const Use = (key, defaultValue) => {
return defaultValue; return defaultValue;
}; };
const If = (cond, a, b = null) => { const If = (cond, a, b = null, options = {}) => {
const marker = createText(""); const marker = createText("");
const container = Tag("div", { style: "display:contents" }, [marker]); const container = Tag("div", { style: "display:contents" }, [marker]);
let view = null; let currentView = null;
let lastState = null;
Watch(() => { Watch(() => {
const state = !!(typeof cond === "function" ? cond() : cond); const state = !!(typeof cond === "function" ? cond() : cond);
if (view) view.destroy(); if (state === lastState) return;
lastState = state;
const branch = state ? a : b; const branch = state ? a : b;
const oldView = currentView;
if (oldView) {
if (options.off) {
options.off(oldView.container, () => oldView.destroy());
} else {
oldView.destroy();
}
}
if (branch) { if (branch) {
view = Render(() => typeof branch === "function" ? branch() : branch); currentView = Render(() => typeof branch === "function" ? branch() : branch);
container.insertBefore(view.container, marker); const el = currentView.container;
container.insertBefore(el, marker);
if (options.on) {
requestAnimationFrame(() => {
requestAnimationFrame(() => options.on(el));
});
}
} else {
currentView = null;
} }
}); });
return container; return container;
}; };
@@ -336,7 +360,7 @@ const Mount = (component, target) => {
return instance; return instance;
}; };
const SigPro = { $, Render, Watch, Tag, If, For, Router, Mount, Share, Use }; const SigPro = { $, $$, Render, Watch, Tag, If, For, Router, Mount, Share, Use };
if (typeof window !== "undefined") { if (typeof window !== "undefined") {
Object.assign(window, SigPro); Object.assign(window, SigPro);
@@ -350,5 +374,5 @@ if (typeof window !== "undefined") {
window.SigPro = Object.freeze(SigPro); window.SigPro = Object.freeze(SigPro);
} }
export { $, Render, Watch, Tag, If, For, Router, Mount, Share, Use }; export { $, $$, Render, Watch, Tag, If, For, Router, Mount, Share, Use };
export default SigPro; export default SigPro;