Skip to content

fix: avoid microtask in flushSync #16394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/soft-moles-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: avoid microtask in flushSync
20 changes: 11 additions & 9 deletions packages/svelte/src/internal/client/reactivity/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -449,7 +451,7 @@ export function flushSync(fn) {

var result;

const batch = Batch.ensure();
const batch = Batch.ensure(false);

if (fn) {
batch.flush_effects();
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy