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

perf(material/tooltip): Avoid unneeded calls to clearTimeout #29643

Merged
merged 1 commit into from
Aug 28, 2024
Merged
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
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
Loading