capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

bug: if a web plugin is used without await, multiple instances get created

Open jcesarmobile opened this issue 9 months ago • 1 comments

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.

jcesarmobile avatar Sep 26 '23 17:09 jcesarmobile