Re: [whatwg/dom] once() and on() async utilities for EventTarget (Issue #1038)

@Jamesernator:
> should web APIs that return async iterators be using ReadableStream instead?

Requiring `ReadableStream` in this case would introduce significant additional and unnecessary overhead. I'd very much prefer to avoid that.

@bathos:
> the most interesting thing I've learned from it has been that "race patterns" are pretty common with DOM events: "any of these events once."

Yes, we get those once in a while in Node.js also. The most common are `'close'` and `'error'` events, which tend to be emitted within the same `process.nextTick()` queue batch. If you're waiting on either `'close'` or `'error'`, then you'll typically miss the other one. What we end up having to do is ensure that a synchronous event listener is set on one before we await the other. I like your suggestion here of "any of these events once", very similar to `Promise.all()` or `Promise.race()` in nature.

```js
await eventTarget.next("foo");  // Wait asynchronously for only 'foo', other events emitted synchronously while waiting may be be missed.

await eventTarget.nextRace(["foo", "bar"]);  // Wait asynchronous for either 'foo' or 'race', other events emitted synchronously while waiting may be missed, including either 'foo' or 'bar' events.
```

The possibility of missing events is definitely very possible with this API and needs to be one of the main considerations when adding it.

@Jamesernator :
> ```
> const x = await element.once("click", async (event) => {
>    // All code prior to an await is run synchronously, so event.preventDefault is fine here
>    if (someCondition) event.preventDefault();
>    return event.x;
> });
> ```

Hmm.. this is significantly less attractive as an option for me simply because it doesn't seem to be much better than just continuing to use `addEventListener()`.

Great discussion tho!


-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/1038#issuecomment-984992329

Received on Thursday, 2 December 2021 20:53:28 UTC