Skip to content
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/fresh-birds-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: prevent batches from getting intertwined
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/dom/blocks/boundary.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class Boundary {
// need to use hydration boundary comments to report whether
// the pending or main block was rendered for a given
// boundary, and hydrate accordingly
queueMicrotask(() => {
Batch.enqueue(() => {
this.#main_effect = this.#run(() => {
Batch.ensure();
return branch(() => this.#children(this.#anchor));
Expand Down
30 changes: 28 additions & 2 deletions packages/svelte/src/internal/client/reactivity/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ export let batch_deriveds = null;
/** @type {Set<() => void>} */
export let effect_pending_updates = new Set();

/** @type {Array<() => void>} */
let tasks = [];

function dequeue() {
const task = /** @type {() => void} */ (tasks.shift());

if (tasks.length > 0) {
queueMicrotask(dequeue);
}

task();
}

/** @type {Effect[]} */
let queued_root_effects = [];

Expand Down Expand Up @@ -438,7 +451,7 @@ export class Batch {
batches.add(current_batch);

if (autoflush) {
queueMicrotask(() => {
Batch.enqueue(() => {
if (current_batch !== batch) {
// a flushSync happened in the meantime
return;
Expand All @@ -451,6 +464,15 @@ export class Batch {

return current_batch;
}

/** @param {() => void} task */
static enqueue(task) {
if (tasks.length === 0) {
queueMicrotask(dequeue);
}

tasks.unshift(task);
}
}

/**
Expand Down Expand Up @@ -593,7 +615,11 @@ export function suspend() {

return function unsuspend() {
boundary.update_pending_count(-1);
if (!pending) batch.decrement();

if (!pending) {
batch.activate();
batch.decrement();
}

unset_context();
};
Expand Down
Loading