[whatwg/dom] Proposal: asynchronous event listeners (Issue #1308)

### What is the issue with the DOM Standard?

I've heard chatterings in a few different places about interest in asynchronous event listeners on the platform, so I figured I'd file a bug here for some more centralized discussion. **The problem**: When some action happens on the web platform that results in firing events, all event listeners for that event are immediately, right then & there on the spot, without regard to the priority of the listener relative to surrounding tasks. It's entirely possible that developers wish to know about/respond to some events at a much lower priority than other competing tasks at around the same time. Currently there is no way to signal to the platform, that an event listener should be invoked _asynchronously after_ the platform would ordinarily do so, saving the event listener's invocation for a less-busy/contentious time, in terms of task scheduling and execution.

Enabling this would let developers extend their existing task scheduling / prioritization logic to their event listeners as well. Something very rough along these lines can already be done today:

```js
button.addEventListener('click', e => {
  if (mustDefer) {
    setTimeout(realClickHandler, kTimeout);
    // or…
    requestAnimationFrame(realClickHandler);
  }
});
```

…but it's pretty limited. First, it still involves immediately invoking user script in response to the event, so we don't actually avoid a big part of that cost. The fact that the queueing / deferral logic is handled immediately in userland is a missed performance opportunity — perhaps a large one? Second, it's not all that ergonomic, and is perhaps harder to schedule the `readClickHandler` relative to _other userland tasks_ that follow a certain scheduling dynamic.

I wonder if there is an opportunity to integrate the [Prioritized Task Scheduling API](https://wicg.github.io/scheduling-apis/) here. One option would be doing something as simple as passing in a [task priority](https://developer.mozilla.org/en-US/docs/Web/API/Prioritized_Task_Scheduling_API#task_priorities) to `addEventListener()`:

```js
button.addEventListener('click', e => {
    // Do real stuff asynchronously…
    // e.preventDefault() does not work here❗
}, {priority: "user-visible"});
```

@chrishtr @mmocny @shaseley @mfreed7

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

Message ID: <whatwg/dom/issues/1308@github.com>

Received on Tuesday, 27 August 2024 01:41:42 UTC