Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cool-roses-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: improve effect over-fire on store subscription init
3 changes: 3 additions & 0 deletions packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { test } from '../../test';
import { log } from './log.js';

export default test({
html: `<button>increment</button>`,

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]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** @type {any[]} */
export const log = [];
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
import { untrack } from 'svelte';
import { writable } from 'svelte/store';
import { log } from './log';

const storeCount = writable(0)
let count = $state(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why make this state, only to untrack it? Couldn't we just do let count = 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't really have any bearing on this test (which I just copied from the repl).


$effect(() => {
$storeCount;
untrack(() => {
count++;
log.push(count);
});
});
</script>

<button on:click={() => $storeCount++}>increment</button>