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 <simon.holthausen@vercel.com>
pull/17600/merge
JY Wey 4 days ago committed by GitHub
parent 6f7857af59
commit 9e69544238
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: in non-async mode, only push variable to current_sources when active_reaction is updating

@ -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);
}
}

@ -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();
}
});
});

Loading…
Cancel
Save