diff --git a/.changeset/rich-tables-sing.md b/.changeset/rich-tables-sing.md new file mode 100644 index 0000000000..dec3c5a522 --- /dev/null +++ b/.changeset/rich-tables-sing.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: improve signal consumer removal logic diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index ffa806dbe2..f49a22caa9 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -382,13 +382,15 @@ function remove_consumer(signal, start_index, remove_unowned) { let consumers_length = 0; if (consumers !== null) { consumers_length = consumers.length - 1; - if (consumers_length === 0) { - dependency.c = null; - } else { - const index = consumers.indexOf(signal); - // Swap with last element and then remove. - consumers[index] = consumers[consumers_length]; - consumers.pop(); + const index = consumers.indexOf(signal); + if (index !== -1) { + if (consumers_length === 0) { + dependency.c = null; + } else { + // Swap with last element and then remove. + consumers[index] = consumers[consumers_length]; + consumers.pop(); + } } } if (remove_unowned && consumers_length === 0 && (dependency.f & UNOWNED) !== 0) { diff --git a/packages/svelte/tests/runtime-runes/samples/effect-dependencies/_config.js b/packages/svelte/tests/runtime-runes/samples/effect-dependencies/_config.js new file mode 100644 index 0000000000..a2d7bb8d0f --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/effect-dependencies/_config.js @@ -0,0 +1,52 @@ +import { flushSync } from 'svelte'; +import { test } from '../../test'; + +export default test({ + async test({ assert, target }) { + const [b1, b2] = target.querySelectorAll('button'); + flushSync(() => { + b1.click(); + }); + + assert.htmlEqual( + target.innerHTML, + `
A
` + ); + + flushSync(() => { + b2.click(); + }); + + assert.htmlEqual( + target.innerHTML, + `
B\n12
` + ); + + flushSync(() => { + b1.click(); + }); + + assert.htmlEqual( + target.innerHTML, + `
A
` + ); + + flushSync(() => { + b2.click(); + }); + + assert.htmlEqual( + target.innerHTML, + `
B\n12
` + ); + + flushSync(() => { + b1.click(); + }); + + assert.htmlEqual( + target.innerHTML, + `
A
` + ); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/effect-dependencies/main.svelte b/packages/svelte/tests/runtime-runes/samples/effect-dependencies/main.svelte new file mode 100644 index 0000000000..75f5a0cd6e --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/effect-dependencies/main.svelte @@ -0,0 +1,31 @@ + + +
+ + +
+ +
+ {#if things.tab === 'A'} + A + {:else} + B + {#each things.list as item} + {item.no} + {/each} + {/if} +