diff --git a/.changeset/cool-roses-trade.md b/.changeset/cool-roses-trade.md new file mode 100644 index 0000000000..ae05a8f8b4 --- /dev/null +++ b/.changeset/cool-roses-trade.md @@ -0,0 +1,5 @@ +--- +"svelte": patch +--- + +fix: improve effect over-fire on store subscription init diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index dbcffed933..8db5cfa54f 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -1210,8 +1210,11 @@ export function set_signal_value(signal, value) { // // $effect(() => x++) // + // We additionally want to skip this logic for when ignore_mutation_validation is + // true, as stores write to source signal on initialization. if ( is_runes(null) && + !ignore_mutation_validation && current_effect !== null && current_effect.c === null && (current_effect.f & CLEAN) !== 0 diff --git a/packages/svelte/tests/runtime-runes/samples/store-subscribe-effect-init/_config.js b/packages/svelte/tests/runtime-runes/samples/store-subscribe-effect-init/_config.js new file mode 100644 index 0000000000..13ada44925 --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/store-subscribe-effect-init/_config.js @@ -0,0 +1,22 @@ +import { test } from '../../test'; +import { log } from './log.js'; + +export default test({ + html: ``, + + before_test() { + log.length = 0; + }, + + async test({ assert, target }) { + const btn = target.querySelector('button'); + + assert.deepEqual(log, [1]); + + await btn?.click(); + assert.deepEqual(log, [1, 2]); + + await btn?.click(); + assert.deepEqual(log, [1, 2, 3]); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/store-subscribe-effect-init/log.js b/packages/svelte/tests/runtime-runes/samples/store-subscribe-effect-init/log.js new file mode 100644 index 0000000000..d3df521f4d --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/store-subscribe-effect-init/log.js @@ -0,0 +1,2 @@ +/** @type {any[]} */ +export const log = []; diff --git a/packages/svelte/tests/runtime-runes/samples/store-subscribe-effect-init/main.svelte b/packages/svelte/tests/runtime-runes/samples/store-subscribe-effect-init/main.svelte new file mode 100644 index 0000000000..27c07d779c --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/store-subscribe-effect-init/main.svelte @@ -0,0 +1,18 @@ + + +