Actualizar sigpro_work.js
This commit is contained in:
@@ -4,6 +4,7 @@ let currentOwner = null;
|
|||||||
const effectQueue = new Set();
|
const effectQueue = new Set();
|
||||||
let isFlushing = false;
|
let isFlushing = false;
|
||||||
const MOUNTED_NODES = new WeakMap();
|
const MOUNTED_NODES = new WeakMap();
|
||||||
|
const reactiveCache = new WeakMap();
|
||||||
|
|
||||||
const doc = document;
|
const doc = document;
|
||||||
const createEl = (t) => doc.createElement(t);
|
const createEl = (t) => doc.createElement(t);
|
||||||
@@ -26,44 +27,19 @@ const cleanupNode = (node) => {
|
|||||||
node.childNodes?.forEach(cleanupNode);
|
node.childNodes?.forEach(cleanupNode);
|
||||||
};
|
};
|
||||||
|
|
||||||
const isSignal = (fn) => typeof fn === "function" && typeof fn.react === "function";
|
const untrack = (fn) => {
|
||||||
|
const prev = activeEffect;
|
||||||
|
activeEffect = null;
|
||||||
|
try { return fn(); }
|
||||||
|
finally { activeEffect = prev; }
|
||||||
|
};
|
||||||
|
|
||||||
const $ = (init, key = null) => {
|
const $ = (init, key = null) => {
|
||||||
const subs = new Set();
|
const subs = new Set();
|
||||||
let val = init;
|
let sig;
|
||||||
if (key) {
|
|
||||||
try {
|
|
||||||
const saved = localStorage.getItem(key);
|
|
||||||
if (saved != null) val = JSON.parse(saved);
|
|
||||||
} catch { }
|
|
||||||
}
|
|
||||||
const sig = (...args) => {
|
|
||||||
if (args.length) {
|
|
||||||
const next = typeof args[0] === "function" ? args[0](val) : args[0];
|
|
||||||
if (!Object.is(val, next)) {
|
|
||||||
val = next;
|
|
||||||
if (key) localStorage.setItem(key, JSON.stringify(val));
|
|
||||||
subs.forEach(e => effectQueue.add(e));
|
|
||||||
if (!isFlushing) queueMicrotask(flushEffects);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return val;
|
|
||||||
};
|
|
||||||
sig.react = () => {
|
|
||||||
if (activeEffect && !activeEffect._deleted) {
|
|
||||||
subs.add(activeEffect);
|
|
||||||
activeEffect._deps.add(subs);
|
|
||||||
}
|
|
||||||
return val;
|
|
||||||
};
|
|
||||||
return sig;
|
|
||||||
};
|
|
||||||
|
|
||||||
const $$ = (fn) => {
|
if (typeof init === "function") {
|
||||||
const subs = new Set();
|
|
||||||
let cached, dirty = true;
|
let cached, dirty = true;
|
||||||
|
|
||||||
// Usamos Watch para rastrear dependencias de fn
|
|
||||||
Watch(() => {
|
Watch(() => {
|
||||||
if (!dirty) {
|
if (!dirty) {
|
||||||
dirty = true;
|
dirty = true;
|
||||||
@@ -71,29 +47,48 @@ const $$ = (fn) => {
|
|||||||
if (!isFlushing) queueMicrotask(flushEffects);
|
if (!isFlushing) queueMicrotask(flushEffects);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
sig = () => {
|
||||||
const sig = () => {
|
if (dirty) { cached = init(); dirty = false; }
|
||||||
if (dirty) { cached = fn(); dirty = false; }
|
|
||||||
// El computado SIEMPRE suscribe si hay un efecto activo
|
|
||||||
if (activeEffect && !activeEffect._deleted) {
|
if (activeEffect && !activeEffect._deleted) {
|
||||||
subs.add(activeEffect);
|
subs.add(activeEffect);
|
||||||
activeEffect._deps.add(subs);
|
activeEffect._deps.add(subs);
|
||||||
}
|
}
|
||||||
return cached;
|
return cached;
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
|
let val = init;
|
||||||
|
if (key) {
|
||||||
|
try {
|
||||||
|
const saved = localStorage.getItem(key);
|
||||||
|
if (saved != null) val = JSON.parse(saved);
|
||||||
|
} catch { }
|
||||||
|
}
|
||||||
|
sig = (...args) => {
|
||||||
|
if (args.length) {
|
||||||
|
const next = typeof args[0] === "function" ? untrack(() => args[0](val)) : args[0];
|
||||||
|
if (!Object.is(val, next)) {
|
||||||
|
val = next;
|
||||||
|
if (key) localStorage.setItem(key, JSON.stringify(val));
|
||||||
|
subs.forEach(e => effectQueue.add(e));
|
||||||
|
if (!isFlushing) queueMicrotask(flushEffects);
|
||||||
|
}
|
||||||
|
} else if (activeEffect && !activeEffect._deleted) {
|
||||||
|
subs.add(activeEffect);
|
||||||
|
activeEffect._deps.add(subs);
|
||||||
|
}
|
||||||
|
return val;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
sig.react = sig;
|
sig._isSig = true;
|
||||||
return sig;
|
return sig;
|
||||||
};
|
};
|
||||||
|
|
||||||
const reactiveCache = new WeakMap();
|
|
||||||
|
|
||||||
const $$_ = (obj) => {
|
const $$_ = (obj) => {
|
||||||
if (obj === null || typeof obj !== "object" || isSignal(obj)) return obj;
|
if (obj === null || typeof obj !== "object" || obj._isSig) return obj;
|
||||||
if (reactiveCache.has(obj)) return reactiveCache.get(obj);
|
if (reactiveCache.has(obj)) return reactiveCache.get(obj);
|
||||||
|
|
||||||
const subs = new Map();
|
const subs = new Map();
|
||||||
|
|
||||||
const proxy = new Proxy(obj, {
|
const proxy = new Proxy(obj, {
|
||||||
get(target, key) {
|
get(target, key) {
|
||||||
if (!subs.has(key)) subs.set(key, new Set());
|
if (!subs.has(key)) subs.set(key, new Set());
|
||||||
@@ -172,6 +167,7 @@ const Tag = (tag, props = {}, children = []) => {
|
|||||||
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);
|
||||||
@@ -193,11 +189,12 @@ const Tag = (tag, props = {}, children = []) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
if (typeof v === "function") {
|
if (typeof v === "function") {
|
||||||
el._cleanups.add(Watch(() => setAttr(isSignal(v) ? v.react() : v())));
|
el._cleanups.add(Watch(() => setAttr(v())));
|
||||||
} else {
|
} else {
|
||||||
setAttr(v);
|
setAttr(v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const append = (c) => {
|
const append = (c) => {
|
||||||
if (Array.isArray(c)) return c.forEach(append);
|
if (Array.isArray(c)) return c.forEach(append);
|
||||||
if (typeof c === "function") {
|
if (typeof c === "function") {
|
||||||
@@ -205,7 +202,7 @@ const Tag = (tag, props = {}, children = []) => {
|
|||||||
el.appendChild(marker);
|
el.appendChild(marker);
|
||||||
let nodes = [];
|
let nodes = [];
|
||||||
el._cleanups.add(Watch(() => {
|
el._cleanups.add(Watch(() => {
|
||||||
const res = isSignal(c) ? c.react() : c();
|
const res = c();
|
||||||
const next = (Array.isArray(res) ? res : [res]).map(n =>
|
const next = (Array.isArray(res) ? res : [res]).map(n =>
|
||||||
n?._isRuntime ? n.container : n instanceof Node ? n : createText(n)
|
n?._isRuntime ? n.container : n instanceof Node ? n : createText(n)
|
||||||
);
|
);
|
||||||
@@ -363,7 +360,7 @@ const Router = (routes) => {
|
|||||||
const outlet = Tag("div");
|
const outlet = Tag("div");
|
||||||
let view = null;
|
let view = null;
|
||||||
Watch(() => {
|
Watch(() => {
|
||||||
const p = path.react();
|
const p = path();
|
||||||
const route = routes.find(r => {
|
const route = routes.find(r => {
|
||||||
const rp = r.path.split("/").filter(Boolean);
|
const rp = r.path.split("/").filter(Boolean);
|
||||||
const pp = p.split("/").filter(Boolean);
|
const pp = p.split("/").filter(Boolean);
|
||||||
@@ -402,7 +399,7 @@ const Mount = (component, target) => {
|
|||||||
return instance;
|
return instance;
|
||||||
};
|
};
|
||||||
|
|
||||||
const SigPro = { $, $$, $$_, Render, Watch, Tag, If, For, Router, Mount, Share, Use };
|
const SigPro = { $, $$_, untrack, 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);
|
||||||
@@ -416,5 +413,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 { $, $$_, untrack, Render, Watch, Tag, If, For, Router, Mount, Share, Use };
|
||||||
export default SigPro;
|
export default SigPro;
|
||||||
Reference in New Issue
Block a user