import { coerceBooleanProperty } from '@angular/cdk/coercion' ; import { ChangeDetectorRef , Directive , ElementRef , EmbeddedViewRef , ErrorHandler , Input , IterableDiffers , NgIterable , NgZone , OnDestroy , OnInit , TemplateRef , TrackByFunction , ViewContainerRef } from '@angular/core' ; import { createListTemplateManager , RxListManager , RxListViewComputedContext , RxListViewContext , RxDefaultListViewContext , coerceDistinctWith , RxStrategyProvider } from '@rx-angular/cdk' ; import { ReplaySubject , Subject , Observable , Subscription } from 'rxjs' ; Directive description docsCategory docsPage publicApi @ Directive ( { selector : '[rxFor]' } ) export class RxFor < T , U extends NgIterable < T > = NgIterable < T > > implements OnInit , OnDestroy { internal static ngTemplateGuard_rxFor : 'binding' ; description example param potentialObservable @ Input ( ) set rxFor ( potentialObservable ) { this . observables$ . next ( potentialObservable ) ; } internal @ Input ( ) set rxForOf ( potentialObservable ) { this . observables$ . next ( potentialObservable ) ; } description example param strategyName see strategies @ Input ( 'rxForStrategy' ) set strategy ( strategyName ) { this . strategyInput$ . next ( strategyName ) ; } description example param renderParent @ Input ( 'rxForParent' ) renderParent = true ; description example param patchZone @ Input ( 'rxForPatchZone' ) patchZone = this . strategyProvider . config . patchZone ; description example param trackByFnOrKey @ Input ( 'rxForTrackBy' ) set trackBy ( trackByFnOrKey ) { this . _trackBy = typeof trackByFnOrKey !== 'function' ? ( i , a ) => a [ trackByFnOrKey ] : trackByFnOrKey ; } internal internal param distinctBy set distinctBy ( distinctBy ) { this . _distinctBy = distinctBy ; } description example param renderCallback @ Input ( 'rxForRenderCallback' ) set renderCallback ( renderCallback ) { this . _renderCallback = renderCallback ; } internal constructor ( private iterableDiffers , private cdRef , private ngZone , private eRef , private readonly templateRef , private readonly viewContainerRef , private strategyProvider , private errorHandler ) { } internal private strategyInput$ = new ReplaySubject < string | Observable < string > > ( 1 ) ; internal private observables$ = new ReplaySubject < Observable < NgIterable < T > > | NgIterable < T > > ( 1 ) ; internal private _renderCallback : Subject < any > ; internal private readonly values$ = this . observables$ . pipe ( coerceDistinctWith ( ) ) ; internal private readonly strategy$ = this . strategyInput$ . pipe ( coerceDistinctWith ( ) ) ; internal private listManager : RxListManager < T > ; internal private _subscription = Subscription . EMPTY ; internal static ngTemplateContextGuard < U > ( dir , ctx ) { return true ; } internal _trackBy : TrackByFunction < T > = ( i , a ) => a ; internal _distinctBy = ( a , b ) => a === b ; internal ngOnInit ( ) { this . listManager = createListTemplateManager < T , RxDefaultListViewContext < T > > ( { iterableDiffers : this . iterableDiffers , renderSettings : { cdRef : this . cdRef , eRef : this . eRef , strategies : this . strategyProvider . strategies as any , defaultStrategyName : this . strategyProvider . primaryStrategy , parent : coerceBooleanProperty ( this . renderParent ) , patchZone : this . patchZone ? this . ngZone : false , errorHandler : this . errorHandler } , templateSettings : { viewContainerRef : this . viewContainerRef , templateRef : this . templateRef , createViewContext : this . createViewContext , updateViewContext : this . updateViewContext , } , trackBy : this . _trackBy } ) ; this . listManager . nextStrategy ( this . strategy$ ) ; this . _subscription = this . listManager . render ( this . values$ ) . subscribe ( v => this . _renderCallback ?. next ( v ) ) ; } internal createViewContext ( item , computedContext ) { return new RxDefaultListViewContext < T > ( item , computedContext ) ; } internal updateViewContext ( item , view , computedContext ) { view . context . updateContext ( computedContext ) ; view . context . $implicit = item ; } internal ngOnDestroy ( ) { this . _subscription . unsubscribe ( ) ; this . viewContainerRef . clear ( ) ; } }