mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2025-05-14 14:44:06 +08:00
refactor(plugin):
This commit is contained in:
parent
b329d9f36b
commit
8d82aff41f
@ -1,4 +1,4 @@
|
|||||||
import {get} from '../util';
|
import { get } from '../util';
|
||||||
|
|
||||||
declare var window;
|
declare var window;
|
||||||
declare var Promise;
|
declare var Promise;
|
||||||
@ -36,7 +36,7 @@ export const cordovaWarn = function(pluginName: string, method: string) {
|
|||||||
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');
|
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');
|
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)
|
||||||
if (opts.callbackOrder === 'reverse') {
|
if (opts.callbackOrder === 'reverse') {
|
||||||
// Get those arguments in the order [resolve, reject, ...restOfArgs]
|
// Get those arguments in the order [resolve, reject, ...restOfArgs]
|
||||||
@ -55,10 +55,10 @@ function setIndex (args: any[], opts: any= {}, resolve?: Function, reject?: Func
|
|||||||
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);
|
||||||
|
|
||||||
@ -96,7 +96,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;
|
let pluginResult, rej;
|
||||||
const p = getPromise((resolve, reject) => {
|
const p = getPromise((resolve, reject) => {
|
||||||
pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject);
|
pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject);
|
||||||
@ -106,7 +106,7 @@ function wrapPromise(pluginObj: any, methodName: string, args: any[], opts: any=
|
|||||||
// a warning that Cordova is undefined or the plugin is uninstalled, so there is no reason
|
// a warning that Cordova is undefined or the plugin is uninstalled, so there is no reason
|
||||||
// to error
|
// to error
|
||||||
if (pluginResult && pluginResult.error) {
|
if (pluginResult && pluginResult.error) {
|
||||||
p.catch(() => {});
|
p.catch(() => { });
|
||||||
rej(pluginResult.error);
|
rej(pluginResult.error);
|
||||||
}
|
}
|
||||||
return p;
|
return p;
|
||||||
@ -139,7 +139,7 @@ function callInstance(pluginObj: any, methodName: string, args: any[], opts: any
|
|||||||
return pluginObj._objectInstance[methodName].apply(pluginObj._objectInstance, args);
|
return pluginObj._objectInstance[methodName].apply(pluginObj._objectInstance, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
function wrapInstance (pluginObj: any, methodName: string, opts: any = {}) {
|
function wrapInstance(pluginObj: any, methodName: string, opts: any = {}) {
|
||||||
return (...args) => {
|
return (...args) => {
|
||||||
if (opts.sync) {
|
if (opts.sync) {
|
||||||
// Sync doesn't wrap the plugin with a promise or observable, it returns the result as-is
|
// Sync doesn't wrap the plugin with a promise or observable, it returns the result as-is
|
||||||
@ -172,7 +172,7 @@ function wrapInstance (pluginObj: any, methodName: string, opts: any = {}) {
|
|||||||
* @param event
|
* @param event
|
||||||
* @returns {Observable}
|
* @returns {Observable}
|
||||||
*/
|
*/
|
||||||
function wrapEventObservable (event: string): Observable<any> {
|
function wrapEventObservable(event: string): Observable<any> {
|
||||||
return new Observable(observer => {
|
return new Observable(observer => {
|
||||||
window.addEventListener(event, observer.next.bind(observer), false);
|
window.addEventListener(event, observer.next.bind(observer), false);
|
||||||
return () => window.removeEventListener(event, observer.next.bind(observer), false);
|
return () => window.removeEventListener(event, observer.next.bind(observer), false);
|
||||||
@ -186,7 +186,7 @@ function wrapEventObservable (event: string): Observable<any> {
|
|||||||
* @param opts
|
* @param opts
|
||||||
* @returns {function(...[any]): (undefined|*|Observable|*|*)}
|
* @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) => {
|
return (...args) => {
|
||||||
|
|
||||||
if (opts.sync)
|
if (opts.sync)
|
||||||
@ -288,7 +288,7 @@ export function CordovaProperty(target: Function, key: string, descriptor: Typed
|
|||||||
let pluginInstance = getPlugin(pluginObj.pluginRef);
|
let pluginInstance = getPlugin(pluginObj.pluginRef);
|
||||||
if (!pluginInstance) {
|
if (!pluginInstance) {
|
||||||
pluginWarn(this, key);
|
pluginWarn(this, key);
|
||||||
return { };
|
return {};
|
||||||
}
|
}
|
||||||
return originalMethod.apply(this, args);
|
return originalMethod.apply(this, args);
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user