Skip to content

Commit b5028dc

Browse files
committed
fix: avoid microtask in flushSync
1 parent 6cf3a19 commit b5028dc

File tree

1 file changed

+14
-7
lines changed
  • packages/svelte/src/internal/client/reactivity

1 file changed

+14
-7
lines changed

packages/svelte/src/internal/client/reactivity/batch.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ let queued_root_effects = [];
5959
let last_scheduled_effect = null;
6060

6161
let is_flushing = false;
62+
let in_flush_sync = false;
6263

6364
export class Batch {
6465
/**
@@ -421,14 +422,16 @@ export class Batch {
421422
const batch = (current_batch = new Batch());
422423
batches.add(current_batch);
423424

424-
queueMicrotask(() => {
425-
if (current_batch !== batch) {
426-
// a flushSync happened in the meantime
427-
return;
428-
}
425+
if (!in_flush_sync) {
426+
queueMicrotask(() => {
427+
if (current_batch !== batch) {
428+
// a flushSync happened in the meantime
429+
return;
430+
}
429431

430-
batch.flush();
431-
});
432+
batch.flush();
433+
});
434+
}
432435
}
433436

434437
return current_batch;
@@ -443,6 +446,8 @@ export class Batch {
443446
* @returns {T}
444447
*/
445448
export function flushSync(fn) {
449+
in_flush_sync = true;
450+
446451
if (async_mode_flag && active_effect !== null) {
447452
e.flush_sync_in_effect();
448453
}
@@ -473,6 +478,8 @@ export function flushSync(fn) {
473478
dev_effect_stack = [];
474479
}
475480

481+
in_flush_sync = false;
482+
476483
return /** @type {T} */ (result);
477484
}
478485

0 commit comments

Comments
 (0)