Re: [whatwg/dom] Support `AbortSignal`s in `addEventListener`s options to unsubscribe from events? (#911)

Oh great feature, so something like that below can be rewritten with AbortController

```js
button.addEventListener('click', () => {
  // ...

  function closeByClick() {
    // ... do stuff
    closeButton.removeEventListener('click', closeByClick);
    window.removeEventListener('keydown', closeByEscape);
  }
  function closeByEscape(e) {
    if (e.key !== 'Escape') return;
    // ... do stuff
    closeButton.removeEventListener('click', closeByClick);
    window.removeEventListener('keydown', closeByEscape);
  }

  closeButton.addEventListener('click', closeByClick);
  window.addEventListener('keydown', closeByEscape);
});
```
like this:

```js
button.addEventListener('click', () => {
  // ...
  const controller = new AbortController();

  closeButton.addEventListener('click', () => {
    // ... do stuff
    controller.abort();
  }, { signal: controller.signal });

  window.addEventListener('keydown', e => {
    if (e.key !== 'Escape') return; // ignore other keys than Escape
    // ... do stuff
    controller.abort();
  }, { signal: controller.signal });
});
```

-- 
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/911#issuecomment-829547030

Received on Thursday, 29 April 2021 19:50:41 UTC