From 41cdbafc40caf21083a40bf86aeadaefab3771b1 Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Mon, 19 Feb 2024 12:24:35 +0000 Subject: [PATCH] fix: improve effect over-fire on store subscription init (#10535) --- .changeset/cool-roses-trade.md | 5 +++++ .../svelte/src/internal/client/runtime.js | 3 +++ .../store-subscribe-effect-init/_config.js | 22 +++++++++++++++++++ .../store-subscribe-effect-init/log.js | 2 ++ .../store-subscribe-effect-init/main.svelte | 18 +++++++++++++++ 5 files changed, 50 insertions(+) create mode 100644 .changeset/cool-roses-trade.md create mode 100644 packages/svelte/tests/runtime-runes/samples/store-subscribe-effect-init/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/store-subscribe-effect-init/log.js create mode 100644 packages/svelte/tests/runtime-runes/samples/store-subscribe-effect-init/main.svelte 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 @@ + + +