From 1e38a6c005305b145b877ab1606d43f530ed767b Mon Sep 17 00:00:00 2001 From: Guillaume Date: Sat, 21 Jan 2017 05:11:19 +0800 Subject: [PATCH] feat(broadcaster): add Broadcaster plugin (#877) * feat(broadcaster): add Broadcaster plugin * fix(broadcaster): return Obserable for addEventListener - also remove the listener when clearing observable --- src/index.ts | 3 +++ src/plugins/broadcaster.ts | 51 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 src/plugins/broadcaster.ts diff --git a/src/index.ts b/src/index.ts index 5e41d57e2..42144bd26 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,6 +19,7 @@ import { BatteryStatus } from './plugins/batterystatus'; import { Brightness } from './plugins/brightness'; import { BLE } from './plugins/ble'; import { BluetoothSerial } from './plugins/bluetoothserial'; +import { Broadcaster } from './plugins/broadcaster'; import { Calendar } from './plugins/calendar'; import { CallNumber } from './plugins/call-number'; import { Camera } from './plugins/camera'; @@ -135,6 +136,7 @@ export * from './plugins/batterystatus'; export * from './plugins/ble'; export * from './plugins/bluetoothserial'; export * from './plugins/brightness'; +export * from './plugins/broadcaster'; export * from './plugins/calendar'; export * from './plugins/call-number'; export * from './plugins/camera'; @@ -253,6 +255,7 @@ window['IonicNative'] = { Brightness, BLE, BluetoothSerial, + Broadcaster, Calendar, CallNumber, Camera, diff --git a/src/plugins/broadcaster.ts b/src/plugins/broadcaster.ts new file mode 100644 index 000000000..8262637ca --- /dev/null +++ b/src/plugins/broadcaster.ts @@ -0,0 +1,51 @@ +import { Plugin, Cordova } from './plugin'; +import { Observable } from 'rxjs/Observable'; + +/** + * @name Broadcaster + * @description + * This plugin adds exchanging events between native code and your app. + * + * @usage + * ``` + * import { Broadcaster } from 'ionic-native'; + * + * // Listen to events from Native + * Broadcaster.addEventListener('eventName').then((event) => console.log(event)); + * + * // Send event to Native + * Broadcaster.fireNativeEvent('eventName', {}).then(() => console.log('success')); + * + * ``` + */ +@Plugin({ + pluginName: 'Broadcaster', + plugin: 'cordova-plugin-broadcaster', + pluginRef: 'broadcaster', + repo: 'https://github.com/bsorrentino/cordova-broadcaster', + platforms: ['Android', 'iOS'] +}) +export class Broadcaster { + + /** + * This function listen to an event sent from the native code + * @param eventName {string} + * @return {Observable} Returns an observable to watch when an event is received + */ + @Cordova({ + observable: true, + clearFunction: 'removeEventListener', + clearWithArgs: true + }) + static addEventListener(eventName: string): Observable { return; } + + /** + * This function sends data to the native code + * @param eventName {string} + * @param eventData {any} + * @return {Promise} Returns a promise that resolves when an event is successfully fired + */ + @Cordova() + static fireNativeEvent(eventName: string, eventData: any): Promise { return; } + +}