feat(plugin): add event wrapper

This commit is contained in:
Ibrahim Hadeed
2016-03-13 15:30:21 -04:00
parent 21dba101f1
commit 0d1bd1335f
2 changed files with 61 additions and 4 deletions
+21 -4
View File
@@ -141,6 +141,19 @@ function wrapObservable(pluginObj:any, methodName:string, args:any[], opts:any =
});
}
/**
* Wrap the event with an observable
* @param event
* @returns {Observable}
*/
function wrapEventObservable (event : string) : Observable<any> {
return new Observable(observer => {
let callback = (status : any) => observer.next(status);
window.addEventListener(event, callback, false);
return () => window.removeEventListener(event, callback, false);
});
}
/**
* @private
* @param pluginObj
@@ -151,13 +164,17 @@ function wrapObservable(pluginObj:any, methodName:string, args:any[], opts:any =
export const wrap = function(pluginObj:any, methodName:string, opts:any = {}) {
return (...args) => {
if (opts.sync){
if (opts.sync)
return callCordovaPlugin(pluginObj, methodName, args, opts);
} else if (opts.observable) {
else if (opts.observable)
return wrapObservable(pluginObj, methodName, args, opts);
} else {
else if (opts.eventObservable && opts.event)
return wrapEventObservable(opts.event);
else
return wrapPromise(pluginObj, methodName, args, opts);
}
}
};