question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use abort controllers to remove events

See original GitHub issue

Either internally or just for the public API, it could support abort controllers instead of returning .destroy()

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#add_an_abortable_listener

Examples for the public API:

// User-defined controller
const controller = new AbortController();
delegate(document, 'a', 'click', handler, {signal: controller.signal});
controller.abort();
// Always create a controller instead of `.destroy()`
const controller = delegate(document, 'a', 'click', handler);
controller.abort();

Unfortunately, Safari does not support the signal natively on addEventListener, so internally it should keep using removeEventListener

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
fregantecommented, Mar 18, 2022

but at the AddEventListener the option.signal is not implemented 😦

It’s Safari 15+, it’s good enough for me, this will be a breaking release anyway 👍

0reactions
cheap-glitchcommented, Mar 19, 2022

I’ve implemented this, but I can’t update the tests because JSDOM doesn’t support AbortSignal in addEventListener yet: https://github.com/jsdom/jsdom/issues/3327. I guess I’ll take a stab at it

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using AbortController as an Alternative for Removing ...
Here's a super simple example using AbortController to cancel a fetch() request: const controller = new AbortController(); ...
Read more >
When to use AbortController to remove event listeners?
OLD ANSWER: The Abort Controller object is for aborting fetch calls, not for removing event listeners. If you need to remove multiple listener's ......
Read more >
AbortSignal: abort event - Web APIs - MDN Web Docs - Mozilla
The abort event of the AbortSignal is fired when the associated request is aborted, i.e. using AbortController.abort().
Read more >
TIL — Removing DOM Event Handlers using AbortController
Today I learned about the ability to remove DOM event handlers using AbortController. There is plenty of other good use cases for this ......
Read more >
Cancel JavaScript Event Listeners with AbortController
By calling controller.abort(); , you can remove the listener from the event target, just like when calling element.removeEventListener .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found