observable icon indicating copy to clipboard operation
observable copied to clipboard

"complete" single fired events?

Open benlesh opened this issue 1 year ago • 3 comments

For example, if you have a web socket, and you get a close event, I'm proposing that we just complete it for the user. This of course means identifying different things that are like this and handling them appropriately.

const socket = new WebSocket('wss://whatever');

socket.when('close').subscribe({
  next: () => console.log('closed'),
  complete: () => console.log('done'),
});

AbortSignal's abort is another one I can think of off the top of my head.

Alternatively, users will just have to add a take(1) to them.

But this does illustrate another advantage to Observable over addEventListener. It also causes the subscription to teardown and clean up, which can cascade through flatMap and other composition.

benlesh avatar Oct 21 '24 19:10 benlesh

I can see arguments both ways on this. One potential issue is, how does this react to socket.dispatchEvent(new Event("close"))?

domenic avatar Oct 22 '24 04:10 domenic

I would assume it would still fire once, then complete. For consistency.

Of course, subscribing again would start waiting for a new close event. But that's interesting.

I guess we would have to look at use cases for dispatching close events manually

benlesh avatar Oct 24 '24 20:10 benlesh

+1 and same for error event!

CxRes avatar Sep 09 '25 10:09 CxRes