Re: [whatwg/dom] Improving ergonomics of events with Observable (#544)

Observables would add ergonomics to EventTarget in two ways: having a standard interface that allows reusable abstractions, and adding pull semantics at the stream level. The issue there is that observables are just a stage-1 proposal, so they're not standard and won't be for years at best, and also that stream-level pull semantics are limited compared to value level.

Async iterables are now a standard stream primitive in the language, and they offer value-level pull semantics using promise generation, which integrates nicely with async functions and other language constructs and is familiar to users.

Converting the EventTarget push streams to iterables requires buffering, but there is a standard solution for that in Streams API, or even a simplified last-value cache could be used (which removes the risk of leaking with the tradeoff of having to be consumed serially).

The workaround to use `Event.preventDefault()` is straightforwardly just using the current API to add a separate listener. It's not good to have a special case, but it's also arguably the minority of use cases that need it.

Instead of waiting for observables for years, async iteration can be supported now. It'd enable a pattern like this:
```js
for await (const event of element.addEventListener('click')) {
  console.log(event);
}
```
Calling `@@asyncIterator()` would add the listener, and the standard `return()` and `throw()` hooks can be used to remove the listener.

-- 
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/544#issuecomment-431818408

Received on Monday, 22 October 2018 12:20:14 UTC