From e6b1f6505523f43798c49d85e496206b2ae1864a Mon Sep 17 00:00:00 2001 From: natxocc Date: Mon, 13 Apr 2026 23:29:27 +0200 Subject: [PATCH] add batch() --- sigpro2.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/sigpro2.js b/sigpro2.js index 273e831..0d56664 100644 --- a/sigpro2.js +++ b/sigpro2.js @@ -8,6 +8,7 @@ const ensureNode = n => n?._isRuntime ? n.container : (n instanceof Node ? n : d let activeEffect = null let activeOwner = null let isFlushing = false +let batchDepth = 0 const effectQueue = new Set() const MOUNTED_NODES = new WeakMap() @@ -94,6 +95,18 @@ const flush = () => { isFlushing = false } +const batch = fn => { + batchDepth++ + try { + return fn() + } finally { + batchDepth-- + if (batchDepth === 0 && effectQueue.size > 0 && !isFlushing) { + flush() + } + } +} + const trackUpdate = (subs, trigger = false) => { if (!trigger && activeEffect && !activeEffect._disposed) { subs.add(activeEffect) @@ -110,7 +123,7 @@ const trackUpdate = (subs, trigger = false) => { hasQueue = true } }) - if (hasQueue && !isFlushing) queueMicrotask(flush) + if (hasQueue && !isFlushing && batchDepth === 0) queueMicrotask(flush) } } @@ -456,7 +469,7 @@ const Mount = (comp, target) => { return inst } -const SigPro = Object.freeze({ $, Watch, Tag, Render, If, For, Router, Mount, onMount, onUnmount, set }) +const SigPro = Object.freeze({ $, Watch, Tag, Render, If, For, Router, Mount, onMount, onUnmount, batch, set }) if (typeof window !== "undefined") { Object.assign(window, SigPro) @@ -464,5 +477,5 @@ if (typeof window !== "undefined") { .split(" ").forEach(t => window[t[0].toUpperCase() + t.slice(1)] = (p, c) => SigPro.Tag(t, p, c)) } -export { $, Watch, Tag, Render, If, For, Router, Mount, onMount, onUnmount, set } +export { $, Watch, Tag, Render, If, For, Router, Mount, onMount, onUnmount, batch, set } export default SigPro \ No newline at end of file