Skip to content

Commit 41cdbaf

Browse files
authored
fix: improve effect over-fire on store subscription init (#10535)
1 parent 7383059 commit 41cdbaf

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

.changeset/cool-roses-trade.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: improve effect over-fire on store subscription init

packages/svelte/src/internal/client/runtime.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,8 +1210,11 @@ export function set_signal_value(signal, value) {
12101210
//
12111211
// $effect(() => x++)
12121212
//
1213+
// We additionally want to skip this logic for when ignore_mutation_validation is
1214+
// true, as stores write to source signal on initialization.
12131215
if (
12141216
is_runes(null) &&
1217+
!ignore_mutation_validation &&
12151218
current_effect !== null &&
12161219
current_effect.c === null &&
12171220
(current_effect.f & CLEAN) !== 0
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { test } from '../../test';
2+
import { log } from './log.js';
3+
4+
export default test({
5+
html: `<button>increment</button>`,
6+
7+
before_test() {
8+
log.length = 0;
9+
},
10+
11+
async test({ assert, target }) {
12+
const btn = target.querySelector('button');
13+
14+
assert.deepEqual(log, [1]);
15+
16+
await btn?.click();
17+
assert.deepEqual(log, [1, 2]);
18+
19+
await btn?.click();
20+
assert.deepEqual(log, [1, 2, 3]);
21+
}
22+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/** @type {any[]} */
2+
export const log = [];
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script>
2+
import { untrack } from 'svelte';
3+
import { writable } from 'svelte/store';
4+
import { log } from './log';
5+
6+
const storeCount = writable(0)
7+
let count = $state(0);
8+
9+
$effect(() => {
10+
$storeCount;
11+
untrack(() => {
12+
count++;
13+
log.push(count);
14+
});
15+
});
16+
</script>
17+
18+
<button on:click={() => $storeCount++}>increment</button>

0 commit comments

Comments
 (0)