I am using a WebSocketSubject, and I often want to block execution until a given event arrives, which is why I use firstValueFrom, like in the following code:
let websocket = new WebSocketSubject<any>(url);
let firstMessage = await firstValueFrom(websocket.pipe(filter(m => true));
I only have one issue, which is that firstValueFrom calls websocket.unsubscribe() when it resolves the promise, but on a WebSocketSubject that has the effect of closing the underlying Web Socket, which I want to keep open!
Currently, I have thought of a few possible ways out:
- Writing an equivalent of
firstValueFromthat does not unsubscribe.
Counter argument: I would prefer not reimplementing a function that is nearly perfect, except for one small issue; - Using another
Subjectthat will subscribe toWebSocketSubject, and I will usefirstValueFromon that subject.
Counter argument: In terms of usage, I see potential confusion to have twoSubjectobjects, and having to know which one to use (E.g. Usewebsocket.nextfor sending messages upstream, and only usewebsocketProxyfor receiving messages, and never get confused between the two!); - Using
multiplexto create temporaryObservableobjects that will then be closed byfirstValueFromwithout issue.
Counter argument: As I am not actually multiplexing in this case, I would rather not use that method, whose signature and usage seems overkill for my use case.
In short, I suspect that I am missing something basic (e.g. an appropriate OperatorFunction) that would allow me to make it so that the unsubscribe call made by firstValueFrom does not result in the underlying web socket being closed.