Skip to content

Commit

Permalink
feat(broadcaster): add Broadcaster plugin (#877)
Browse files Browse the repository at this point in the history
* feat(broadcaster): add Broadcaster plugin

* fix(broadcaster): return Obserable for addEventListener

- also remove the listener when clearing observable
  • Loading branch information
mebibou authored and ihadeed committed Jan 20, 2017
1 parent 1615b74 commit 1e38a6c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -253,6 +255,7 @@ window['IonicNative'] = {
Brightness,
BLE,
BluetoothSerial,
Broadcaster,
Calendar,
CallNumber,
Camera,
Expand Down
51 changes: 51 additions & 0 deletions src/plugins/broadcaster.ts
Original file line number Diff line number Diff line change
@@ -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<any>} Returns an observable to watch when an event is received
*/
@Cordova({
observable: true,
clearFunction: 'removeEventListener',
clearWithArgs: true
})
static addEventListener(eventName: string): Observable<any> { return; }

/**
* This function sends data to the native code
* @param eventName {string}
* @param eventData {any}
* @return {Promise<any>} Returns a promise that resolves when an event is successfully fired
*/
@Cordova()
static fireNativeEvent(eventName: string, eventData: any): Promise<any> { return; }

}

0 comments on commit 1e38a6c

Please sign in to comment.