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/metal-parents-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: flush batches whenever an async value resolves
2 changes: 0 additions & 2 deletions packages/svelte/src/internal/client/reactivity/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ export function flatten(sync, async, fn) {

Promise.all(async.map((expression) => async_derived(expression)))
.then((result) => {
batch?.activate();

restore();

try {
Expand Down
45 changes: 21 additions & 24 deletions packages/svelte/src/internal/client/reactivity/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ export class Batch {
flush_queued_effects(render_effects);
flush_queued_effects(effects);

previous_batch = null;

this.#deferred?.resolve();
} else {
this.#defer_effects(this.#render_effects);
Expand Down Expand Up @@ -280,17 +282,6 @@ export class Batch {

deactivate() {
current_batch = null;
previous_batch = null;

for (const update of effect_pending_updates) {
effect_pending_updates.delete(update);
update();

if (current_batch !== null) {
// only do one at a time
break;
}
}
}

flush() {
Expand All @@ -307,6 +298,16 @@ export class Batch {
}

this.deactivate();

for (const update of effect_pending_updates) {
effect_pending_updates.delete(update);
update();

if (current_batch !== null) {
// only do one at a time
break;
}
}
}

/**
Expand Down Expand Up @@ -375,21 +376,17 @@ export class Batch {
decrement() {
this.#pending -= 1;

if (this.#pending === 0) {
for (const e of this.#dirty_effects) {
set_signal_status(e, DIRTY);
schedule_effect(e);
}

for (const e of this.#maybe_dirty_effects) {
set_signal_status(e, MAYBE_DIRTY);
schedule_effect(e);
}
for (const e of this.#dirty_effects) {
set_signal_status(e, DIRTY);
schedule_effect(e);
}

this.flush();
} else {
this.deactivate();
for (const e of this.#maybe_dirty_effects) {
set_signal_status(e, MAYBE_DIRTY);
schedule_effect(e);
}

this.flush();
}

/** @param {() => void} fn */
Expand Down
Loading