This commit is contained in:
2026-04-10 09:08:52 +02:00
parent adafb8eb11
commit 52af01e128

View File

@@ -1,4 +1,4 @@
/** SigPro (Signals & Proxies) */ /** SigPro 1.3 (Signals & Proxies) */
// Helpers // Helpers
const doc = typeof document !== "undefined" ? document : null; const doc = typeof document !== "undefined" ? document : null;
@@ -115,7 +115,7 @@ export const $ = (val, key = null) => {
if (computed._deps) { computed._deps.forEach(depSet => depSet.delete(computed)); computed._deps.clear(); } if (computed._deps) { computed._deps.forEach(depSet => depSet.delete(computed)); computed._deps.clear(); }
subs.clear(); subs.clear();
}; };
onUnmount(computed.stop); if (activeOwner) onUnmount(computed.stop);
return computed; return computed;
} }
if (key) try { val = JSON.parse(localStorage.getItem(key)) ?? val; } catch (e) { } if (key) try { val = JSON.parse(localStorage.getItem(key)) ?? val; } catch (e) { }
@@ -152,6 +152,18 @@ export const Watch = (sources, cb) => {
return () => dispose(effect); return () => dispose(effect);
} }
export const watch = (source, callback) => {
let oldValue, first = true;
const effect = createEffect(() => {
const newValue = isFunc(source) ? source() : source;
if (!first) untrack(() => callback(newValue, oldValue));
else first = false;
oldValue = newValue;
});
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(); }
if (node._ownerEffect) dispose(node._ownerEffect); if (node._ownerEffect) dispose(node._ownerEffect);