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

Plugin Import Proposal #3306

Closed
imhoffd opened this issue Jul 21, 2020 · 5 comments · Fixed by #3305
Closed

Plugin Import Proposal #3306

imhoffd opened this issue Jul 21, 2020 · 5 comments · Fixed by #3305
Milestone

Comments

@imhoffd
Copy link
Contributor

imhoffd commented Jul 21, 2020

Please feel free to comment on this proposal. Thanks!


@capacitor/core should provide a registerPlugin function that takes an object of implementations for each platform. For Android and iOS, the plugins proxy can be used. For web, the web implementation will need to be provided.

registerPlugin will return a RegisteredPlugin object with a getImplementation method that returns the instance of the plugin for the current platform.

The "echo" example will be used for this proposal.

echo-plugin/index.ts

import { Plugins, registerPlugin } from '@capacitor/core';
import { EchoPluginWeb } from './web';

export interface EchoPlugin {
  echo: (something: string) => string;
}

const Plugin = registerPlugin<EchoPlugin>('Echo', {
  android: Plugins.EchoPlugin,
  ios: Plugins.EchoPlugin,
  web: new EchoPluginWeb(),
}).getImplementation();

export { Plugin };

echo-plugin/web.ts

import { WebPlugin } from '@capacitor/core';
import type { EchoPlugin } from '.';

export class EchoPluginWeb extends WebPlugin implements EchoPlugin {
  constructor() {
    super({ name: 'Echo' });
  }

  ...
}

my-app/some-page.tsx

import { Echo } from 'echo-plugin';

await Echo.echo('hello world');

// TODO: Figure out testing.
export class Foo {
  constructor() {
    Echo.echo();
  }
}

This shouldn't be a breaking change because plugins will need to opt-in to using registerPlugin in order for the new plugin import to work. (Previously, users of the plugin would have to import { Plugins } from '@capacitor/core';).

This was referenced Jul 21, 2020
@IT-MikeS
Copy link
Contributor

Shouldn't

export class EchoPluginWeb extends WebPlugin implements EchoPlugin {
  constructor() {
    super({ name: 'Echo' });
  }
}

have an echo method? 🤔

@imhoffd
Copy link
Contributor Author

imhoffd commented Jul 21, 2020

Yeah, but not super important to the proposal, I'll add ... to make it clear there's more implementation details.

@IT-MikeS
Copy link
Contributor

Just didn't want people trying it with your code and going "why isn't this working?!?!" I know most people will see the issue but I've been there before where its a small little thing I screw up that takes me far too long to figure out... 😅

@imhoffd
Copy link
Contributor Author

imhoffd commented Jul 22, 2020

If you want, you can try the PR: #3305

It's meant to be a working example. 😄

@imhoffd imhoffd added this to the 3.0.0 milestone Jul 23, 2020
@ionitron-bot
Copy link

ionitron-bot bot commented Nov 11, 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 Nov 11, 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