import { MonoTypeOperatorFunction , Observable } from 'rxjs' ; import { takeUntil } from 'rxjs/operators' ; export const destroy$ = Symbol ( 'destroy$' ) ; /**
 * An operator that takes until destroy it takes a components this a parameter
 * returns a pipeable RxJS operator.
 */ export const untilDestroy = < T > ( component ) : MonoTypeOperatorFunction < T > => { if ( component [ destroy$ ] === undefined ) { addDestroyObservableToComponent ( component ) ; } return takeUntil < T > ( component [ destroy$ ] ) ; } ; internal export function addDestroyObservableToComponent ( component ) { component [ destroy$ ] = new Observable < void > ( ( observer ) => { const orignalDestroy = component . ngOnDestroy ; if ( orignalDestroy == null ) { throw new Error ( 'untilDestroy operator needs the component to have an ngOnDestroy method' ) ; } component . ngOnDestroy = ( ) => { observer . next ( ) ; observer . complete ( ) ; orignalDestroy . call ( component ) ; } ; return ( _ ) => ( component [ destroy$ ] = undefined ) ; } ) ; }