chore(tslint): add tslint & clean up code

This commit is contained in:
Ibrahim Hadeed
2016-04-29 23:56:49 -04:00
parent 8b6d9f33f6
commit 06146b322f
58 changed files with 1014 additions and 941 deletions
+61 -61
View File
@@ -34,7 +34,7 @@ export const isInstalled = function(pluginRef: string): boolean {
export const pluginWarn = function(pluginObj: any, method: string) {
var pluginName = pluginObj.name;
var plugin = pluginObj.plugin;
if(method) {
if (method) {
console.warn('Native: tried calling ' + pluginName + '.' + method + ', but the ' + pluginName + ' plugin is not installed.');
} else {
console.warn('Native: tried accessing the ' + pluginName + ' plugin but it\'s not installed.');
@@ -48,47 +48,47 @@ export const pluginWarn = function(pluginObj: any, method: string) {
* @param method
*/
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');
} 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 {
// If the plugin method expects myMethod(success, err, options)
if (opts.callbackOrder == 'reverse') {
// Get those arguments in the order [resolve, reject, ...restOfArgs]
args.unshift(reject);
args.unshift(resolve);
} else if(typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') {
// If we've specified a success/error index
args.splice(opts.successIndex, 0, resolve);
args.splice(opts.errorIndex, 0, reject);
} else {
// Otherwise, let's tack them on to the end of the argument list
// which is 90% of cases
args.push(resolve);
args.push(reject);
}
function setIndex (args: any[], opts: any= {}, resolve?: Function, reject?: Function): any {
// If the plugin method expects myMethod(success, err, options)
if (opts.callbackOrder === 'reverse') {
// Get those arguments in the order [resolve, reject, ...restOfArgs]
args.unshift(reject);
args.unshift(resolve);
} else if (typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') {
// If we've specified a success/error index
args.splice(opts.successIndex, 0, resolve);
args.splice(opts.errorIndex, 0, reject);
} else {
// Otherwise, let's tack them on to the end of the argument list
// which is 90% of cases
args.push(resolve);
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
// to our promise resolve/reject handlers.
args = setIndex (args, opts, resolve, reject);
args = setIndex (args, opts, resolve, reject);
let pluginInstance = getPlugin(pluginObj.pluginRef);
if(!pluginInstance) {
if (!pluginInstance) {
// Do this check in here in the case that the Web API for this plugin is available (for example, Geolocation).
if(!window.cordova) {
if (!window.cordova) {
cordovaWarn(pluginObj.name, methodName);
return {
error: 'cordova_not_available'
}
};
}
pluginWarn(pluginObj, methodName);
@@ -102,12 +102,12 @@ function callCordovaPlugin(pluginObj:any, methodName:string, args:any[], opts:an
}
function getPromise(cb) {
if(window.Promise) {
if (window.Promise) {
// console.log('Native promises available...');
return new Promise((resolve, reject) => {
cb(resolve, reject);
})
} else if(window.angular) {
});
} else if (window.angular) {
let $q = window.angular.injector(['ng']).get('$q');
// console.log('Loaded $q', $q);
return $q((resolve, reject) => {
@@ -118,7 +118,7 @@ function getPromise(cb) {
}
}
function wrapPromise(pluginObj:any, methodName:string, args:any[], opts:any={}) {
function wrapPromise(pluginObj: any, methodName: string, args: any[], opts: any= {}) {
let pluginResult, rej;
const p = getPromise((resolve, reject) => {
pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject);
@@ -134,7 +134,7 @@ function wrapPromise(pluginObj:any, methodName:string, args:any[], opts:any={})
return p;
}
function wrapObservable(pluginObj:any, methodName:string, args:any[], opts:any = {}) {
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));
if (pluginResult && pluginResult.error) {
@@ -143,48 +143,48 @@ function wrapObservable(pluginObj:any, methodName:string, args:any[], opts:any =
return () => {
try {
if (opts.clearWithArgs){
if (opts.clearWithArgs) {
return get(window, pluginObj.pluginRef)[opts.clearFunction].apply(pluginObj, args);
}
return get(window, pluginObj.pluginRef)[opts.clearFunction].call(pluginObj, pluginResult);
} catch(e) {
} catch (e) {
console.warn('Unable to clear the previous observable watch for', pluginObj.name, methodName);
console.error(e);
}
}
};
});
}
function callInstance(pluginObj:any, methodName : string, args:any[], opts:any = {}, resolve? : Function, reject? : Function){
args = setIndex(args, opts, resolve, reject);
return pluginObj._objectInstance[methodName].apply(pluginObj._objectInstance, args);
function callInstance(pluginObj: any, methodName: string, args: any[], opts: any = {}, resolve?: Function, reject?: Function) {
args = setIndex(args, opts, resolve, reject);
return pluginObj._objectInstance[methodName].apply(pluginObj._objectInstance, args);
}
function wrapInstance (pluginObj:any, methodName:string, args:any[], opts:any = {}){
function wrapInstance (pluginObj: any, methodName: string, args: any[], opts: any = {}) {
return (...args) => {
if (opts.sync) {
return callInstance(pluginObj, methodName, args, opts);
} else if (opts.observable) {
return new Observable(observer => {
let pluginResult = callInstance(pluginObj, methodName,args, opts, observer.next.bind(observer), observer.error.bind(observer));
let pluginResult = callInstance(pluginObj, methodName, args, opts, observer.next.bind(observer), observer.error.bind(observer));
return () => {
try {
if (opts.clearWithArgs){
if (opts.clearWithArgs) {
return pluginObj._objectInstance[opts.clearFunction].apply(pluginObj._objectInstance, args);
}
return pluginObj._objectInstance[opts.clearFunction].call(pluginObj, pluginResult);
} catch(e) {
} catch (e) {
console.warn('Unable to clear the previous observable watch for', pluginObj.name, methodName);
console.error(e);
}
}
};
});
} else {
return getPromise((resolve, reject) => {
callInstance(pluginObj, methodName, args, opts, resolve, reject);
});
}
}
};
}
/**
@@ -192,9 +192,9 @@ function wrapInstance (pluginObj:any, methodName:string, args:any[], opts:any =
* @param event
* @returns {Observable}
*/
function wrapEventObservable (event : string) : Observable<any> {
function wrapEventObservable (event: string): Observable<any> {
return new Observable(observer => {
let callback = (status : any) => observer.next(status);
let callback = (status: any) => observer.next(status);
window.addEventListener(event, callback, false);
return () => window.removeEventListener(event, callback, false);
});
@@ -207,10 +207,10 @@ function wrapEventObservable (event : string) : Observable<any> {
* @param opts
* @returns {function(...[any]): (undefined|*|Observable|*|*)}
*/
export const wrap = function(pluginObj:any, methodName:string, 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)
@@ -221,7 +221,7 @@ export const wrap = function(pluginObj:any, methodName:string, opts:any = {}) {
else
return wrapPromise(pluginObj, methodName, args, opts);
}
};
};
/**
@@ -256,7 +256,7 @@ export function Plugin(config) {
};
return cls;
}
};
}
/**
@@ -265,7 +265,7 @@ export function Plugin(config) {
* Wrap a stub function in a call to a Cordova plugin, checking if both Cordova
* and the required plugin are installed.
*/
export function Cordova(opts:any = {}) {
export function Cordova(opts: any = {}) {
return (target: Object, methodName: string, descriptor: TypedPropertyDescriptor<any>) => {
let originalMethod = descriptor.value;
@@ -273,8 +273,8 @@ export function Cordova(opts:any = {}) {
value: function(...args: any[]) {
return wrap(this, methodName, opts).apply(this, args);
}
}
}
};
};
}
/**
@@ -282,14 +282,14 @@ export function Cordova(opts:any = {}) {
*
* Wrap an instance method
*/
export function CordovaInstance(opts:any = {}) {
return (target: Object, methodName: string) => {
return {
value: function(...args: any[]) {
return wrapInstance(this, methodName, opts).apply(this, args);
}
}
}
export function CordovaInstance(opts: any = {}) {
return (target: Object, methodName: string) => {
return {
value: function(...args: any[]) {
return wrapInstance(this, methodName, opts).apply(this, args);
}
};
};
}
/**
@@ -302,15 +302,15 @@ export function CordovaProperty(target: Function, key: string, descriptor: Typed
let originalMethod = descriptor.get;
descriptor.get = function(...args: any[]) {
if(!window.cordova) {
if (!window.cordova) {
cordovaWarn(this.name, null);
return {};
}
let pluginInstance = getPlugin(this.pluginRef);
if(!pluginInstance) {
if (!pluginInstance) {
pluginWarn(this, key);
return {};
return { };
}
return originalMethod.apply(this, args);
};