ionic-team / ionic-team/capacitor

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

Open
#6,938 1 comment 5 reactions 0 assignees View on GitHub
platform: web type: bug
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.