@@ -354,7 +354,7 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
354
354
private _document : Document ;
355
355
356
356
/** Timer started at the last `touchstart` event. */
357
- private _touchstartTimeout : ReturnType < typeof setTimeout > ;
357
+ private _touchstartTimeout : null | ReturnType < typeof setTimeout > = null ;
358
358
359
359
/** Emits when the component is destroyed. */
360
360
private readonly _destroyed = new Subject < void > ( ) ;
@@ -434,7 +434,10 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
434
434
ngOnDestroy ( ) {
435
435
const nativeElement = this . _elementRef . nativeElement ;
436
436
437
- clearTimeout ( this . _touchstartTimeout ) ;
437
+ // Optimization: Do not call clearTimeout unless there is an active timer.
438
+ if ( this . _touchstartTimeout ) {
439
+ clearTimeout ( this . _touchstartTimeout ) ;
440
+ }
438
441
439
442
if ( this . _overlayRef ) {
440
443
this . _overlayRef . dispose ( ) ;
@@ -802,13 +805,15 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
802
805
// Note that it's important that we don't `preventDefault` here,
803
806
// because it can prevent click events from firing on the element.
804
807
this . _setupPointerExitEventsIfNeeded ( ) ;
805
- clearTimeout ( this . _touchstartTimeout ) ;
808
+ if ( this . _touchstartTimeout ) {
809
+ clearTimeout ( this . _touchstartTimeout ) ;
810
+ }
806
811
807
812
const DEFAULT_LONGPRESS_DELAY = 500 ;
808
- this . _touchstartTimeout = setTimeout (
809
- ( ) => this . show ( undefined , origin ) ,
810
- this . _defaultOptions . touchLongPressShowDelay ?? DEFAULT_LONGPRESS_DELAY ,
811
- ) ;
813
+ this . _touchstartTimeout = setTimeout ( ( ) => {
814
+ this . _touchstartTimeout = null ;
815
+ this . show ( undefined , origin ) ;
816
+ } , this . _defaultOptions . touchLongPressShowDelay ?? DEFAULT_LONGPRESS_DELAY ) ;
812
817
} ,
813
818
] ) ;
814
819
}
@@ -839,7 +844,9 @@ export class MatTooltip implements OnDestroy, AfterViewInit {
839
844
} else if ( this . touchGestures !== 'off' ) {
840
845
this . _disableNativeGesturesIfNecessary ( ) ;
841
846
const touchendListener = ( ) => {
842
- clearTimeout ( this . _touchstartTimeout ) ;
847
+ if ( this . _touchstartTimeout ) {
848
+ clearTimeout ( this . _touchstartTimeout ) ;
849
+ }
843
850
this . hide ( this . _defaultOptions . touchendHideDelay ) ;
844
851
} ;
845
852
0 commit comments