diff --git a/.changeset/fuzzy-donuts-provide.md b/.changeset/fuzzy-donuts-provide.md new file mode 100644 index 000000000000..ae5333e6ecd9 --- /dev/null +++ b/.changeset/fuzzy-donuts-provide.md @@ -0,0 +1,5 @@ +--- +'svelte': patch +--- + +fix: ensure effect cleanup functions are called with null `this` diff --git a/packages/svelte/src/internal/client/reactivity/effects.js b/packages/svelte/src/internal/client/reactivity/effects.js index fafd3d144205..0e21f70d2cc6 100644 --- a/packages/svelte/src/internal/client/reactivity/effects.js +++ b/packages/svelte/src/internal/client/reactivity/effects.js @@ -245,7 +245,7 @@ export function destroy_effect(effect) { } } - effect.teardown?.(); + effect.teardown?.call(null); if (effect.dom !== null) { remove(effect.dom); diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index a41f8a6f7f33..5c3df99fc61c 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -390,7 +390,7 @@ export function execute_effect(effect) { destroy_effect_children(effect); } - effect.teardown?.(); + effect.teardown?.call(null); var teardown = execute_reaction_fn(effect); effect.teardown = typeof teardown === 'function' ? teardown : null; } finally { diff --git a/packages/svelte/tests/runtime-runes/samples/effect-cleanup/_config.js b/packages/svelte/tests/runtime-runes/samples/effect-cleanup/_config.js index b745e0de3d2f..e61a6bffdda7 100644 --- a/packages/svelte/tests/runtime-runes/samples/effect-cleanup/_config.js +++ b/packages/svelte/tests/runtime-runes/samples/effect-cleanup/_config.js @@ -15,6 +15,6 @@ export default test({ flushSync(() => { b1.click(); }); - assert.deepEqual(log, ['init 0', 'cleanup 2', 'init 2', 'cleanup 4', 'init 4']); + assert.deepEqual(log, ['init 0', 'cleanup 2', null, 'init 2', 'cleanup 4', null, 'init 4']); } }); diff --git a/packages/svelte/tests/runtime-runes/samples/effect-cleanup/main.svelte b/packages/svelte/tests/runtime-runes/samples/effect-cleanup/main.svelte index a67624b76015..1a5f7b632ff0 100644 --- a/packages/svelte/tests/runtime-runes/samples/effect-cleanup/main.svelte +++ b/packages/svelte/tests/runtime-runes/samples/effect-cleanup/main.svelte @@ -8,8 +8,10 @@ log.push('init ' + double); - return () => { + return function() { log.push('cleanup ' + double); + // @ts-expect-error + log.push(this); }; })