Skip to content

Commit

Permalink
fix: avoid duplicate signal dependencies (#12245)
Browse files Browse the repository at this point in the history
Fixes #12230
  • Loading branch information
trueadm authored Jul 1, 2024
1 parent 20bb4f7 commit 9f51760
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/polite-peas-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: avoid duplicate signal dependencies
5 changes: 4 additions & 1 deletion packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,10 @@ export function get(signal) {
) {
if (current_dependencies === null) {
current_dependencies = [signal];
} else if (current_dependencies[current_dependencies.length - 1] !== signal) {
} else if (
current_dependencies[current_dependencies.length - 1] !== signal &&
!current_dependencies.includes(signal)
) {
current_dependencies.push(signal);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
async test({ assert, target }) {
let [btn1, btn2] = target.querySelectorAll('button');

flushSync(() => {
btn1?.click();
});

assert.htmlEqual(target.innerHTML, `<button>hide</button><button>show</button`);

flushSync(() => {
btn2?.click();
});

assert.htmlEqual(
target.innerHTML,
`<h1>John Doe</h1><p>Body</p><button>hide</button><button>show</button>`
);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script>
let data = $state({
event: {
author: 'John Doe',
body: 'Body'
}
});
const { event } = $derived(data);
</script>

{#if event}
<h1>{event.author}</h1>
<p>{event.body}</p>
{/if}

<button onclick={() => {
data = {}
}}>hide</button>

<button onclick={() => {
data = {
event: {
author: 'John Doe',
body: 'Body'
}
}
}}>show</button>

0 comments on commit 9f51760

Please sign in to comment.