refactoring

This commit is contained in:
Max Lynch
2015-11-29 19:54:45 -06:00
parent 9d54bfcd0b
commit dfef6dcfe8
42 changed files with 719 additions and 185 deletions
+31 -7
View File
@@ -39,11 +39,22 @@ export const wrap = function(pluginObj, methodName, opts: any = {}) {
return;
}
if(typeof opts.successIndex !== 'undefined') {
// Try to figure out where the success/error callbacks need to be bound
// to our promise resolve/reject handlers.
// If the plugin method expects myMethod(success, err, options)
if(opts.callbackOrder == 'reverse') {
args[0] = resolve;
args[1] = reject;
} else if(typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') {
// If we've specified a success/error index
args[opts.successIndex] = resolve;
}
if(typeof opts.errorIndex !== 'undefined') {
args[opts.errorIndex] = 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);
}
let pluginInstance = getPlugin(pluginObj.pluginRef);
@@ -61,6 +72,9 @@ export const wrap = function(pluginObj, methodName, opts: any = {}) {
}
}
/**
* Class decorator specifying Plugin metadata. Required for all plugins.
*/
export function Plugin(config) {
return function(cls) {
@@ -73,15 +87,25 @@ 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 = {}) {
return function(obj, methodName) {
if(opts.promise) {
console.log('TODO: Promise');
return (target: Object, methodName: string, descriptor: TypedPropertyDescriptor<any>) => {
let originalMethod = descriptor.value;
return {
value: function(...args: any[]) {
return wrap(this, methodName, opts)();
}
}
obj[methodName] = wrap(obj, methodName, opts).bind(obj);
}
}
/**
* Before calling the original method, ensure Cordova and the plugin are installed.
*/
export function RequiresPlugin(target: Function, key: string, descriptor: TypedPropertyDescriptor<any>) {
let originalMethod = descriptor.value;