From 4dcac41c10b1fbd4e6a1c5b7061e6a668ef062fd Mon Sep 17 00:00:00 2001 From: Dominic Gannaway Date: Mon, 14 Oct 2024 16:20:03 +0100 Subject: [PATCH] fix: cleanup non-branch effects created inside block effects (#13600) * fix: cleanup non-branch effects created inside block effects * cleaner --- .changeset/eleven-humans-kneel.md | 5 +++++ .../svelte/src/internal/client/runtime.js | 20 ++++++++++++++++++- .../clean-block-inner-effects/_config.js | 13 ++++++++++++ .../clean-block-inner-effects/main.svelte | 18 +++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 .changeset/eleven-humans-kneel.md create mode 100644 packages/svelte/tests/runtime-runes/samples/clean-block-inner-effects/_config.js create mode 100644 packages/svelte/tests/runtime-runes/samples/clean-block-inner-effects/main.svelte diff --git a/.changeset/eleven-humans-kneel.md b/.changeset/eleven-humans-kneel.md new file mode 100644 index 0000000000..bb721a83be --- /dev/null +++ b/.changeset/eleven-humans-kneel.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: cleanup non-branch effects created inside block effects diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index 6f0131b8ea..6786d99d90 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -429,6 +429,22 @@ export function destroy_effect_children(signal, remove_dom = false) { } } +/** + * @param {Effect} signal + * @returns {void} + */ +export function destroy_block_effect_children(signal) { + var effect = signal.first; + + while (effect !== null) { + var next = effect.next; + if ((effect.f & BRANCH_EFFECT) === 0) { + destroy_effect(effect); + } + effect = next; + } +} + /** * @param {Effect} effect * @returns {void} @@ -454,7 +470,9 @@ export function update_effect(effect) { } try { - if ((flags & BLOCK_EFFECT) === 0) { + if ((flags & BLOCK_EFFECT) !== 0) { + destroy_block_effect_children(effect); + } else { destroy_effect_children(effect); } diff --git a/packages/svelte/tests/runtime-runes/samples/clean-block-inner-effects/_config.js b/packages/svelte/tests/runtime-runes/samples/clean-block-inner-effects/_config.js new file mode 100644 index 0000000000..6e42ebeadf --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/clean-block-inner-effects/_config.js @@ -0,0 +1,13 @@ +import { flushSync } from 'svelte'; +import { test } from '../../test'; + +export default test({ + async test({ assert, target, instance, logs }) { + const button = target.querySelector('button'); + assert.deepEqual(logs, ['effect', 1]); + flushSync(() => { + button?.click(); + }); + assert.deepEqual(logs, ['effect', 1, 'clean', 1, 'effect', 2]); + } +}); diff --git a/packages/svelte/tests/runtime-runes/samples/clean-block-inner-effects/main.svelte b/packages/svelte/tests/runtime-runes/samples/clean-block-inner-effects/main.svelte new file mode 100644 index 0000000000..cd46715a7e --- /dev/null +++ b/packages/svelte/tests/runtime-runes/samples/clean-block-inner-effects/main.svelte @@ -0,0 +1,18 @@ + + +{#if track(count)} +{/if} + +