From 32f4451f2eebb67e7b4f44042d6a37d129813809 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Mon, 21 Sep 2026 17:57:56 +0200 Subject: [PATCH] fix flushSync (should drain queue before calling the function) --- .../src/internal/client/reactivity/batch.js | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/svelte/src/internal/client/reactivity/batch.js b/packages/svelte/src/internal/client/reactivity/batch.js index cc1545696e..f7d657aa93 100644 --- a/packages/svelte/src/internal/client/reactivity/batch.js +++ b/packages/svelte/src/internal/client/reactivity/batch.js @@ -1001,7 +1001,7 @@ export class Batch { if (current_batch === null) { const batch = (current_batch = new Batch()); - if (!is_processing && !is_flushing_sync) { + if (!is_processing) { queue_micro_task(() => { if (!batch.started) { batch.flush(); @@ -1124,9 +1124,7 @@ export function flushSync(fn) { var result; if (fn) { - if (current_batch !== null && !current_batch.is_fork) { - current_batch.flush(); - } + flushSync(); // drain queue via the lower while(true) part result = fn(); } @@ -1464,15 +1462,21 @@ export function fork(fn) { e.fork_timing(); } - var batch = Batch.ensure(); - batch.is_fork = true; - batch_values = new Map(); - wv_values = new Map(); - var committed = false; - var settled = batch.settled(); + /** @type {Promise} */ + var settled; + /** @type {Batch} */ + var batch; - flushSync(fn); + flushSync(() => { + batch = Batch.ensure(); + batch.is_fork = true; + batch_values = new Map(); + wv_values = new Map(); + settled = batch.settled(); + + fn(); + }); return { commit: async () => {