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.

feat: removing a single event listener / `Plugin#removeListener`

See original GitHub issue

Feature Request

Description

Allow removing listeners on plugins selectively, instead of just removeAllListeners().

Platform(s)

Android, iOS, web

Preferred Solution

Add a removeListener(eventName: string, listenerFunc: ListenerCallback) method to all plugins that support addListener. The removeListener method already exists as private API on WebPlugin.

It expects an eventName and a stable listenerFunc reference. If the given pair is not currently registered, it no-ops.

Bonus points for adding { once: boolean } as an option to addListener.

Additional Context

https://github.com/ionic-team/capacitor/blob/a37e188bf1ab57465e86f6b2ab7c4c2308a21173/core/src/web/index.ts#L83-L108

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener

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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

7reactions
JMCollinscommented, Nov 22, 2020

Would this existing mechanism meet your needs? (Example In TypeScript)

const myPluginEventListener = Plugins.MyPlugin.addListener(
  'myPluginEvent',
  (info: any) => {
    console.log('myPluginEvent was fired');
  },
);

myPluginEventListener.remove();

See “Plugin Events” in: https://capacitorjs.com/docs/plugins/ios https://capacitorjs.com/docs/plugins/android

1reaction
JMCollinscommented, Nov 22, 2020

I am a first-time Capacitor user (not on the dev team). I am currently working on a plugin and happened to see your post while doing some research. Glad I could be helpful. Cheers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

EventTarget.removeEventListener() - Web APIs | MDN
The removeEventListener() method of the EventTarget interface removes an event listener previously registered with EventTarget.
Read more >
Removing an anonymous event listener - Stack Overflow
There is no way to cleanly remove an event handler unless you stored a reference to the event handler at creation.
Read more >
An in-depth guide to event listeners - Aurelio De Rosa blog
In this article, I'll explain what event listeners are, and how to add and remove them from a web page. I'll show several...
Read more >
Java Widget Fundamentals - 1.3 Events and Listeners - InformIT
It is also possible to remove listeners using removeListener(). ... However, for events that are particular to your widget, you will also ...
Read more >
Removing Event Listeners - YouTube
When you add event listeners to DOM elements you also open up the possibility of creating a memory leak and adversely affecting memory...
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