Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to remove all event listeners #2531

Closed
MatthewPringle opened this issue Mar 5, 2020 · 2 comments · Fixed by #2609
Closed

Need to remove all event listeners #2531

MatthewPringle opened this issue Mar 5, 2020 · 2 comments · Fixed by #2609

Comments

@MatthewPringle
Copy link

MatthewPringle commented Mar 5, 2020

Im using live reload and the js layer is setting up a plugin event listener on every reload.

Unfortunately as the native layer is not reloading the listeners are multiplying on every reload and I can see in the logs that they are all firing events, even though only the newest one is hitting the reloaded js code.

As the js code has reloaded I no longer have a reference to the listeners and so can't remove them.

There also doesn't seem to be a way of detecting before the js code reloads.

So is there a way to remove all listeners from the plugin, I'm assuming what im seeing in the logs is the plugin / bridge firing an event for each listener that was setup

@angstyloop
Copy link

angstyloop commented Nov 2, 2022

I'm not an ionic programmer - I just do JS. But maybe I can help out.

Is the target of your event an EventTarget from the JavaScript Web API?

If so, you can extend EventTarget, overriding the addEventListener and removeEventListener methods to maintain a data structure containing handler function references as they are added and removed.

I follow this approach in the Gist below, where I also have a few unit tests. You can run the tests in the browser JS console by copying the code from the Gist to your clipboard, pasting it into the browser JS console, and pressing enter. Please read strange JS from the internet before running it in your JS browser console.

Fork and make a pull request if you think you made improvements.

https://gist.github.com/angstyloop/504414aba95b61b98be0db580cb2a3b0

Hope this helped at all

@ionitron-bot
Copy link

ionitron-bot bot commented Dec 2, 2022

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Dec 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants