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.

Add a convenient way to remove a listener inside itself.

See original GitHub issue

It would be nice to have a method to remove a listener inside itself. This might be possible by overloading all registerXyzListener() with a method that accepts a BiConsumer that consumes both the event and the listener manager for this listener.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Bastiancommented, Aug 16, 2020

Oops, you are right ofc. But the “idea” keeps the same:

var listener ​=​ ​new​ ​AtomicReference<​MessageCreateListener​>​();
listener.set(event ​-​>​ {
  api​.​removeListener(listener​.​get());
});

api​.​addListener(listener​.​get());
0reactions
Vampirecommented, Nov 17, 2021

Ah, right. Actually also the array variant should work without race condition and be marginally cheaper due to no need for volatile field and compare and swap algorithm as it is written before the other thread will read it and not modified.

var listener = new ​MessageCreateListener​[1];
listener[0] = e -> discordApi.removeListener(listener[0]);
discordApi.addMessageCreateListener(listener[0]);
Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript: remove an event listener from within that listener?
You can pass the once option to have a listener act only once, then remove itself.
Read more >
JavaScript | removeEventListener() method with examples
The removeEventListener() is an inbuilt function in JavaScript which removes an event handler from an element for a attached event.for ...
Read more >
EventTarget.removeEventListener() - Web APIs | MDN
The removeEventListener() method of the EventTarget interface removes an event listener previously registered with EventTarget.
Read more >
Four ways of listening to DOM events in Angular (Part 3
Unlike Event Binding or @HostListener methods, with Renderer2.listen , you can manage complex event listeners by adding and removing them on specific conditions ......
Read more >
Events | Maps JavaScript API - Google Developers
Removing Event Listeners ... To remove a specific event listener, it must have been assigned to a variable. You can then call removeListener()...
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