p
This commit is contained in:
@@ -32,8 +32,14 @@ const createEffect = (fn, isComputed = false) => {
|
|||||||
if (effect._cleanups) { effect._cleanups.forEach(cl => cl()); effect._cleanups.clear(); }
|
if (effect._cleanups) { effect._cleanups.forEach(cl => cl()); effect._cleanups.clear(); }
|
||||||
const prevEffect = activeEffect, prevOwner = activeOwner;
|
const prevEffect = activeEffect, prevOwner = activeOwner;
|
||||||
activeEffect = activeOwner = effect;
|
activeEffect = activeOwner = effect;
|
||||||
try { return isComputed ? fn() : (fn(), undefined); }
|
try {
|
||||||
finally { activeEffect = prevEffect; activeOwner = prevOwner; }
|
const res = isComputed ? fn() : (fn(), undefined);
|
||||||
|
if (!isComputed) effect._result = res; // ← ESTO ES LO NUEVO
|
||||||
|
return res;
|
||||||
|
} finally {
|
||||||
|
activeEffect = prevEffect;
|
||||||
|
activeOwner = prevOwner;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
effect._deps = effect._cleanups = effect._children = null;
|
effect._deps = effect._cleanups = effect._children = null;
|
||||||
effect._disposed = false;
|
effect._disposed = false;
|
||||||
@@ -136,12 +142,15 @@ export const $$ = (obj, cache = new WeakMap()) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Watcher
|
// Watcher
|
||||||
export const Watch = (target, cb) => {
|
export const Watch = (sources, cb) => {
|
||||||
const explicit = isArr(target);
|
const isArr = Array.isArray(sources);
|
||||||
const effect = createEffect(() => explicit ? (untrack(cb), target.forEach(d => isFunc(d) && d())) : cb());
|
const effect = createEffect(() => {
|
||||||
|
const vals = isArr ? sources.map(s => s()) : sources();
|
||||||
|
untrack(() => cb(vals));
|
||||||
|
});
|
||||||
effect();
|
effect();
|
||||||
return () => dispose(effect);
|
return () => dispose(effect);
|
||||||
};
|
}
|
||||||
|
|
||||||
const cleanupNode = node => {
|
const cleanupNode = node => {
|
||||||
if (node._cleanups) { node._cleanups.forEach(fn => fn()); node._cleanups.clear(); }
|
if (node._cleanups) { node._cleanups.forEach(fn => fn()); node._cleanups.clear(); }
|
||||||
@@ -166,6 +175,30 @@ export const inject = (key, defaultValue) => {
|
|||||||
// CreateElement
|
// CreateElement
|
||||||
export const Tag = (tag, props = {}, children = []) => {
|
export const Tag = (tag, props = {}, children = []) => {
|
||||||
if (props instanceof Node || isArr(props) || !isObj(props)) { children = props; props = {}; }
|
if (props instanceof Node || isArr(props) || !isObj(props)) { children = props; props = {}; }
|
||||||
|
if (isFunc(tag)) {
|
||||||
|
const ctx = {
|
||||||
|
_mounts: [],
|
||||||
|
_cleanups: new Set(),
|
||||||
|
_provisions: activeOwner?._provisions ? { ...activeOwner._provisions } : {}
|
||||||
|
};
|
||||||
|
const effect = createEffect(() => {
|
||||||
|
const result = tag(props, {
|
||||||
|
children,
|
||||||
|
emit: (ev, ...args) => props[`on${ev[0].toUpperCase()}${ev.slice(1)}`]?.(...args)
|
||||||
|
});
|
||||||
|
effect._result = result;
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
effect();
|
||||||
|
ctx._mounts = effect._mounts || [];
|
||||||
|
ctx._cleanups = effect._cleanups || new Set();
|
||||||
|
const result = effect._result;
|
||||||
|
const attachLifecycle = (node) => node && typeof node === 'object' && !node._isRuntime && (node._mounts = ctx._mounts, node._cleanups = ctx._cleanups, node._ownerEffect = effect);
|
||||||
|
Array.isArray(result) ? result.forEach(attachLifecycle) : attachLifecycle(result);
|
||||||
|
if (result == null) return null;
|
||||||
|
if (isNode(result) || (Array.isArray(result) && result.every(isNode))) return result;
|
||||||
|
return doc.createTextNode(String(result));
|
||||||
|
}
|
||||||
const isSVG = /^(svg|path|circle|rect|line|polyline|polygon|g|defs|text|tspan|use)$/.test(tag);
|
const isSVG = /^(svg|path|circle|rect|line|polyline|polygon|g|defs|text|tspan|use)$/.test(tag);
|
||||||
const el = isSVG ? doc.createElementNS("http://www.w3.org/2000/svg", tag) : doc.createElement(tag);
|
const el = isSVG ? doc.createElementNS("http://www.w3.org/2000/svg", tag) : doc.createElement(tag);
|
||||||
el._cleanups = new Set();
|
el._cleanups = new Set();
|
||||||
@@ -240,7 +273,7 @@ export const Render = fn => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// If
|
// If
|
||||||
export const If = (cond, t, f = null, trans = null) => {
|
export const If = (cond, ifYes, ifNot = null, trans = null) => {
|
||||||
const anchor = doc.createTextNode("");
|
const anchor = doc.createTextNode("");
|
||||||
const root = Tag("div", { style: "display:contents" }, [anchor]);
|
const root = Tag("div", { style: "display:contents" }, [anchor]);
|
||||||
let currentView = null, last = null;
|
let currentView = null, last = null;
|
||||||
@@ -251,7 +284,7 @@ export const If = (cond, t, f = null, trans = null) => {
|
|||||||
const disposeView = () => { if (currentView) { currentView.destroy(); currentView = null; } };
|
const disposeView = () => { if (currentView) { currentView.destroy(); currentView = null; } };
|
||||||
if (currentView && !show && trans?.out) trans.out(currentView.container, disposeView);
|
if (currentView && !show && trans?.out) trans.out(currentView.container, disposeView);
|
||||||
else disposeView();
|
else disposeView();
|
||||||
const content = show ? t : f;
|
const content = show ? ifYes : ifNot;
|
||||||
if (content) {
|
if (content) {
|
||||||
currentView = Render(() => isFunc(content) ? content() : content);
|
currentView = Render(() => isFunc(content) ? content() : content);
|
||||||
root.insertBefore(currentView.container, anchor);
|
root.insertBefore(currentView.container, anchor);
|
||||||
|
|||||||
Reference in New Issue
Block a user