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,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();

Loading…
Cancel
Save