Skip to content

Commit

Permalink
feat: implement removeAllListeners (#2609)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Mar 20, 2020
1 parent 59fcf9e commit ac55d63
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 1 deletion.
10 changes: 10 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,16 @@ public void removeListener(PluginCall call) {
}
}

/**
* Exported plugin call to remove all listeners from this plugin
* @param call
*/
@SuppressWarnings("unused")
@PluginMethod(returnType=PluginMethod.RETURN_NONE)
public void removeAllListeners(PluginCall call) {
eventListeners.clear();
}

/**
* Exported plugin call to request all permissions for this plugin
* @param call
Expand Down
39 changes: 39 additions & 0 deletions core/src/core-plugin-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ export interface AccessibilityPlugin {
* Listen for screen reader state change (on/off)
*/
addListener(eventName: 'accessibilityScreenReaderStateChange', listenerFunc: ScreenReaderStateChangeCallback): PluginListenerHandle;

/**
* Remove all native listeners for this plugin
*/
removeAllListeners(): void;
}

export interface AccessibilitySpeakOptions {
Expand Down Expand Up @@ -131,6 +136,11 @@ export interface AppPlugin extends Plugin {
* If you want to close the app, call `App.exitApp()`.
*/
addListener(eventName: 'backButton', listenerFunc: (data: AppUrlOpen) => void): PluginListenerHandle;

/**
* Remove all native listeners for this plugin
*/
removeAllListeners(): void;
}

export interface AppState {
Expand Down Expand Up @@ -235,6 +245,10 @@ export interface BrowserPlugin extends Plugin {

addListener(eventName: 'browserFinished', listenerFunc: (info: any) => void): PluginListenerHandle;
addListener(eventName: 'browserPageLoaded', listenerFunc: (info: any) => void): PluginListenerHandle;
/**
* Remove all native listeners for this plugin
*/
removeAllListeners(): void;
}

export interface BrowserOpenOptions {
Expand Down Expand Up @@ -950,6 +964,11 @@ export interface KeyboardPlugin extends Plugin {
addListener(eventName: 'keyboardDidShow', listenerFunc: (info: KeyboardInfo) => void): PluginListenerHandle;
addListener(eventName: 'keyboardWillHide', listenerFunc: () => void): PluginListenerHandle;
addListener(eventName: 'keyboardDidHide', listenerFunc: () => void): PluginListenerHandle;

/**
* Remove all native listeners for this plugin
*/
removeAllListeners(): void;
}

export interface KeyboardInfo {
Expand Down Expand Up @@ -1101,6 +1120,11 @@ export interface LocalNotificationsPlugin extends Plugin {
requestPermission(): Promise<NotificationPermissionResponse>;
addListener(eventName: 'localNotificationReceived', listenerFunc: (notification: LocalNotification) => void): PluginListenerHandle;
addListener(eventName: 'localNotificationActionPerformed', listenerFunc: (notificationAction: LocalNotificationActionPerformed) => void): PluginListenerHandle;

/**
* Remove all native listeners for this plugin
*/
removeAllListeners(): void;
}


Expand Down Expand Up @@ -1194,6 +1218,11 @@ export interface MotionPlugin extends Plugin {
* Listen for device orientation change (compass heading, etc.)
*/
addListener(eventName: 'orientation', listenerFunc: (event: MotionOrientationEventResult) => void): PluginListenerHandle;

/**
* Remove all native listeners for this plugin
*/
removeAllListeners(): void;
}

export type MotionWatchOrientationCallback = (accel: MotionOrientationEventResult) => void;
Expand Down Expand Up @@ -1237,6 +1266,11 @@ export interface NetworkPlugin extends Plugin {
* Listen for network status change events
*/
addListener(eventName: 'networkStatusChange', listenerFunc: (status: NetworkStatus) => void): PluginListenerHandle;

/**
* Remove all native listeners for this plugin
*/
removeAllListeners(): void;
}

export interface NetworkStatus {
Expand Down Expand Up @@ -1514,6 +1548,11 @@ export interface PushNotificationsPlugin extends Plugin {
addListener(eventName: 'registrationError', listenerFunc: (error: any) => void): PluginListenerHandle;
addListener(eventName: 'pushNotificationReceived', listenerFunc: (notification: PushNotification) => void): PluginListenerHandle;
addListener(eventName: 'pushNotificationActionPerformed', listenerFunc: (notification: PushNotificationActionPerformed) => void): PluginListenerHandle;

/**
* Remove all native listeners for this plugin
*/
removeAllListeners(): void;
}

//
Expand Down
2 changes: 1 addition & 1 deletion core/src/web/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WebPlugin } from './index';

import { AppPlugin, AppLaunchUrl, } from '../core-plugin-definitions';
import { AppPlugin, AppLaunchUrl } from '../core-plugin-definitions';

export class AppPluginWeb extends WebPlugin implements AppPlugin {
constructor() {
Expand Down
8 changes: 8 additions & 0 deletions core/src/web/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ export class WebPlugin {
}
}

removeAllListeners(): void {
this.listeners = {};
for (const listener in this.windowListeners) {
this.removeWindowListener(this.windowListeners[listener]);
}
this.windowListeners = {};
}

notifyListeners(eventName: string, data: any): void {
let listeners = this.listeners[eventName];
if (listeners) {
Expand Down
1 change: 1 addition & 0 deletions ios/Capacitor/Capacitor/CAPPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- (BOOL)hasListeners:(NSString *)eventName;
- (void)addListener:(CAPPluginCall *)call;
- (void)removeListener:(CAPPluginCall *)call;
- (void)removeAllListeners:(CAPPluginCall *)call;

// Called after init if the plugin wants to do
// some loading so the plugin author doesn't
Expand Down
4 changes: 4 additions & 0 deletions ios/Capacitor/Capacitor/CAPPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ - (void)removeListener:(CAPPluginCall *)call {
[self.bridge releaseCallWithCallbackId:callbackId];
}

- (void)removeAllListeners:(CAPPluginCall *)call {
[self.eventListeners removeAllObjects];
}

- (NSArray<CAPPluginCall *>*)getListeners:(NSString *)eventName {
NSArray<CAPPluginCall *>* listeners = [self.eventListeners objectForKey:eventName];
return listeners;
Expand Down
7 changes: 7 additions & 0 deletions ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
CAP_PLUGIN(CAPAccessibilityPlugin, "Accessibility",
CAP_PLUGIN_METHOD(isScreenReaderEnabled, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(speak, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnNone);
)

CAP_PLUGIN(CAPAppPlugin, "App",
CAP_PLUGIN_METHOD(exitApp, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(getLaunchUrl, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(canOpenUrl, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(openUrl, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnNone);
)

CAP_PLUGIN(CAPBackgroundTaskPlugin, "BackgroundTask",
Expand All @@ -23,6 +25,7 @@
CAP_PLUGIN_METHOD(open, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(close, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(prefetch, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnNone);
)

CAP_PLUGIN(CAPCameraPlugin, "Camera",
Expand Down Expand Up @@ -80,6 +83,7 @@
CAP_PLUGIN_METHOD(setStyle, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(setResizeMode, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(setScroll, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnNone);
)

CAP_PLUGIN(CAPLocalNotificationsPlugin, "LocalNotifications",
Expand All @@ -89,6 +93,7 @@
CAP_PLUGIN_METHOD(getPending, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(registerActionTypes, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(areEnabled, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnNone);
)

CAP_PLUGIN(CAPModalsPlugin, "Modals",
Expand All @@ -100,6 +105,7 @@

CAP_PLUGIN(CAPNetworkPlugin, "Network",
CAP_PLUGIN_METHOD(getStatus, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnNone);
)

CAP_PLUGIN(CAPPermissionsPlugin, "Permissions",
Expand All @@ -115,6 +121,7 @@
CAP_PLUGIN_METHOD(createChannel, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(deleteChannel, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(listChannels, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(removeAllListeners, CAPPluginReturnNone);
)

CAP_PLUGIN(CAPPhotosPlugin, "Photos",
Expand Down

0 comments on commit ac55d63

Please sign in to comment.