Skip to content

Commit 9f51760

Browse files
authored
fix: avoid duplicate signal dependencies (#12245)
Fixes #12230
1 parent 20bb4f7 commit 9f51760

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

.changeset/polite-peas-mate.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: avoid duplicate signal dependencies

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,10 @@ export function get(signal) {
774774
) {
775775
if (current_dependencies === null) {
776776
current_dependencies = [signal];
777-
} else if (current_dependencies[current_dependencies.length - 1] !== signal) {
777+
} else if (
778+
current_dependencies[current_dependencies.length - 1] !== signal &&
779+
!current_dependencies.includes(signal)
780+
) {
778781
current_dependencies.push(signal);
779782
}
780783
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
export default test({
5+
async test({ assert, target }) {
6+
let [btn1, btn2] = target.querySelectorAll('button');
7+
8+
flushSync(() => {
9+
btn1?.click();
10+
});
11+
12+
assert.htmlEqual(target.innerHTML, `<button>hide</button><button>show</button`);
13+
14+
flushSync(() => {
15+
btn2?.click();
16+
});
17+
18+
assert.htmlEqual(
19+
target.innerHTML,
20+
`<h1>John Doe</h1><p>Body</p><button>hide</button><button>show</button>`
21+
);
22+
}
23+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<script>
2+
let data = $state({
3+
event: {
4+
author: 'John Doe',
5+
body: 'Body'
6+
}
7+
});
8+
const { event } = $derived(data);
9+
</script>
10+
11+
{#if event}
12+
<h1>{event.author}</h1>
13+
<p>{event.body}</p>
14+
{/if}
15+
16+
<button onclick={() => {
17+
data = {}
18+
}}>hide</button>
19+
20+
<button onclick={() => {
21+
data = {
22+
event: {
23+
author: 'John Doe',
24+
body: 'Body'
25+
}
26+
}
27+
}}>show</button>

0 commit comments

Comments
 (0)