fix: ensure effect cleanup functions are called with null `this`

pull/11024/head
Rich Harris 2 years ago
parent 0a162924fb
commit fb78f86ea4

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: ensure effect cleanup functions are called with null `this`

@ -245,7 +245,7 @@ export function destroy_effect(effect) {
}
}
effect.teardown?.();
effect.teardown?.call(null);
if (effect.dom !== null) {
remove(effect.dom);

@ -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 {

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

@ -8,8 +8,10 @@
log.push('init ' + double);
return () => {
return function() {
log.push('cleanup ' + double);
// @ts-expect-error
log.push(this);
};
})
</script>

Loading…
Cancel
Save