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: improved memory profile for transitions/animations #12796

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/thin-papayas-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: improved memory profile for transitions/animations
34 changes: 14 additions & 20 deletions packages/svelte/src/internal/client/dom/elements/transitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,25 +404,13 @@ function animate(element, options, counterpart, t2, on_finish, on_abort) {
fill: 'forwards'
});

animation.finished
.then(() => {
on_finish?.();

if (t2 === 1) {
animation.cancel();
}
})
.catch((e) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The catch is necessary to prevent an unhandled rejection when a transition is aborted:

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, I remember now. That sucks :/ can't think of why it causes FF to leak. I've tried making the animation undefined inside it but that doesn't help.

// Error for DOMException: The user aborted a request. This results in two things:
// - startTime is `null`
// - currentTime is `null`
// We can't use the existence of an AbortError as this error and error code is shared
// with other Web APIs such as fetch().

if (animation.startTime !== null && animation.currentTime !== null) {
throw e;
}
});
animation.finished.then(() => {
on_finish?.();

if (t2 === 1) {
animation.cancel();
}
});
});
} else {
// Timer
Expand All @@ -448,9 +436,15 @@ function animate(element, options, counterpart, t2, on_finish, on_abort) {

return {
abort: () => {
animation?.cancel();
if (animation) {
animation.cancel();
// This prevents memory leaks in Chromium
animation.effect = null;
}
task?.abort();
on_abort?.();
on_finish = undefined;
on_abort = undefined;
},
deactivate: () => {
on_finish = undefined;
Expand Down
Loading