@@ -3,17 +3,22 @@ import {
3
3
EmbeddedViewRef ,
4
4
Input ,
5
5
NgModule ,
6
+ OnDestroy ,
6
7
TemplateRef ,
7
8
ViewContainerRef ,
8
9
ɵstringify as stringify
9
10
} from '@angular/core' ;
10
11
import { CommonModule } from '@angular/common' ;
12
+ import { distinctUntilChanged , mergeAll , Observable , ReplaySubject , Subscription } from "rxjs" ;
13
+ import { coerceObservable } from "@angular-kit/cdk/coercing" ;
11
14
12
15
@Directive ( {
13
16
// eslint-disable-next-line @angular-eslint/directive-selector
14
17
selector : '[rxIfList]' ,
15
18
} )
16
- export class RxIfListDirective {
19
+ export class RxIfListDirective implements OnDestroy {
20
+ private readonly sub = new Subscription ( ) ;
21
+ private readonly valueSource$$ = new ReplaySubject < Observable < ArrayLike < unknown > | null | undefined > > ( 1 ) ;
17
22
private _context : RxIfListContext = new RxIfListContext ( ) ;
18
23
private _thenTemplateRef : TemplateRef < RxIfListContext > | null = null ;
19
24
private _elseTemplateRef : TemplateRef < RxIfListContext > | null = null ;
@@ -45,15 +50,27 @@ export class RxIfListDirective {
45
50
46
51
constructor ( private _viewContainer : ViewContainerRef , templateRef : TemplateRef < RxIfListContext > ) {
47
52
this . _thenTemplateRef = templateRef ;
53
+ this . sub . add (
54
+ this . valueSource$$ . asObservable ( ) . pipe (
55
+ distinctUntilChanged ( ) ,
56
+ mergeAll ( ) ,
57
+ distinctUntilChanged ( )
58
+ ) . subscribe ( value => {
59
+ this . _context . $implicit = this . _context . rxIfList = value ;
60
+ this . _updateView ( this . _context ) ;
61
+ } )
62
+ )
63
+ }
64
+ ngOnDestroy ( ) {
65
+ this . sub . unsubscribe ( ) ;
48
66
}
49
67
50
68
/**
51
69
* The Boolean expression to evaluate as the condition for showing a template.
52
70
*/
53
71
@Input ( )
54
- set rxIfList ( value : ArrayLike < unknown > | null | undefined ) {
55
- this . _context . $implicit = this . _context . rxIfList = value ;
56
- this . _updateView ( ) ;
72
+ set rxIfList ( value : ArrayLike < unknown > | Observable < ArrayLike < unknown > > | null | undefined ) {
73
+ this . valueSource$$ . next ( coerceObservable ( value ) ) ;
57
74
}
58
75
59
76
/**
@@ -64,7 +81,7 @@ export class RxIfListDirective {
64
81
assertTemplate ( 'rxIfListThen' , templateRef ) ;
65
82
this . _thenTemplateRef = templateRef ;
66
83
this . _thenViewRef = null ; // clear previous view if any.
67
- this . _updateView ( ) ;
84
+ this . _updateView ( this . _context ) ;
68
85
}
69
86
70
87
/**
@@ -75,17 +92,17 @@ export class RxIfListDirective {
75
92
assertTemplate ( 'rxIfListElse' , templateRef ) ;
76
93
this . _elseTemplateRef = templateRef ;
77
94
this . _elseViewRef = null ; // clear previous view if any.
78
- this . _updateView ( ) ;
95
+ this . _updateView ( this . _context ) ;
79
96
}
80
97
81
- private _updateView ( ) {
82
- if ( this . _context . $implicit && ( ( ( this . _context . $implicit as ArrayLike < any > ) ?. length ?? [ ] ) > 0 ) ) {
98
+ private _updateView ( ctx : RxIfListContext ) {
99
+ if ( ctx . $implicit && ( ( ( ctx . $implicit as ArrayLike < any > ) ?. length ?? [ ] ) > 0 ) ) {
83
100
if ( ! this . _thenViewRef ) {
84
101
this . _viewContainer . clear ( ) ;
85
102
this . _elseViewRef = null ;
86
103
if ( this . _thenTemplateRef ) {
87
104
this . _thenViewRef =
88
- this . _viewContainer . createEmbeddedView ( this . _thenTemplateRef , this . _context ) ;
105
+ this . _viewContainer . createEmbeddedView ( this . _thenTemplateRef , ctx ) ;
89
106
}
90
107
}
91
108
} else {
@@ -94,7 +111,7 @@ export class RxIfListDirective {
94
111
this . _thenViewRef = null ;
95
112
if ( this . _elseTemplateRef ) {
96
113
this . _elseViewRef =
97
- this . _viewContainer . createEmbeddedView ( this . _elseTemplateRef , this . _context ) ;
114
+ this . _viewContainer . createEmbeddedView ( this . _elseTemplateRef , ctx ) ;
98
115
}
99
116
}
100
117
}
0 commit comments