Skip to content

Commit

Permalink
perf(material/tooltip): Avoid unneeded calls to clearTimeout (#29643)
Browse files Browse the repository at this point in the history
(cherry picked from commit 64cf19c)
  • Loading branch information
kseamon authored and crisbeto committed Aug 28, 2024
1 parent 3ce4e9f commit f93d0f4
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/material/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
private _document: Document;

/** Timer started at the last `touchstart` event. */
private _touchstartTimeout: ReturnType<typeof setTimeout>;
private _touchstartTimeout: null | ReturnType<typeof setTimeout> = null;

/** Emits when the component is destroyed. */
private readonly _destroyed = new Subject<void>();
Expand Down Expand Up @@ -434,7 +434,10 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
ngOnDestroy() {
const nativeElement = this._elementRef.nativeElement;

clearTimeout(this._touchstartTimeout);
// Optimization: Do not call clearTimeout unless there is an active timer.
if (this._touchstartTimeout) {
clearTimeout(this._touchstartTimeout);
}

if (this._overlayRef) {
this._overlayRef.dispose();
Expand Down Expand Up @@ -802,13 +805,15 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
// Note that it's important that we don't `preventDefault` here,
// because it can prevent click events from firing on the element.
this._setupPointerExitEventsIfNeeded();
clearTimeout(this._touchstartTimeout);
if (this._touchstartTimeout) {
clearTimeout(this._touchstartTimeout);
}

const DEFAULT_LONGPRESS_DELAY = 500;
this._touchstartTimeout = setTimeout(
() => this.show(undefined, origin),
this._defaultOptions.touchLongPressShowDelay ?? DEFAULT_LONGPRESS_DELAY,
);
this._touchstartTimeout = setTimeout(() => {
this._touchstartTimeout = null;
this.show(undefined, origin);
}, this._defaultOptions.touchLongPressShowDelay ?? DEFAULT_LONGPRESS_DELAY);
},
]);
}
Expand Down Expand Up @@ -839,7 +844,9 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
} else if (this.touchGestures !== 'off') {
this._disableNativeGesturesIfNecessary();
const touchendListener = () => {
clearTimeout(this._touchstartTimeout);
if (this._touchstartTimeout) {
clearTimeout(this._touchstartTimeout);
}
this.hide(this._defaultOptions.touchendHideDelay);
};

Expand Down

0 comments on commit f93d0f4

Please sign in to comment.