From 3c155e3f3e930408b7c8fdd3d8423b3d6efdb3d6 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 2 Apr 2024 05:16:54 -0400 Subject: [PATCH] fix: ensure effect cleanup functions are called with null `this` (#11024) --- .changeset/fuzzy-donuts-provide.md | 5 +++++ packages/svelte/src/internal/client/reactivity/effects.js | 2 +- packages/svelte/src/internal/client/runtime.js | 2 +- .../tests/runtime-runes/samples/effect-cleanup/_config.js | 2 +- .../tests/runtime-runes/samples/effect-cleanup/main.svelte | 4 +++- 5 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 .changeset/fuzzy-donuts-provide.md 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 fd478fc3dce5..5506737e3172 100644 --- a/packages/svelte/src/internal/client/reactivity/effects.js +++ b/packages/svelte/src/internal/client/reactivity/effects.js @@ -244,7 +244,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 8f4c275810f4..d3d7e88412aa 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -391,7 +391,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); }; })