ionic-team / ionic-team/capacitor
bug: if a web plugin is used without await, multiple instances get created
- Dominant language
- TypeScript
- Stars
- 16.7k
- Forks
- 1.3k
- Avg merge
- 3d 15h
- Merged PRs (30d)
- 10
Description
## Bug Report
### Capacitor Version
5.x
### Platform(s)
web
### Current Behavior
If a web plugin is used without await, multiple instances get created, which causes problems for `addListener` calls.
See https://github.com/ionic-team/capacitor-plugins/issues/1699 for more details.
### Expected Behavior
A single instance of the plugin should be created
### Code Reproduction
https://github.com/miqmago/capacitor-plugins-issue-1699
### Other Technical Details
If not using await on the plugin calls, the plugin's `registerPlugin` function gets executed multiple times, and the way we have created the plugins a new plugin gets created every time.
Note that the web's `registerPlugin` doesn't get executed multiple times, the plugin is only registered once on web as expected and it already has a "singleton" like mechanism that would return an existing plugin if it was already created, but the plugin's `registerPlugin` multiple executions don't result in multiples web runtime `registerPlugin` calls.
### Additional Context
While this could be fixed plugin by plugin (like the proposed "singleton" in the linked issue or creating the web plugin outside of the `registerPlugin` call so it always returns the same, I think we should try to fix it in `@capacitor/core` so it get fixed for all plugins without additional changes on them.
Contributor guide
Assessment
This issue has not been assessed yet.