mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2026-05-02 00:07:23 +08:00
refactoring
This commit is contained in:
Vendored
+36
-8
@@ -38,12 +38,24 @@ exports.wrap = function (pluginObj, methodName, opts) {
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (typeof opts.successIndex !== 'undefined') {
|
||||
args[opts.successIndex] = resolve;
|
||||
// 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;
|
||||
}
|
||||
if (typeof opts.errorIndex !== 'undefined') {
|
||||
else if (typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') {
|
||||
// If we've specified a success/error index
|
||||
args[opts.successIndex] = resolve;
|
||||
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);
|
||||
}
|
||||
var pluginInstance = exports.getPlugin(pluginObj.pluginRef);
|
||||
if (!pluginInstance) {
|
||||
exports.pluginWarn(pluginObj.name, methodName, pluginObj.name);
|
||||
@@ -56,6 +68,9 @@ exports.wrap = function (pluginObj, methodName, opts) {
|
||||
});
|
||||
};
|
||||
};
|
||||
/**
|
||||
* Class decorator specifying Plugin metadata. Required for all plugins.
|
||||
*/
|
||||
function Plugin(config) {
|
||||
return function (cls) {
|
||||
// Add these fields to the class
|
||||
@@ -66,16 +81,29 @@ function Plugin(config) {
|
||||
};
|
||||
}
|
||||
exports.Plugin = Plugin;
|
||||
/**
|
||||
* Wrap a stub function in a call to a Cordova plugin, checking if both Cordova
|
||||
* and the required plugin are installed.
|
||||
*/
|
||||
function Cordova(opts) {
|
||||
if (opts === void 0) { opts = {}; }
|
||||
return function (obj, methodName) {
|
||||
if (opts.promise) {
|
||||
console.log('TODO: Promise');
|
||||
}
|
||||
obj[methodName] = exports.wrap(obj, methodName, opts).bind(obj);
|
||||
return function (target, methodName, descriptor) {
|
||||
var originalMethod = descriptor.value;
|
||||
return {
|
||||
value: function () {
|
||||
var args = [];
|
||||
for (var _i = 0; _i < arguments.length; _i++) {
|
||||
args[_i - 0] = arguments[_i];
|
||||
}
|
||||
return exports.wrap(this, methodName, opts)();
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
exports.Cordova = Cordova;
|
||||
/**
|
||||
* Before calling the original method, ensure Cordova and the plugin are installed.
|
||||
*/
|
||||
function RequiresPlugin(target, key, descriptor) {
|
||||
var originalMethod = descriptor.value;
|
||||
descriptor.value = function () {
|
||||
|
||||
Reference in New Issue
Block a user