Skip to content

Commit

Permalink
fix: ensure effect cleanup functions are called with null this (#11024
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Rich-Harris authored Apr 2, 2024
1 parent ad11c50 commit 3c155e3
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-donuts-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: ensure effect cleanup functions are called with null `this`
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/reactivity/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export function destroy_effect(effect) {
}
}

effect.teardown?.();
effect.teardown?.call(null);

if (effect.dom !== null) {
remove(effect.dom);
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
log.push('init ' + double);
return () => {
return function() {
log.push('cleanup ' + double);
// @ts-expect-error
log.push(this);
};
})
</script>
Expand Down

0 comments on commit 3c155e3

Please sign in to comment.