[whatwg/dom] Allow other specs to add event listeners ergonomically (#878)

Background: https://github.com/whatwg/streams/pull/1053#discussion_r452433904, as well as the (on-pause) https://wicg.github.io/kv-storage/#add-a-simple-event-listener.

There are times when it is convenient for specs to listen to events. For Streams, we want to use the MessagePort infrastructure, and its message/messageerror events. @ricea states that there are tons of other places in Blink that similarly listen to events from C++.

Another instance of this is #493, where we decided to add a custom mechanism specific for `AbortSignal`. The downside noted there is that the ordering is not interleaved in an intuitive fashion with web-developer-added events. I'm not proposing we revisit that at the moment, although it might be a good idea after this issue settles.

From specs, adding such a listener is not currently easy, mainly because creating JS functions / Web IDL callbacks from algorithm steps is not currently easy. I think we should add something to make it easier.

Some options:

1. Make it easy to create Web IDL callbacks from specs (so, this would probably become a Web IDL issue). Then Streams would write something like "[Add an event listener](https://dom.spec.whatwg.org/#add-an-event-listener) with _target_ and an event listener whose callback is an `EventListener` [that performs the following steps]:".

    - This could clean up https://streams.spec.whatwg.org/#blqs-internal-slots

    - The main downside of this is that the general case will need you to supply a length and name for the function, which is undesirable overhead for the event listener case. We could have Web IDL make these optional, with the understanding that the caller has to be very careful to pass them when they're observable... but I don't like it much.

2. Make the DOM spec's event listener struct contain either an `EventListener` or some steps, and branch at the appropriate point.

3. Add a wrapper algorithm (or modify "add an event listener") which accepts steps instead of `EventListener` instances, and converts at the boundary, so that an "event listener" struct always contains an `EventListener`.

I'm open to any of these, although I disprefer (1). I'd be happy to send a pull request for any of them if I can get some editor direction.

-- 
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/878

Received on Monday, 13 July 2020 19:08:57 UTC