I would like to allow a client to dynamically change the url to a webSocket
and maintain Observables on connection to the new webSocket
. Looking over the RxJS Github and Stackoverflow, this is not something that is supported by RxJS (see https://github.com/ReactiveX/rxjs/issues/4355)
The referenced Github links to a Stackoverflow (Resetting ReplaySubject in RxJS 6) where a resettable
is used to "reset" a subject.
Could this be used to dynamically modify the url of a webSocket
?
https://github.com/cartant/rxjs-etc/blob/master/source/observable/resettable.ts
Here's what I've tried so far:
function resettable<T, U extends any[]>(
factory: (...args: U) => WebSocketSubject<T>,
...args: U
): {
readonly closed: boolean;
readonly observable: Observable<T>;
readonly subject: Subject<T>;
reset(...args: U): void;
unsubscribe(): void;
} {
const resetter = new Subject<any>();
const source = new Subject<T>();
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<T> {
return resetter.asObservable().pipe(
startWith(null),
switchMap(() => destination)
);
},
get subject(): Subject<T> {
return source;
}
};
}
If there are any other ways to accomplish this, I'm all ears! Thanks!