fix: prevent numerous transition/animation memory leaks (#12759)

* fix: prevent numerous transition/animation memory leaks

* address feedback

* tweak
pull/12795/head
Dominic Gannaway 3 months ago committed by GitHub
parent ba116a1b43
commit d2efca0b04
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: prevent numerous transition/animation memory leaks

@ -222,6 +222,8 @@ export function transition(flags, element, get_fn, get_params) {
1, 1,
() => { () => {
dispatch_event(element, 'introend'); dispatch_event(element, 'introend');
// Ensure we cancel the animation to prevent leaking
intro?.abort();
intro = current_options = undefined; intro = current_options = undefined;
}, },
is_both is_both
@ -249,6 +251,8 @@ export function transition(flags, element, get_fn, get_params) {
0, 0,
() => { () => {
dispatch_event(element, 'outroend'); dispatch_event(element, 'outroend');
// Ensure we cancel the animation to prevent leaking
outro?.abort();
outro = current_options = undefined; outro = current_options = undefined;
fn?.(); fn?.();
}, },
@ -322,8 +326,10 @@ function animate(element, options, counterpart, t2, on_finish, on_abort) {
// once DOM has been updated... // once DOM has been updated...
/** @type {Animation} */ /** @type {Animation} */
var a; var a;
var aborted = false;
queue_micro_task(() => { queue_micro_task(() => {
if (aborted) return;
var o = options({ direction: is_intro ? 'in' : 'out' }); var o = options({ direction: is_intro ? 'in' : 'out' });
a = animate(element, o, counterpart, t2, on_finish, on_abort); a = animate(element, o, counterpart, t2, on_finish, on_abort);
}); });
@ -331,7 +337,10 @@ function animate(element, options, counterpart, t2, on_finish, on_abort) {
// ...but we want to do so without using `async`/`await` everywhere, so // ...but we want to do so without using `async`/`await` everywhere, so
// we return a facade that allows everything to remain synchronous // we return a facade that allows everything to remain synchronous
return { return {
abort: () => a.abort(), abort: () => {
aborted = true;
a?.abort();
},
deactivate: () => a.deactivate(), deactivate: () => a.deactivate(),
reset: () => a.reset(), reset: () => a.reset(),
t: (now) => a.t(now) t: (now) => a.t(now)

@ -20,9 +20,6 @@
const component = render(App, { const component = render(App, {
target: document.getElementById('root') target: document.getElementById('root')
}); });
// @ts-ignore
window.unmount = () => unmount(component);
</script> </script>
</body> </body>
</html> </html>

Loading…
Cancel
Save