fix: avoid microtask in flushSync (#16394)

* fix: avoid microtask in flushSync

* fix/simplify

* on second thoughts

* changeset
pull/16397/head
Rich Harris 2 months ago committed by GitHub
parent be44f8b23e
commit 4947283fa5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: avoid microtask in flushSync

@ -416,11 +416,12 @@ export class Batch {
return (this.#deferred ??= deferred()).promise; return (this.#deferred ??= deferred()).promise;
} }
static ensure() { static ensure(autoflush = true) {
if (current_batch === null) { if (current_batch === null) {
const batch = (current_batch = new Batch()); const batch = (current_batch = new Batch());
batches.add(current_batch); batches.add(current_batch);
if (autoflush) {
queueMicrotask(() => { queueMicrotask(() => {
if (current_batch !== batch) { if (current_batch !== batch) {
// a flushSync happened in the meantime // a flushSync happened in the meantime
@ -430,6 +431,7 @@ export class Batch {
batch.flush(); batch.flush();
}); });
} }
}
return current_batch; return current_batch;
} }
@ -449,7 +451,7 @@ export function flushSync(fn) {
var result; var result;
const batch = Batch.ensure(); const batch = Batch.ensure(false);
if (fn) { if (fn) {
batch.flush_effects(); batch.flush_effects();

Loading…
Cancel
Save