diff --git a/packages/svelte/src/internal/client/reactivity/batch.js b/packages/svelte/src/internal/client/reactivity/batch.js index bccb078250..03483ea9ac 100644 --- a/packages/svelte/src/internal/client/reactivity/batch.js +++ b/packages/svelte/src/internal/client/reactivity/batch.js @@ -654,26 +654,10 @@ export class Batch { var next_batch = /** @type {Batch | null} */ (/** @type {unknown} */ (current_batch)); - if (this.#pending === 0 && (this.#scheduled.length === 0 || next_batch !== null)) { + if (this.#pending === 0) { this.#unlink(); } - // Edge case: During traversal new branches might create effects that run immediately and set state, - // causing an effect to be scheduled again. We need to traverse the current batch - // once more in that case - most of the time this will just clean up dirty branches. - // TODO I think we can delete this now since we re-iterate above - if (this.#scheduled.length > 0) { - if (next_batch !== null) { - for (const e of this.#scheduled) { - next_batch.#scheduled.push(e); - } - - this.#scheduled = []; - } else { - next_batch = this; - } - } - if (next_batch !== null) { old_values.clear(); next_batch.#process(); diff --git a/packages/svelte/src/internal/client/reactivity/deriveds.js b/packages/svelte/src/internal/client/reactivity/deriveds.js index d2919a7e45..2abc477fef 100644 --- a/packages/svelte/src/internal/client/reactivity/deriveds.js +++ b/packages/svelte/src/internal/client/reactivity/deriveds.js @@ -436,8 +436,9 @@ export function update_derived(derived) { // to the previous batch, not the new one (which can already exist if an earlier // effect wrote to a source). Not adding it to either can cause bugs, so we add it to both. // See https://github.com/sveltejs/svelte/pull/18117 for more details. - previous_batch?.capture(derived, value, true); + // Order matters: current_batch first so it gets the correct previous value. current_batch?.capture(derived, value, true); + previous_batch?.capture(derived, value, true, current_batch !== null); } else { derived.v = value; derived.wv = increment_write_version();