From 4947283fa5547691adcb129e145eed77c38f39fc Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 16 Jul 2025 12:08:45 -0400 Subject: [PATCH] fix: avoid microtask in flushSync (#16394) * fix: avoid microtask in flushSync * fix/simplify * on second thoughts * changeset --- .changeset/soft-moles-work.md | 5 +++++ .../src/internal/client/reactivity/batch.js | 20 ++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 .changeset/soft-moles-work.md diff --git a/.changeset/soft-moles-work.md b/.changeset/soft-moles-work.md new file mode 100644 index 0000000000..c3573e4bc5 --- /dev/null +++ b/.changeset/soft-moles-work.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: avoid microtask in flushSync diff --git a/packages/svelte/src/internal/client/reactivity/batch.js b/packages/svelte/src/internal/client/reactivity/batch.js index f881330e90..1d08b5c3d8 100644 --- a/packages/svelte/src/internal/client/reactivity/batch.js +++ b/packages/svelte/src/internal/client/reactivity/batch.js @@ -416,19 +416,21 @@ export class Batch { return (this.#deferred ??= deferred()).promise; } - static ensure() { + static ensure(autoflush = true) { if (current_batch === null) { const batch = (current_batch = new Batch()); batches.add(current_batch); - queueMicrotask(() => { - if (current_batch !== batch) { - // a flushSync happened in the meantime - return; - } + if (autoflush) { + queueMicrotask(() => { + if (current_batch !== batch) { + // a flushSync happened in the meantime + return; + } - batch.flush(); - }); + batch.flush(); + }); + } } return current_batch; @@ -449,7 +451,7 @@ export function flushSync(fn) { var result; - const batch = Batch.ensure(); + const batch = Batch.ensure(false); if (fn) { batch.flush_effects();