import { BehaviorSubject , EMPTY , Observable , Subject , of } from "rxjs" ; import { Directive } from "@angular/core" ; import type { OnChanges , OnDestroy , SimpleChanges } from "@angular/core" ; import { distinctUntilChanged , mergeMap , takeUntil } from "rxjs/operators" ; @ Directive ( ) export abstract class NgChangesObservableComponent implements OnChanges , OnDestroy { protected ngChanges = new BehaviorSubject < Partial < { [ k in keyof this ] : this [ k ] ; } > > ( { } ) ; protected ngOnDestroy$ = new Subject ( ) ; ngOnChanges ( changes ) { const props = { } ; Object . assign ( props , this . ngChanges . value ) ; for ( const propertyName of Object . keys ( changes ) ) { props [ propertyName ] = changes [ propertyName ] ?. currentValue ; } this . ngChanges . next ( props as any ) ; } ngOnDestroy ( ) { this . ngChanges . complete ( ) ; this . ngOnDestroy$ . next ( void 0 ) ; this . ngOnDestroy$ . complete ( ) ; } protected ngChangesObservable < K extends keyof this > ( propertyName ) { return this . ngChanges . pipe ( mergeMap ( ( props ) => { return propertyName in props ? of ( props [ propertyName ] as this [ K ] ) : EMPTY ; } ) , distinctUntilChanged ( ) , takeUntil ( this . ngOnDestroy$ ) ) ; } }