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

Remove all listeners for a plugin #185

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hampoelz
Copy link

@hampoelz hampoelz commented Apr 21, 2022

Match removeAllListeners() function with Capacitor API to remove all listeners of a plugin.

Refs #137

@matallui
Copy link

Is there any documentation on how to use events on a Capacitor Electron plugin?
I'm still having issues understanding how to use/implement this, and can't find any implementation examples.

@hampoelz
Copy link
Author

@matallui You can find a small example in the createplugin-documentation: https://github.com/capacitor-community/electron/blob/main/docs-site/docs/creatingplugins.md

@matallui
Copy link

@hampoelz thanks! I have something similar but it's not working.
I'm just confused what to put in each file of the plugin codebase.

src/definitions.ts:

export interface SomePlugin {
    echo(s: string): Promise<string>;
}

electron/src/index.ts

export class Some extends EventEmitter implements SomePlugin {
    echo(s: string): Promise<string> {
        return Promise.resolve(s);
    }
}

It sounds like the above example would let you do something like this, from the client code:

import { Some } from 'some-plugin';

const hello = await Some.echo('hello');

const id = Some.addListener('some-event', () => console.log('fired'));
Some.removeListener(id);

But the last two lines won't work because those methods are not implemented on the plugin.

I also don't understand what this means from the example:

CapacitorCustomPlatform.plugins.MyPlugin.addListener('my-event', console.log);

Where do I access CapacitorCustomPlatform from?

Any help is much appreciated!

@hampoelz
Copy link
Author

hampoelz commented May 11, 2022

@matallui your code looks good to me.

If you're using Capacitor with the bundled Capacitor-Core you can access your plugin with Capacitor.Plugins.yourPlugin or on Electron with CapacitorCustomPlatform.plugins.yourPlugin. (Capacitor and CapacitorCustomPlatform are globally scoped)

(I'm using const myPlugin = Capacitor.Plugins.myPlugin || CapacitorCustomPlatform.plugins.myPlugin; to access my plugin cross-platform)

@matallui
Copy link

matallui commented May 11, 2022

@hampoelz thanks! I actually got it working by simply type casting the plugin to any:

import { Some } from 'some-plugin';

let listener: any;

listener = (Some as any).addListener('some-event', console.log).then(res => (listener = res));

// some code

if (listener) {
  if (listener.remove) {
    // iOS & Android
    listener.remove();
  } else {
    // electron case
    (Some as any).removeListener(listener);
  }
}

This is what is working for me right now.

I guess those methods are added at runtime, so Typescript was not letting me access methods that were not defined by the plugin. At least that's my take on this.

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.

None yet

2 participants