Skip to content

fix(core): create a lazy plugin implementation only once - #8702

Open
b84330808 wants to merge 1 commit into
ionic-team:mainfrom
b84330808:fix-lazy-plugin-single-instance
Open

b84330808 wants to merge 1 commit into
ionic-team:mainfrom
b84330808:fix-lazy-plugin-single-instance

Conversation

@b84330808

Copy link
Copy Markdown

Description

registerPlugin() loads a lazy implementation such as web: () => import('./web').then((m) => new m.MyWeb()) on the first method call. Calls made before that load finishes each ran the loader again, so the plugin got several instances and only the last one was kept. Listeners added on the earlier instances never fire.

This shares the pending load between those calls. If the load fails, the next call retries, as before.

Change Type

  • Fix

Rationale / Problems Fixed

Fixes #6938 (from ionic-team/capacitor-plugins#1699: calling addListener() twice without await on web loses the first listener).

Tests or Reproductions

Added two tests to core/src/tests/plugin.spec.ts: two addListener() calls without await on a lazy web plugin now create one instance and both listeners fire (the first test fails on main with 2 instances), and a lazy implementation that fails to load is retried on the next call. npm test in core passes.

Platforms Affected

  • Web

When a plugin registers a lazy implementation such as
`web: () => import('./web').then((m) => new m.MyWeb())`, every call made
before the first load settled ran the loader again. Calling addListener()
twice without awaiting created two instances, and only the last one was
kept, so listeners added to the others never fired.

Share the pending load between those calls. A load that fails is still
retried on the next call.

Fixes ionic-team#6938

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant