fix: cleanup non-branch effects created inside block effects (#13600)

* fix: cleanup non-branch effects created inside block effects

* cleaner
pull/13545/head
Dominic Gannaway 2 months ago committed by GitHub
parent eafddf4358
commit 4dcac41c10
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: cleanup non-branch effects created inside block effects

@ -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 * @param {Effect} effect
* @returns {void} * @returns {void}
@ -454,7 +470,9 @@ export function update_effect(effect) {
} }
try { try {
if ((flags & BLOCK_EFFECT) === 0) { if ((flags & BLOCK_EFFECT) !== 0) {
destroy_block_effect_children(effect);
} else {
destroy_effect_children(effect); destroy_effect_children(effect);
} }

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

@ -0,0 +1,18 @@
<script>
let count = $state(1);
function track(value){
let val = value;
$effect(() => {
console.log("effect", val);
return ()=>{
console.log("clean", val);
}
});
return value;
}
</script>
{#if track(count)}
{/if}
<button onclick={()=> count++}></button>
Loading…
Cancel
Save