@@ -16,22 +16,44 @@ import {
16
16
} from '@angular/core' ;
17
17
import { RxStrategyProvider } from '@rx-angular/cdk/render-strategies' ;
18
18
import { RxDefaultListViewContext } from '@rx-angular/cdk/template' ;
19
- import { concat , forkJoin , Observable , ObservableInput , of , ReplaySubject , Subject , Subscription } from 'rxjs' ;
20
- import { catchError , distinctUntilChanged , filter , map , shareReplay , switchAll , switchMap , take , tap } from 'rxjs/operators' ;
19
+ import {
20
+ concat ,
21
+ forkJoin ,
22
+ Observable ,
23
+ ObservableInput ,
24
+ of ,
25
+ ReplaySubject ,
26
+ Subject ,
27
+ Subscription ,
28
+ } from 'rxjs' ;
29
+ import {
30
+ catchError ,
31
+ distinctUntilChanged ,
32
+ filter ,
33
+ map ,
34
+ shareReplay ,
35
+ switchAll ,
36
+ switchMap ,
37
+ take ,
38
+ tap ,
39
+ } from 'rxjs/operators' ;
21
40
22
41
@Directive ( {
23
- selector : '[rxForNormal]'
42
+ selector : '[rxForNormal]' ,
24
43
} )
25
44
export class RxForNormal < T , U extends NgIterable < T > = NgIterable < T > >
26
- implements OnInit , OnDestroy {
45
+ implements OnInit , OnDestroy
46
+ {
27
47
@Input ( )
28
48
set rxForNormal ( potentialObservable : ObservableInput < U > | null | undefined ) {
29
49
this . _rxFor = potentialObservable ;
30
50
this . observables$ . next ( potentialObservable ) ;
31
51
}
32
52
33
53
@Input ( )
34
- set rxForNormalOf ( potentialObservable : ObservableInput < U > | null | undefined ) {
54
+ set rxForNormalOf (
55
+ potentialObservable : ObservableInput < U > | null | undefined
56
+ ) {
35
57
this . _rxFor = potentialObservable ;
36
58
this . observables$ . next ( potentialObservable ) ;
37
59
}
@@ -59,7 +81,6 @@ export class RxForNormal<T, U extends NgIterable<T> = NgIterable<T>>
59
81
private iterableDiffers : IterableDiffers
60
82
) { }
61
83
62
-
63
84
private differ : IterableDiffer < T > | null = null ;
64
85
private observables$ = new ReplaySubject < ObservableInput < U > > ( 1 ) ;
65
86
private readonly strategies = this . strategyProvider . strategies ;
@@ -105,25 +126,23 @@ export class RxForNormal<T, U extends NgIterable<T> = NgIterable<T>>
105
126
rxForDistinctBy = ( a , b ) => a . value === b . value ;
106
127
107
128
initDiffer ( iterable : U = [ ] as U ) {
108
- this . differ = this . iterableDiffers
109
- . find ( iterable )
110
- . create ( this . _trackByFn ) ;
129
+ this . differ = this . iterableDiffers . find ( iterable ) . create ( this . _trackByFn ) ;
111
130
}
112
131
113
132
ngOnInit ( ) {
114
133
this . sub . add (
115
134
concat (
116
135
this . values$ . pipe (
117
136
take ( 1 ) ,
118
- tap ( value => this . initDiffer ( value || ( [ ] as any ) ) )
137
+ tap ( ( value ) => this . initDiffer ( value || ( [ ] as any ) ) )
119
138
) ,
120
139
this . values$
121
140
)
122
141
. pipe (
123
- map ( i => this . differ . diff ( i ) ) ,
124
- filter ( diff => ! ! diff ) ,
125
- switchMap ( diff => this . applyChanges ( diff ) ) ,
126
- catchError ( e => {
142
+ map ( ( i ) => this . differ . diff ( i ) ) ,
143
+ filter ( ( diff ) => ! ! diff ) ,
144
+ switchMap ( ( diff ) => this . applyChanges ( diff ) ) ,
145
+ catchError ( ( e ) => {
127
146
console . error ( e ) ;
128
147
return of ( null ) ;
129
148
} ) ,
@@ -144,7 +163,10 @@ export class RxForNormal<T, U extends NgIterable<T> = NgIterable<T>>
144
163
const strat = this . strategies [ this . strategy ] ;
145
164
146
165
const insertMap = new Map < number , RxDefaultListViewContext < T , U > > ( ) ;
147
- const scheduleInsert = ( idx : number , ctx : RxDefaultListViewContext < T , U > ) => {
166
+ const scheduleInsert = (
167
+ idx : number ,
168
+ ctx : RxDefaultListViewContext < T , U >
169
+ ) => {
148
170
if ( ! insertMap . has ( idx ) ) {
149
171
insertMap . set ( idx , ctx ) ;
150
172
const insert = new Subject < void > ( ) ;
@@ -163,7 +185,7 @@ export class RxForNormal<T, U extends NgIterable<T> = NgIterable<T>>
163
185
} ;
164
186
this . sub . add (
165
187
of ( null )
166
- . pipe ( strat . behavior ( work , this ) , take ( 1 ) )
188
+ . pipe ( strat . behavior ( { work, scope : this } ) , take ( 1 ) )
167
189
. subscribe ( insert )
168
190
) ;
169
191
behaviors$ . push ( insert ) ;
@@ -172,7 +194,7 @@ export class RxForNormal<T, U extends NgIterable<T> = NgIterable<T>>
172
194
const updateMap = new WeakMap <
173
195
EmbeddedViewRef < any > ,
174
196
( ( context : RxDefaultListViewContext < T , U > ) => void ) [ ]
175
- > ( ) ;
197
+ > ( ) ;
176
198
const scheduleUpdate = (
177
199
idx : number ,
178
200
update : ( context : RxDefaultListViewContext < T , U > ) => void
@@ -188,11 +210,11 @@ export class RxForNormal<T, U extends NgIterable<T> = NgIterable<T>>
188
210
// detach the view so that the parent cd cycle does not render this view
189
211
const work = ( ) => {
190
212
view . reattach ( ) ;
191
- updateMap . get ( view ) . forEach ( u => u ( view . context ) ) ;
213
+ updateMap . get ( view ) . forEach ( ( u ) => u ( view . context ) ) ;
192
214
strat . work ( view ) ;
193
215
} ;
194
216
behaviors$ . push (
195
- of ( null ) . pipe ( strat . behavior ( work , view ) , take ( 1 ) )
217
+ of ( null ) . pipe ( strat . behavior ( { work, scope : view as any } ) , take ( 1 ) )
196
218
) ;
197
219
}
198
220
} else if ( insertMap . has ( idx ) ) {
@@ -216,8 +238,7 @@ export class RxForNormal<T, U extends NgIterable<T> = NgIterable<T>>
216
238
detectParent = true ;
217
239
} else if ( currentIndex == null ) {
218
240
// remove
219
- const i =
220
- previousIndex === null ? undefined : previousIndex ;
241
+ const i = previousIndex === null ? undefined : previousIndex ;
221
242
if ( this . viewContainerRef . get ( i ) ) {
222
243
this . viewContainerRef . remove ( i ) ;
223
244
// a view got removed, notify parent about the change
@@ -230,7 +251,7 @@ export class RxForNormal<T, U extends NgIterable<T> = NgIterable<T>>
230
251
) ;
231
252
this . viewContainerRef . move ( view , idx ) ;
232
253
const $implicit = r . item ;
233
- scheduleUpdate ( idx , ctx => {
254
+ scheduleUpdate ( idx , ( ctx ) => {
234
255
ctx . $implicit = $implicit ;
235
256
} ) ;
236
257
}
@@ -239,10 +260,7 @@ export class RxForNormal<T, U extends NgIterable<T> = NgIterable<T>>
239
260
// if views only had identityChanges, update the $implict value
240
261
changes . forEachIdentityChange ( ( record : IterableChangeRecord < T > ) => {
241
262
const $implicit = record . item ;
242
- scheduleUpdate (
243
- record . currentIndex ,
244
- ctx => ( ctx . $implicit = $implicit )
245
- ) ;
263
+ scheduleUpdate ( record . currentIndex , ( ctx ) => ( ctx . $implicit = $implicit ) ) ;
246
264
} ) ;
247
265
// update view contexts (index, count, odd/even and stuff)
248
266
const count = this . viewContainerRef . length + insertMap . size ;
@@ -254,9 +272,9 @@ export class RxForNormal<T, U extends NgIterable<T> = NgIterable<T>>
254
272
first : index === 0 ,
255
273
last : index === count - 1 ,
256
274
even,
257
- odd : ! even
275
+ odd : ! even ,
258
276
} ;
259
- scheduleUpdate ( index , ctx => {
277
+ scheduleUpdate ( index , ( ctx ) => {
260
278
ctx . updateContext ( newCtx ) ;
261
279
} ) ;
262
280
}
@@ -268,9 +286,9 @@ export class RxForNormal<T, U extends NgIterable<T> = NgIterable<T>>
268
286
first : index === 0 ,
269
287
last : index === count - 1 ,
270
288
even,
271
- odd : ! even
289
+ odd : ! even ,
272
290
} ;
273
- scheduleUpdate ( index , ctx => {
291
+ scheduleUpdate ( index , ( ctx ) => {
274
292
ctx . updateContext ( newCtx ) ;
275
293
} ) ;
276
294
}
@@ -281,11 +299,10 @@ export class RxForNormal<T, U extends NgIterable<T> = NgIterable<T>>
281
299
// console.log('parent notified');
282
300
} ,
283
301
strategy : this . strategy ,
284
- scope : ( this . cdRef as any ) . context
302
+ scope : ( this . cdRef as any ) . context ,
285
303
} ) ;
286
304
} ) ;
287
305
}
288
306
return forkJoin ( behaviors$ ) ;
289
307
}
290
-
291
308
}
0 commit comments