fix: avoid duplicate signal dependencies (#12245)

Fixes #12230
pull/12249/head
Dominic Gannaway 5 months ago committed by GitHub
parent 20bb4f7ff7
commit 9f51760dc6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: avoid duplicate signal dependencies

@ -774,7 +774,10 @@ export function get(signal) {
) { ) {
if (current_dependencies === null) { if (current_dependencies === null) {
current_dependencies = [signal]; 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); current_dependencies.push(signal);
} }
} }

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

@ -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>
Loading…
Cancel
Save