/** * @license Use of this source code is governed by an MIT-style license that * can be found in the LICENSE file at https://github.com/cartant/rxjs-etc */ import { Observable, Subject } from "rxjs"; import { startWith, switchMap } from "rxjs/operators"; // https://stackoverflow.com/a/51147023/6680611 export function resettable( factory: (...args: U) => Subject, ...args: U ): { readonly closed: boolean; readonly observable: Observable; readonly subject: Subject; reset(...args: U): void; unsubscribe(): void; } { const resetter = new Subject(); const source = new Subject(); let destination = factory(...args); let subscription = source.subscribe(destination); return { reset(...args: U): void { subscription.unsubscribe(); destination = factory(...args); subscription = source.subscribe(destination); resetter.next(); }, unsubscribe(): void { subscription.unsubscribe(); /*tslint:disable:rxjs-no-subject-unsubscribe*/ source.unsubscribe(); /*tslint:enable:rxjs-no-subject-unsubscribe*/ }, get closed(): boolean { return subscription.closed; }, get observable(): Observable { return resetter.asObservable().pipe( startWith(null), switchMap(() => destination) ); }, get subject(): Subject { return source; }, }; }