@@ -16,11 +16,10 @@ import {
16
16
afterNextRender ,
17
17
afterRender ,
18
18
untracked ,
19
- AfterRenderRef ,
20
19
} from '@angular/core' ;
21
20
import { Location } from '@angular/common' ;
22
21
import { Observable , Subject , merge , SubscriptionLike , Subscription } from 'rxjs' ;
23
- import { takeUntil } from 'rxjs/operators' ;
22
+ import { take } from 'rxjs/operators' ;
24
23
import { OverlayKeyboardDispatcher } from './dispatchers/overlay-keyboard-dispatcher' ;
25
24
import { OverlayOutsideClickDispatcher } from './dispatchers/overlay-outside-click-dispatcher' ;
26
25
import { OverlayConfig } from './overlay-config' ;
@@ -39,7 +38,7 @@ export type ImmutableObject<T> = {
39
38
*/
40
39
export class OverlayRef implements PortalOutlet {
41
40
private _backdropElement : HTMLElement | null = null ;
42
- private _backdropTimeout : number | undefined ;
41
+ private _backdropTimeout : ReturnType < typeof setTimeout > | undefined ;
43
42
private readonly _backdropClick = new Subject < MouseEvent > ( ) ;
44
43
private readonly _attachments = new Subject < void > ( ) ;
45
44
private readonly _detachments = new Subject < void > ( ) ;
@@ -63,10 +62,6 @@ export class OverlayRef implements PortalOutlet {
63
62
/** Stream of mouse outside events dispatched to this overlay. */
64
63
readonly _outsidePointerEvents = new Subject < MouseEvent > ( ) ;
65
64
66
- private _renders = new Subject < void > ( ) ;
67
-
68
- private _afterRenderRef : AfterRenderRef ;
69
-
70
65
constructor (
71
66
private _portalOutlet : PortalOutlet ,
72
67
private _host : HTMLElement ,
@@ -86,18 +81,6 @@ export class OverlayRef implements PortalOutlet {
86
81
}
87
82
88
83
this . _positionStrategy = _config . positionStrategy ;
89
-
90
- // Users could open the overlay from an `effect`, in which case we need to
91
- // run the `afterRender` as `untracked`. We don't recommend that users do
92
- // this, but we also don't want to break users who are doing it.
93
- this . _afterRenderRef = untracked ( ( ) =>
94
- afterRender (
95
- ( ) => {
96
- this . _renders . next ( ) ;
97
- } ,
98
- { injector : this . _injector } ,
99
- ) ,
100
- ) ;
101
84
}
102
85
103
86
/** The overlay's HTML element */
@@ -275,8 +258,6 @@ export class OverlayRef implements PortalOutlet {
275
258
}
276
259
277
260
this . _detachments . complete ( ) ;
278
- this . _afterRenderRef . destroy ( ) ;
279
- this . _renders . complete ( ) ;
280
261
}
281
262
282
263
/** Whether the overlay has attached content. */
@@ -516,28 +497,38 @@ export class OverlayRef implements PortalOutlet {
516
497
// Normally we wouldn't have to explicitly run this outside the `NgZone`, however
517
498
// if the consumer is using `zone-patch-rxjs`, the `Subscription.unsubscribe` call will
518
499
// be patched to run inside the zone, which will throw us into an infinite loop.
500
+ // We can't remove the host here immediately, because the overlay pane's content
501
+ // might still be animating. This stream helps us avoid interrupting the animation
502
+ // by waiting for the pane to become empty.
519
503
this . _ngZone . runOutsideAngular ( ( ) => {
520
- // We can't remove the host here immediately, because the overlay pane's content
521
- // might still be animating. This stream helps us avoid interrupting the animation
522
- // by waiting for the pane to become empty.
523
- const subscription = this . _renders
524
- . pipe ( takeUntil ( merge ( this . _attachments , this . _detachments ) ) )
525
- . subscribe ( ( ) => {
526
- // Needs a couple of checks for the pane and host, because
527
- // they may have been removed by the time the zone stabilizes.
528
- if ( ! this . _pane || ! this . _host || this . _pane . children . length === 0 ) {
529
- if ( this . _pane && this . _config . panelClass ) {
530
- this . _toggleClasses ( this . _pane , this . _config . panelClass , false ) ;
504
+ // Users could open the overlay from an `effect`, in which case we need to
505
+ // run the `afterRender` as `untracked`. We don't recommend that users do
506
+ // this, but we also don't want to break users who are doing it.
507
+ const ref = untracked ( ( ) =>
508
+ afterRender (
509
+ ( ) => {
510
+ // Needs a couple of checks for the pane and host, because
511
+ // they may have been removed by the time the zone stabilizes.
512
+ if ( ! this . _pane || ! this . _host || this . _pane . children . length === 0 ) {
513
+ if ( this . _pane && this . _config . panelClass ) {
514
+ this . _toggleClasses ( this . _pane , this . _config . panelClass , false ) ;
515
+ }
516
+
517
+ if ( this . _host && this . _host . parentElement ) {
518
+ this . _previousHostParent = this . _host . parentElement ;
519
+ this . _host . remove ( ) ;
520
+ }
521
+
522
+ ref . destroy ( ) ;
531
523
}
532
-
533
- if ( this . _host && this . _host . parentElement ) {
534
- this . _previousHostParent = this . _host . parentElement ;
535
- this . _host . remove ( ) ;
536
- }
537
-
538
- subscription . unsubscribe ( ) ;
539
- }
540
- } ) ;
524
+ } ,
525
+ { injector : this . _injector } ,
526
+ ) ,
527
+ ) ;
528
+
529
+ merge ( this . _attachments , this . _detachments )
530
+ . pipe ( take ( 1 ) )
531
+ . subscribe ( ( ) => ref . destroy ( ) ) ;
541
532
} ) ;
542
533
}
543
534
0 commit comments