license import { ReplaySubject , Subject , Observable , SchedulerLike } from 'rxjs' ; import { startWith , switchMap } from 'rxjs/operators' ; export default class ResettableSubject < T > extends Subject < T > { reset : ( ) => void ; constructor ( bufferSize , windowTime ? , scheduler ? ) { super ( ) ; const values = resettableFactory ( ( ) => new ReplaySubject < T > ( bufferSize , windowTime , scheduler ) ) ; ( Object . keys ( values . subject ) as ( keyof typeof values . subject ) [ ] ) . forEach ( ( key ) => { this [ key ] = values . subject [ key ] ; } ) ; ( Object . keys ( values . observable ) as ( keyof typeof values . observable ) [ ] ) . forEach ( ( key ) => { this [ key ] = values . observable [ key ] ; } ) ; this . reset = values . reset ; } } function resettableFactory < T > ( factory ) { const resetter = new Subject < any > ( ) ; const source = new Subject < T > ( ) ; let destination = factory ( ) ; let subscription = source . subscribe ( destination ) ; return { observable : resetter . asObservable ( ) . pipe ( startWith ( null ) , switchMap ( ( ) => destination ) ) , reset : ( ) => { subscription . unsubscribe ( ) ; destination = factory ( ) ; subscription = source . subscribe ( destination ) ; resetter . next ( ) ; } , subject : source , } ; }