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

pull/11027/head
Rich Harris 1 year ago committed by GitHub
parent ad11c5087f
commit 3c155e3f3e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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

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

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

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