fix: improve effect over-fire on store subscription init (#10535)

pull/10534/head
Dominic Gannaway 11 months ago committed by GitHub
parent 73830596b5
commit 41cdbafc40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"svelte": patch
---
fix: improve effect over-fire on store subscription init

@ -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

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

@ -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);
$effect(() => {
$storeCount;
untrack(() => {
count++;
log.push(count);
});
});
</script>
<button on:click={() => $storeCount++}>increment</button>
Loading…
Cancel
Save