Skip to content

Commit

Permalink
feat(otherPromise): can work better with plugins that return promises (
Browse files Browse the repository at this point in the history
  • Loading branch information
ihadeed authored Jul 18, 2016
1 parent f59570e commit 0aee6c8
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/plugins/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ function wrapPromise(pluginObj: any, methodName: string, args: any[], opts: any
return p;
}

function wrapOtherPromise(pluginObj: any, methodName: string, args: any[], opts: any= {}) {
return getPromise((resolve, reject) => {
let pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts);
if (pluginResult && pluginResult.error) {
reject(pluginResult.error);
}
pluginResult.then(resolve).catch(reject);
});
}

function wrapObservable(pluginObj: any, methodName: string, args: any[], opts: any = {}) {
return new Observable(observer => {
let pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, observer.next.bind(observer), observer.error.bind(observer));
Expand Down Expand Up @@ -199,6 +209,9 @@ export const wrap = function(pluginObj: any, methodName: string, opts: any = {})
else if (opts.eventObservable && opts.event)
return wrapEventObservable(opts.event);

else if (opts.otherPromise)
return wrapOtherPromise(pluginObj, methodName, args, opts);

else
return wrapPromise(pluginObj, methodName, args, opts);
};
Expand Down

0 comments on commit 0aee6c8

Please sign in to comment.