Skip to content

Commit

Permalink
fix(motion): avoid memory leak in Animation:finished (#33431)
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter authored Dec 9, 2024
1 parent 39feb05 commit 80f2ce0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: avoid memory leak in Animation:finished",
"packageName": "@fluentui/react-motion",
"email": "olfedias@microsoft.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ function createElementMock() {
cancel: jest.fn(),
persist: jest.fn(),
finish: finishMock,
finished: Promise.resolve(),
set onfinish(fn: () => void) {
fn();
},
}));
const ElementMock = React.forwardRef<{ animate: () => void }, { onRender?: () => void }>((props, ref) => {
React.useImperativeHandle(ref, () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ function createElementMock() {
cancel: jest.fn(),
persist: jest.fn(),
finish: finishMock,
finished: Promise.resolve(),

set onfinish(fn: () => void) {
fn();
},
}));
const ElementMock = React.forwardRef<{ animate: () => void }, { onRender?: () => void }>((props, ref) => {
React.useImperativeHandle(ref, () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,22 @@ function useAnimateAtomsInSupportedEnvironment() {
});
},
setMotionEndCallbacks(onfinish: () => void, oncancel: () => void) {
Promise.all(animations.map(animation => animation.finished))
// Heads up!
// This could use "Animation:finished", but it's causing a memory leak in Chromium.
// See: https://issues.chromium.org/u/2/issues/383016426
const promises = animations.map(animation => {
return new Promise<void>((resolve, reject) => {
animation.onfinish = () => resolve();
animation.oncancel = () => reject();
});
});

Promise.all(promises)
.then(() => {
onfinish();
})
.catch((err: unknown) => {
const DOMException = element.ownerDocument.defaultView?.DOMException;

// Ignores "DOMException: The user aborted a request" that appears if animations are cancelled
if (DOMException && err instanceof DOMException && err.name === 'AbortError') {
oncancel();
return;
}

throw err;
.catch(() => {
oncancel();
});
},

Expand Down

0 comments on commit 80f2ce0

Please sign in to comment.