import { ChangeDetectionStrategy , Component , OnDestroy } from '@angular/core' ; import { select , Store } from '@ngrx/store' ; import { rootEntities } from 'ngrx-entity-relationship' ; import { combineLatest , Observable } from 'rxjs' ; import { filter , map , switchMap } from 'rxjs/operators' ; import { Company } from './store/company/company.model' ; import { EntityService } from './store/entity.service' ; import { relAddressCompany , rootCompany , relCompanyAddress , relCompanyAdmin , rootCompanyStaff , selectCurrentCompanyId , selectCurrentUsersIds , State , rootUser , relUserCompany , relUserEmployees , relUserManager , } from './store/reducers' ; import { User } from './store/user/user.model' ; @ Component ( { selector : 'app-entity' , templateUrl : './entity.component.html' , changeDetection : ChangeDetectionStrategy . OnPush , } ) export class EntityComponent implements OnDestroy { public readonly company$ : Observable < Company | undefined > ; private readonly companyWithCrazyData = rootCompany ( relCompanyAddress ( ) , relCompanyAdmin ( relUserEmployees ( ) ) , rootCompanyStaff ( relUserCompany ( relCompanyAddress ( relAddressCompany ( ) ) ) ) ) ; public readonly users$ : Observable < Array < User > > ; private readonly users = rootEntities ( rootUser ( relUserEmployees ( relUserManager ( ) ) , relUserManager ( ) ) ) ; constructor ( protected readonly store , public readonly entitiesService ) { this . users$ = combineLatest ( [ this . store . select ( this . users , selectCurrentUsersIds ) , this . store . pipe ( select ( selectCurrentUsersIds ) , switchMap ( ids => this . store . select ( this . users , ids ) ) ) , ] ) . pipe ( filter ( ( [ a , b ] ) => a === b ) , map ( ( [ a ] ) => a ) ) ; this . company$ = combineLatest ( [ this . store . select ( this . companyWithCrazyData , selectCurrentCompanyId ) , this . store . pipe ( select ( selectCurrentCompanyId ) , switchMap ( id => this . store . select ( this . companyWithCrazyData , id ) ) ) , ] ) . pipe ( filter ( ( [ a , b ] ) => a === b ) , map ( ( [ a ] ) => a ) ) ; } public ngOnDestroy ( ) { this . users . release ( ) ; this . companyWithCrazyData . release ( ) ; } }