Skip to content

Commit 66396f3

Browse files
committed
fix: only abort effect flushing if it causes an existing effect to be scheduled
1 parent 2e02868 commit 66396f3

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

.changeset/clever-months-clap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: only abort effect flushing if it causes an existing effect to be scheduled

packages/svelte/src/internal/client/reactivity/batch.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import * as e from '../errors.js';
2828
import { flush_tasks } from '../dom/task.js';
2929
import { DEV } from 'esm-env';
3030
import { invoke_error_boundary } from '../error-handling.js';
31-
import { old_values } from './sources.js';
31+
import { old_values, schedule_version } from './sources.js';
3232
import { unlink_effect } from './effects.js';
3333
import { unset_context } from './async.js';
3434

@@ -598,7 +598,7 @@ function flush_queued_effects(effects) {
598598
var effect = effects[i++];
599599

600600
if ((effect.f & (DESTROYED | INERT)) === 0 && is_dirty(effect)) {
601-
var n = current_batch ? current_batch.current.size : 0;
601+
var sv = schedule_version;
602602

603603
update_effect(effect);
604604

@@ -619,13 +619,9 @@ function flush_queued_effects(effects) {
619619
}
620620
}
621621

622-
// if state is written in a user effect, abort and re-schedule, lest we run
623-
// effects that should be removed as a result of the state change
624-
if (
625-
current_batch !== null &&
626-
current_batch.current.size > n &&
627-
(effect.f & USER_EFFECT) !== 0
628-
) {
622+
// if an effect is invalidated by a user effect, abort and re-schedule, lest we
623+
// run effects that should be removed as a result of the state change
624+
if (schedule_version > sv && (effect.f & USER_EFFECT) !== 0) {
629625
break;
630626
}
631627
}

packages/svelte/src/internal/client/reactivity/sources.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ export function increment(source) {
299299
set(source, source.v + 1);
300300
}
301301

302+
/**
303+
* We increment this value when an effect is scheduled as a result of a state change
304+
*/
305+
export let schedule_version = 0;
306+
302307
/**
303308
* @param {Value} signal
304309
* @param {number} status should be DIRTY or MAYBE_DIRTY
@@ -334,6 +339,7 @@ function mark_reactions(signal, status) {
334339
if ((flags & DERIVED) !== 0) {
335340
mark_reactions(/** @type {Derived} */ (reaction), MAYBE_DIRTY);
336341
} else if (not_dirty) {
342+
schedule_version += 1;
337343
schedule_effect(/** @type {Effect} */ (reaction));
338344
}
339345
}

0 commit comments

Comments
 (0)