import { Observable , ReplaySubject , Subject } from "rxjs" ; import { takeUntil } from "rxjs/operators" ; const ON_DESTROY_SUBJECT_KEY = Symbol ( "ON_DESTROY_SUBJECT_KEY" ) ; export type ComponentWithOnDestroyObservable = { observeOnDestroy ( ) : Observable < void > ; } ; export class OnDestroyMixin { constructor ( ) { ( this as any ) [ ON_DESTROY_SUBJECT_KEY ] = new ReplaySubject ( ) ; } observeOnDestroy ( ) { return ( this as any ) [ ON_DESTROY_SUBJECT_KEY ] ; } ngOnDestroy ( ) { ( this . observeOnDestroy ( ) as Subject < void > ) . next ( ) ; } } export function componentDestroyed ( target ) { const onDestroySubject = ( target as any ) [ ON_DESTROY_SUBJECT_KEY ] ; if ( onDestroySubject === undefined ) { const proto = Object . getPrototypeOf ( target ) ; const compInfo = proto !== undefined && proto . constructor !== undefined !== proto . constructor . name !== undefined ? ` (component: ${ proto . constructor . name })` : "" ; throw new Error ( `You are almost there! Please extends the base class 'OnDestroyMixin'${ compInfo }.` ) ; } return onDestroySubject ; } export function untilComponentDestroyed < T > ( component ) { return ( source ) => source . pipe ( takeUntil ( componentDestroyed ( component ) ) ) ; }