import { fromEventPattern , MonoTypeOperatorFunction , Observable , ObservedValueOf , OperatorFunction } from 'rxjs' ; import { NodeEventHandler } from 'rxjs/internal/observable/fromEvent' ; import { filter , first , map , shareReplay , switchMap } from 'rxjs/operators' ; export interface EventHandlers { on ( eventName , addHandler ) : void ; off ( eventName , removeHandler ) : void ; } export function shareReplayFirst < T > ( ) { return input$ => input$ . pipe ( first ( ) , shareReplay ( { bufferSize : 1 , refCount : true } ) ) ; } export function filterTruthy < T > ( ) { return filter ( x => ! ! x ) ; } export function switchMapNodeEvent < T extends EventHandlers > ( action ) { return switchMap < T , Observable < T > > ( ( events ) => fromEventPattern < T > ( ( h ) => events . on ( action , h ) , ( h ) => events . off ( action , h ) ) . pipe ( map ( ( ) => events ) ) ) ; }