@@ -2,9 +2,11 @@ import {
22AfterViewInit ,
33ChangeDetectorRef ,
44ComponentRef ,
5+ DestroyRef ,
56Directive ,
67ElementRef ,
78HostBinding ,
9+ inject ,
810Inject ,
911Input ,
1012OnChanges ,
@@ -15,9 +17,9 @@ import {
1517TemplateRef ,
1618ViewContainerRef
1719} from '@angular/core' ;
20+ import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
1821import { DOCUMENT } from '@angular/common' ;
19- import { Subscription } from 'rxjs' ;
20- import { debounceTime } from 'rxjs/operators' ;
22+ import { debounceTime , finalize } from 'rxjs/operators' ;
2123import { createPopper , Instance , Options } from '@popperjs/core' ;
2224
2325import { Triggers } from '../coreui.types' ;
@@ -96,7 +98,7 @@ export class TooltipDirective implements OnChanges, OnDestroy, OnInit, AfterView
9698]
9799} ;
98100
99- private intersectingSubscription ?: Subscription ;
101+ readonly #destroyRef = inject ( DestroyRef ) ;
100102
101103constructor (
102104 @Inject ( DOCUMENT ) private document :Document ,
@@ -109,7 +111,6 @@ export class TooltipDirective implements OnChanges, OnDestroy, OnInit, AfterView
109111) { }
110112
111113ngAfterViewInit ( ) :void {
112- this . intersectionService . createIntersectionObserver ( this . hostElement ) ;
113114this . intersectionServiceSubscribe ( ) ;
114115}
115116
@@ -122,7 +123,6 @@ export class TooltipDirective implements OnChanges, OnDestroy, OnInit, AfterView
122123ngOnDestroy ( ) :void {
123124this . clearListeners ( ) ;
124125this . destroyTooltipElement ( ) ;
125- this . intersectionServiceSubscribe ( false ) ;
126126}
127127
128128ngOnInit ( ) :void {
@@ -154,18 +154,19 @@ export class TooltipDirective implements OnChanges, OnDestroy, OnInit, AfterView
154154}
155155
156156private intersectionServiceSubscribe ( subscribe :boolean = true ) :void {
157- if ( subscribe ) {
158- this . intersectingSubscription = this . intersectionService . intersecting$
159- . pipe (
160- debounceTime ( 100 )
161- )
162- . subscribe ( isIntersecting => {
163- this . visible = isIntersecting ?this . visible :false ;
164- ! this . visible && this . removeTooltipElement ( ) ;
165- } ) ;
166- } else {
167- this . intersectingSubscription ?. unsubscribe ( ) ;
168- }
157+ this . intersectionService . createIntersectionObserver ( this . hostElement ) ;
158+ this . intersectionService . intersecting$
159+ . pipe (
160+ debounceTime ( 100 ) ,
161+ finalize ( ( ) => {
162+ this . intersectionService . unobserve ( this . hostElement ) ;
163+ } ) ,
164+ takeUntilDestroyed ( this . #destroyRef)
165+ )
166+ . subscribe ( isIntersecting => {
167+ this . visible = isIntersecting ?this . visible :false ;
168+ ! this . visible && this . removeTooltipElement ( ) ;
169+ } ) ;
169170}
170171
171172private getUID ( prefix :string ) :string {