refactor(plugin): cleanup

This commit is contained in:
Ibrahim Hadeed 2016-06-11 10:35:41 -04:00
parent be7b9e2192
commit 9e3a866ba3

View File

@ -15,29 +15,16 @@ export const getPlugin = function(pluginRef: string): any {
return get(window, pluginRef); return get(window, pluginRef);
}; };
/**
* @private
* @param pluginRef
* @returns {boolean}
*/
export const isInstalled = function(pluginRef: string): boolean {
return !!getPlugin(pluginRef);
};
/** /**
* @private * @private
* @param pluginObj * @param pluginObj
* @param method * @param method
*/ */
export const pluginWarn = function(pluginObj: any, method: string) { export const pluginWarn = function(pluginObj: any, method: string) {
var pluginName = pluginObj.name; let pluginName = pluginObj.name, plugin = pluginObj.plugin;
var plugin = pluginObj.plugin; if (method) console.warn('Native: tried calling ' + pluginName + '.' + method + ', but the ' + pluginName + ' plugin is not installed.');
if (method) { else console.warn('Native: tried accessing the ' + pluginName + ' plugin but it\'s not installed.');
console.warn('Native: tried calling ' + pluginName + '.' + method + ', but the ' + pluginName + ' plugin is not installed.'); console.warn('Install the ' + pluginName + ' plugin: \'ionic plugin add ' + plugin + '\'');
} else {
console.warn('Native: tried accessing the ' + pluginName + ' plugin but it\'s not installed.');
}
console.warn('Install the ' + pluginName + ' plugin: \'cordova plugin add ' + plugin + '\'');
}; };
/** /**
@ -46,11 +33,8 @@ export const pluginWarn = function(pluginObj: any, method: string) {
* @param method * @param method
*/ */
export const cordovaWarn = function(pluginName: string, method: string) { export const cordovaWarn = function(pluginName: string, method: string) {
if (method) { if (method) console.warn('Native: tried calling ' + pluginName + '.' + method + ', but Cordova is not available. Make sure to include cordova.js or run in a device/simulator');
console.warn('Native: tried calling ' + pluginName + '.' + method + ', but Cordova is not available. Make sure to include cordova.js or run in a device/simulator'); else console.warn('Native: tried accessing the ' + pluginName + ' plugin but Cordova is not available. Make sure to include cordova.js or run in a device/simulator');
} else {
console.warn('Native: tried accessing the ' + pluginName + ' plugin but Cordova is not available. Make sure to include cordova.js or run in a device/simulator');
}
}; };
function setIndex (args: any[], opts: any= {}, resolve?: Function, reject?: Function): any { function setIndex (args: any[], opts: any= {}, resolve?: Function, reject?: Function): any {
// If the plugin method expects myMethod(success, err, options) // If the plugin method expects myMethod(success, err, options)
@ -68,14 +52,12 @@ function setIndex (args: any[], opts: any= {}, resolve?: Function, reject?: Func
args.push(resolve); args.push(resolve);
args.push(reject); args.push(reject);
} }
return args; return args;
} }
function callCordovaPlugin(pluginObj: any, methodName: string, args: any[], opts: any= {}, resolve?: Function, reject?: Function) { function callCordovaPlugin(pluginObj: any, methodName: string, args: any[], opts: any= {}, resolve?: Function, reject?: Function) {
// Try to figure out where the success/error callbacks need to be bound // Try to figure out where the success/error callbacks need to be bound
// to our promise resolve/reject handlers. // to our promise resolve/reject handlers.
args = setIndex (args, opts, resolve, reject); args = setIndex (args, opts, resolve, reject);
let pluginInstance = getPlugin(pluginObj.pluginRef); let pluginInstance = getPlugin(pluginObj.pluginRef);
@ -101,13 +83,11 @@ function callCordovaPlugin(pluginObj: any, methodName: string, args: any[], opts
function getPromise(cb) { function getPromise(cb) {
if (window.Promise) { if (window.Promise) {
// console.log('Native promises available...');
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cb(resolve, reject); cb(resolve, reject);
}); });
} else if (window.angular) { } else if (window.angular) {
let $q = window.angular.injector(['ng']).get('$q'); let $q = window.angular.injector(['ng']).get('$q');
// console.log('Loaded $q', $q);
return $q((resolve, reject) => { return $q((resolve, reject) => {
cb(resolve, reject); cb(resolve, reject);
}); });
@ -138,7 +118,6 @@ function wrapObservable(pluginObj: any, methodName: string, args: any[], opts: a
if (pluginResult && pluginResult.error) { if (pluginResult && pluginResult.error) {
observer.error(pluginResult.error); observer.error(pluginResult.error);
} }
return () => { return () => {
try { try {
if (opts.clearFunction) { if (opts.clearFunction) {
@ -267,8 +246,6 @@ export function Plugin(config) {
*/ */
export function Cordova(opts: any = {}) { export function Cordova(opts: any = {}) {
return (target: Object, methodName: string, descriptor: TypedPropertyDescriptor<any>) => { return (target: Object, methodName: string, descriptor: TypedPropertyDescriptor<any>) => {
let originalMethod = descriptor.value;
return { return {
value: function(...args: any[]) { value: function(...args: any[]) {
return wrap(this, methodName, opts).apply(this, args); return wrap(this, methodName, opts).apply(this, args);