fix: avoid microtask in flushSync

pull/16394/head
Rich Harris 2 months ago
parent 6cf3a19342
commit b5028dcb1a

@ -59,6 +59,7 @@ let queued_root_effects = [];
let last_scheduled_effect = null; let last_scheduled_effect = null;
let is_flushing = false; let is_flushing = false;
let in_flush_sync = false;
export class Batch { export class Batch {
/** /**
@ -421,14 +422,16 @@ export class Batch {
const batch = (current_batch = new Batch()); const batch = (current_batch = new Batch());
batches.add(current_batch); batches.add(current_batch);
queueMicrotask(() => { if (!in_flush_sync) {
if (current_batch !== batch) { queueMicrotask(() => {
// a flushSync happened in the meantime if (current_batch !== batch) {
return; // a flushSync happened in the meantime
} return;
}
batch.flush(); batch.flush();
}); });
}
} }
return current_batch; return current_batch;
@ -443,6 +446,8 @@ export class Batch {
* @returns {T} * @returns {T}
*/ */
export function flushSync(fn) { export function flushSync(fn) {
in_flush_sync = true;
if (async_mode_flag && active_effect !== null) { if (async_mode_flag && active_effect !== null) {
e.flush_sync_in_effect(); e.flush_sync_in_effect();
} }
@ -473,6 +478,8 @@ export function flushSync(fn) {
dev_effect_stack = []; dev_effect_stack = [];
} }
in_flush_sync = false;
return /** @type {T} */ (result); return /** @type {T} */ (result);
} }

Loading…
Cancel
Save