From 9e695442380a7c3bec5bf90d6c7341c3e1ae9bac Mon Sep 17 00:00:00 2001 From: JY Wey <34165386+JaiWey@users.noreply.github.com> Date: Fri, 4 Sep 2026 02:06:28 +1200 Subject: [PATCH] fix: in non-async mode, only push variable to current_sources when active_reaction is updating (#18550) fix: #16814 in non-async mode, when the derived inside `SvelteDate` is created and evaluated, the `#reaction` is from the `active_reaction` when the `SvelteDate` instance is created, which could possible in NON_UPDATING state, so the derived should not be added to `current_sources` to prevent re-run. --------- Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com> Co-authored-by: Simon Holthausen --- .changeset/tricky-cooks-help.md | 5 +++ .../svelte/src/internal/client/runtime.js | 6 ++- packages/svelte/tests/signals/test.ts | 39 +++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 .changeset/tricky-cooks-help.md diff --git a/.changeset/tricky-cooks-help.md b/.changeset/tricky-cooks-help.md new file mode 100644 index 0000000000..f61c7944a1 --- /dev/null +++ b/.changeset/tricky-cooks-help.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: in non-async mode, only push variable to current_sources when active_reaction is updating diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index a881403f58..d86c349445 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -100,7 +100,11 @@ export let current_sources = null; /** @param {Value} value */ export function push_reaction_value(value) { - if (active_reaction !== null && (!async_mode_flag || (active_reaction.f & DERIVED) !== 0)) { + if ( + active_reaction !== null && + ((!async_mode_flag && (active_reaction.f & REACTION_IS_UPDATING) !== 0) || + (active_reaction.f & DERIVED) !== 0) + ) { (current_sources ??= new Set()).add(value); } } diff --git a/packages/svelte/tests/signals/test.ts b/packages/svelte/tests/signals/test.ts index c1ebc3ec75..285dc1b3ce 100644 --- a/packages/svelte/tests/signals/test.ts +++ b/packages/svelte/tests/signals/test.ts @@ -1591,4 +1591,43 @@ describe('signals', () => { assert.equal(s.reactions, null); }; }); + + // https://github.com/sveltejs/svelte/issues/16814 + it('does not treat values created for inactive reactions as current', () => { + push({}, true); + + const trigger = state(false); + const value = state(0); + const log: number[] = []; + let inactive_reaction: Effect; + + const destroy = effect_root(() => { + render_effect(() => { + $.get(trigger); + inactive_reaction = $.active_reaction as Effect; + }); + + render_effect(() => { + $.get(trigger); + + const reaction = $.active_reaction; + $.set_active_reaction(inactive_reaction); + $.push_reaction_value(value); + $.set_active_reaction(reaction); + + log.push($.get(value)); + }); + }); + + try { + flushSync(); + flushSync(() => set(trigger, true)); + flushSync(() => set(value, 1)); + + assert.deepEqual(log, [0, 0, 1]); + } finally { + destroy(); + pop(); + } + }); });