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

[ButtonBase] Fix potential memory leak for multi-touch devices #19333

Merged
merged 2 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 16 additions & 11 deletions packages/material-ui/src/ButtonBase/TouchRipple.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,22 @@ const TouchRipple = React.forwardRef(function TouchRipple(props, ref) {

// Touche devices
if (event.touches) {
// Prepare the ripple effect.
startTimerCommit.current = () => {
startCommit({ pulsate, rippleX, rippleY, rippleSize, cb });
};
// Delay the execution of the ripple effect.
startTimer.current = setTimeout(() => {
if (startTimerCommit.current) {
startTimerCommit.current();
startTimerCommit.current = null;
}
}, DELAY_RIPPLE); // We have to make a tradeoff with this value.
// check that this isn't another touchstart due to multitouch
// otherwise we will only clear a single timer when unmounting while two
// are running
if (startTimerCommit.current === null) {
// Prepare the ripple effect.
startTimerCommit.current = () => {
startCommit({ pulsate, rippleX, rippleY, rippleSize, cb });
};
// Delay the execution of the ripple effect.
startTimer.current = setTimeout(() => {
if (startTimerCommit.current) {
startTimerCommit.current();
startTimerCommit.current = null;
}
}, DELAY_RIPPLE); // We have to make a tradeoff with this value.
}
} else {
startCommit({ pulsate, rippleX, rippleY, rippleSize, cb });
}
Expand Down
18 changes: 17 additions & 1 deletion packages/material-ui/src/ButtonBase/TouchRipple.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('<TouchRipple />', () => {
*/
function renderTouchRipple(other) {
const touchRippleRef = React.createRef();
const { container } = render(
const { container, unmount } = render(
<TouchRipple
ref={touchRippleRef}
classes={{
Expand All @@ -42,6 +42,7 @@ describe('<TouchRipple />', () => {
queryRipple() {
return container.querySelector('.ripple');
},
unmount,
};
}

Expand Down Expand Up @@ -155,6 +156,9 @@ describe('<TouchRipple />', () => {
});

describe('mobile', () => {
/**
* @type {ReturnType<typeof useFakeTimers>}
*/
let clock;

before(() => {
Expand Down Expand Up @@ -227,5 +231,17 @@ describe('<TouchRipple />', () => {
expect(queryAllActiveRipples()).to.have.lengthOf(0);
expect(queryAllStoppingRipples()).to.have.lengthOf(0);
});

it('should not leak on multi-touch', function multiTouchTest() {
const { instance, unmount } = renderTouchRipple();

instance.start({ type: 'touchstart', touches: [{}] }, () => {});
instance.start({ type: 'touchstart', touches: [{}] }, () => {});
unmount();

// expect this to run gracefully without
// "react state update on an unmounted component"
clock.runAll();
});
});
});