Skip to content

Commit

Permalink
[ButtonBase] Fix potential memory leak for multi-touch devices (#19333)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Jan 21, 2020
1 parent a9a5f9f commit 2082679
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
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();
});
});
});

0 comments on commit 2082679

Please sign in to comment.