Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure effect cleanup functions are called with null this #11024

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -245,7 +245,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 @@ -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 {
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
Loading