mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2025-01-18 16:02:53 +08:00
refactor(): small changes
This commit is contained in:
parent
184986f06b
commit
92140cd2db
@ -32,9 +32,7 @@ gulp.task('lint', () => {
|
||||
|
||||
gulp.task('plugin:create', () => {
|
||||
if (flags.n && flags.n !== '') {
|
||||
const src = flags.m
|
||||
? './scripts/templates/wrap-min.tmpl'
|
||||
: './scripts/templates/wrap.tmpl',
|
||||
const src = flags.m ? './scripts/templates/wrap-min.tmpl' : './scripts/templates/wrap.tmpl',
|
||||
pluginName = flags.n,
|
||||
spaced = pluginName.replace(/(?!^)([A-Z])/g, ' $1'),
|
||||
kebabCase = _.kebabCase(pluginName);
|
||||
|
@ -9,9 +9,7 @@ export function checkReady() {
|
||||
|
||||
let didFireReady = false;
|
||||
document.addEventListener('deviceready', () => {
|
||||
console.log(
|
||||
`Ionic Native: deviceready event fired after ${Date.now() - before} ms`
|
||||
);
|
||||
console.log(`Ionic Native: deviceready event fired after ${Date.now() - before} ms`);
|
||||
didFireReady = true;
|
||||
});
|
||||
|
||||
|
@ -30,13 +30,9 @@ class MockInstancePluginObject {
|
||||
|
||||
class MockCordovaPlugin {
|
||||
static ping = jest.fn((arg: string) => 'pong');
|
||||
static pingAsync = jest.fn(
|
||||
(arg: string, success: Function, error: Function) => success('pong')
|
||||
);
|
||||
static pingAsync = jest.fn((arg: string, success: Function, error: Function) => success('pong'));
|
||||
ping = jest.fn((arg: string) => 'pong');
|
||||
pingAsync = jest.fn((arg: string, success: Function, error: Function) =>
|
||||
success('pong')
|
||||
);
|
||||
pingAsync = jest.fn((arg: string, success: Function, error: Function) => success('pong'));
|
||||
}
|
||||
|
||||
describe('Common decorator functions', () => {
|
||||
@ -73,12 +69,8 @@ describe('Common decorator functions', () => {
|
||||
|
||||
test('original method should have received args', () => {
|
||||
expect(MockCordovaPlugin.pingAsync.mock.calls[0][0]).toBe('pingpong');
|
||||
expect(typeof MockCordovaPlugin.pingAsync.mock.calls[0][1]).toBe(
|
||||
'function'
|
||||
);
|
||||
expect(typeof MockCordovaPlugin.pingAsync.mock.calls[0][2]).toBe(
|
||||
'function'
|
||||
);
|
||||
expect(typeof MockCordovaPlugin.pingAsync.mock.calls[0][1]).toBe('function');
|
||||
expect(typeof MockCordovaPlugin.pingAsync.mock.calls[0][2]).toBe('function');
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Observable, fromEvent } from 'rxjs';
|
||||
import { fromEvent, Observable } from 'rxjs';
|
||||
|
||||
import { CordovaOptions } from './interfaces';
|
||||
|
||||
@ -7,9 +7,7 @@ declare const window: any;
|
||||
export const ERR_CORDOVA_NOT_AVAILABLE = { error: 'cordova_not_available' };
|
||||
export const ERR_PLUGIN_NOT_INSTALLED = { error: 'plugin_not_installed' };
|
||||
|
||||
export function getPromise<T>(
|
||||
callback: (resolve: Function, reject?: Function) => any
|
||||
): Promise<T> {
|
||||
export function getPromise<T>(callback: (resolve: Function, reject?: Function) => any): Promise<T> {
|
||||
const tryNativePromise = () => {
|
||||
if (Promise) {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
@ -58,14 +56,7 @@ export function wrapPromise(
|
||||
(...args: any[]) => reject(args)
|
||||
);
|
||||
} else {
|
||||
pluginResult = callCordovaPlugin(
|
||||
pluginObj,
|
||||
methodName,
|
||||
args,
|
||||
opts,
|
||||
resolve,
|
||||
reject
|
||||
);
|
||||
pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject);
|
||||
}
|
||||
rej = reject;
|
||||
});
|
||||
@ -79,12 +70,7 @@ export function wrapPromise(
|
||||
return p;
|
||||
}
|
||||
|
||||
function wrapOtherPromise(
|
||||
pluginObj: any,
|
||||
methodName: string,
|
||||
args: any[],
|
||||
opts: any = {}
|
||||
) {
|
||||
function wrapOtherPromise(pluginObj: any, methodName: string, args: any[], opts: any = {}) {
|
||||
return getPromise((resolve: Function, reject: Function) => {
|
||||
const pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts);
|
||||
if (pluginResult) {
|
||||
@ -99,12 +85,7 @@ function wrapOtherPromise(
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@ -203,10 +184,7 @@ export function checkAvailability(
|
||||
|
||||
pluginInstance = getPlugin(pluginRef);
|
||||
|
||||
if (
|
||||
!pluginInstance ||
|
||||
(!!methodName && typeof pluginInstance[methodName] === 'undefined')
|
||||
) {
|
||||
if (!pluginInstance || (!!methodName && typeof pluginInstance[methodName] === 'undefined')) {
|
||||
if (!window.cordova) {
|
||||
cordovaWarn(pluginName, methodName);
|
||||
return ERR_CORDOVA_NOT_AVAILABLE;
|
||||
@ -223,23 +201,14 @@ export function checkAvailability(
|
||||
* Checks if _objectInstance exists and has the method/property
|
||||
* @private
|
||||
*/
|
||||
export function instanceAvailability(
|
||||
pluginObj: any,
|
||||
methodName?: string
|
||||
): boolean {
|
||||
export function instanceAvailability(pluginObj: any, methodName?: string): boolean {
|
||||
return (
|
||||
pluginObj._objectInstance &&
|
||||
(!methodName ||
|
||||
typeof pluginObj._objectInstance[methodName] !== 'undefined')
|
||||
(!methodName || typeof pluginObj._objectInstance[methodName] !== 'undefined')
|
||||
);
|
||||
}
|
||||
|
||||
export function setIndex(
|
||||
args: any[],
|
||||
opts: any = {},
|
||||
resolve?: Function,
|
||||
reject?: Function
|
||||
): any {
|
||||
export function setIndex(args: any[], opts: any = {}, resolve?: Function, reject?: Function): any {
|
||||
// ignore resolve and reject in case sync
|
||||
if (opts.sync) {
|
||||
return args;
|
||||
@ -258,19 +227,12 @@ export function setIndex(
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
} else if (
|
||||
opts.callbackStyle === 'object' &&
|
||||
opts.successName &&
|
||||
opts.errorName
|
||||
) {
|
||||
} else if (opts.callbackStyle === 'object' && opts.successName && opts.errorName) {
|
||||
const obj: any = {};
|
||||
obj[opts.successName] = resolve;
|
||||
obj[opts.errorName] = reject;
|
||||
args.push(obj);
|
||||
} else if (
|
||||
typeof opts.successIndex !== 'undefined' ||
|
||||
typeof opts.errorIndex !== 'undefined'
|
||||
) {
|
||||
} else if (typeof opts.successIndex !== 'undefined' || typeof opts.errorIndex !== 'undefined') {
|
||||
const setSuccessIndex = () => {
|
||||
// If we've specified a success/error index
|
||||
if (opts.successIndex > args.length) {
|
||||
@ -339,10 +301,7 @@ export function callInstance(
|
||||
args = setIndex(args, opts, resolve, reject);
|
||||
|
||||
if (instanceAvailability(pluginObj, methodName)) {
|
||||
return pluginObj._objectInstance[methodName].apply(
|
||||
pluginObj._objectInstance,
|
||||
args
|
||||
);
|
||||
return pluginObj._objectInstance[methodName].apply(pluginObj._objectInstance, args);
|
||||
}
|
||||
}
|
||||
|
||||
@ -362,11 +321,7 @@ export function get(element: Element | Window, path: string) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
export function pluginWarn(
|
||||
pluginName: string,
|
||||
plugin?: string,
|
||||
method?: string
|
||||
): void {
|
||||
export function pluginWarn(pluginName: string, plugin?: string, method?: string): void {
|
||||
if (method) {
|
||||
console.warn(
|
||||
'Native: tried calling ' +
|
||||
@ -378,14 +333,10 @@ export function pluginWarn(
|
||||
' plugin is not installed.'
|
||||
);
|
||||
} else {
|
||||
console.warn(
|
||||
`Native: tried accessing the ${pluginName} plugin but it's not installed.`
|
||||
);
|
||||
console.warn(`Native: tried accessing the ${pluginName} plugin but it's not installed.`);
|
||||
}
|
||||
if (plugin) {
|
||||
console.warn(
|
||||
`Install the ${pluginName} plugin: 'ionic cordova plugin add ${plugin}'`
|
||||
);
|
||||
console.warn(`Install the ${pluginName} plugin: 'ionic cordova plugin add ${plugin}'`);
|
||||
}
|
||||
}
|
||||
|
||||
@ -419,11 +370,7 @@ export type WrapFn = (...args: any[]) => any;
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
export const wrap = (
|
||||
pluginObj: any,
|
||||
methodName: string,
|
||||
opts: CordovaOptions = {}
|
||||
): WrapFn => {
|
||||
export const wrap = (pluginObj: any, methodName: string, opts: CordovaOptions = {}): WrapFn => {
|
||||
return (...args: any[]) => {
|
||||
if (opts.sync) {
|
||||
// Sync doesn't wrap the plugin with a promise or observable, it returns the result as-is
|
||||
@ -443,11 +390,7 @@ export const wrap = (
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
export function wrapInstance(
|
||||
pluginObj: any,
|
||||
methodName: string,
|
||||
opts: any = {}
|
||||
): Function {
|
||||
export function wrapInstance(pluginObj: any, methodName: string, opts: any = {}): Function {
|
||||
return (...args: any[]) => {
|
||||
if (opts.sync) {
|
||||
return callInstance(pluginObj, methodName, args, opts);
|
||||
@ -515,14 +458,7 @@ export function wrapInstance(
|
||||
(...args: any[]) => reject(args)
|
||||
);
|
||||
} else {
|
||||
result = callInstance(
|
||||
pluginObj,
|
||||
methodName,
|
||||
args,
|
||||
opts,
|
||||
resolve,
|
||||
reject
|
||||
);
|
||||
result = callInstance(pluginObj, methodName, args, opts, resolve, reject);
|
||||
}
|
||||
if (result && result.then) {
|
||||
result.then(resolve, reject);
|
||||
@ -543,14 +479,7 @@ export function wrapInstance(
|
||||
(...args: any[]) => reject(args)
|
||||
);
|
||||
} else {
|
||||
pluginResult = callInstance(
|
||||
pluginObj,
|
||||
methodName,
|
||||
args,
|
||||
opts,
|
||||
resolve,
|
||||
reject
|
||||
);
|
||||
pluginResult = callInstance(pluginObj, methodName, args, opts, resolve, reject);
|
||||
}
|
||||
rej = reject;
|
||||
});
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { Observable, Observer } from 'rxjs';
|
||||
|
||||
import { checkAvailability, getPlugin } from './common';
|
||||
|
||||
function overrideFunction(pluginObj: any, methodName: string): Observable<any> {
|
||||
|
@ -1,7 +1,12 @@
|
||||
import { wrapInstance } from './common';
|
||||
import { CordovaOptions } from './interfaces';
|
||||
|
||||
export function cordovaInstance(pluginObj: any, methodName: string, config: CordovaOptions, args: IArguments | any[]) {
|
||||
export function cordovaInstance(
|
||||
pluginObj: any,
|
||||
methodName: string,
|
||||
config: CordovaOptions,
|
||||
args: IArguments | any[]
|
||||
) {
|
||||
args = Array.from(args);
|
||||
return wrapInstance(pluginObj, methodName, config).apply(this, args);
|
||||
}
|
||||
|
@ -1,6 +1,11 @@
|
||||
import { wrap } from './common';
|
||||
import { CordovaOptions } from './interfaces';
|
||||
|
||||
export function cordova(pluginObj: any, methodName: string, config: CordovaOptions, args: IArguments | any[]) {
|
||||
export function cordova(
|
||||
pluginObj: any,
|
||||
methodName: string,
|
||||
config: CordovaOptions,
|
||||
args: IArguments | any[]
|
||||
) {
|
||||
return wrap(pluginObj, methodName, config).apply(this, args);
|
||||
}
|
||||
|
@ -18,31 +18,42 @@ export class IonicNativePlugin {
|
||||
* Returns a boolean that indicates whether the plugin is installed
|
||||
* @return {boolean}
|
||||
*/
|
||||
static installed(): boolean { return checkAvailability(this.pluginRef) === true; }
|
||||
static installed(): boolean {
|
||||
return checkAvailability(this.pluginRef) === true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the original plugin object
|
||||
*/
|
||||
static getPlugin(): any { return get(window, this.pluginRef); }
|
||||
static getPlugin(): any {
|
||||
return get(window, this.pluginRef);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the plugin's name
|
||||
*/
|
||||
static getPluginName(): string { return this.pluginName; }
|
||||
static getPluginName(): string {
|
||||
return this.pluginName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the plugin's reference
|
||||
*/
|
||||
static getPluginRef(): string { return this.pluginRef; }
|
||||
static getPluginRef(): string {
|
||||
return this.pluginRef;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the plugin's install name
|
||||
*/
|
||||
static getPluginInstallName(): string { return this.plugin; }
|
||||
static getPluginInstallName(): string {
|
||||
return this.plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the plugin's supported platforms
|
||||
*/
|
||||
static getSupportedPlatforms(): string[] { return this.platforms || []; }
|
||||
|
||||
static getSupportedPlatforms(): string[] {
|
||||
return this.platforms || [];
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,9 @@ export function get(element: Element | Window, path: string) {
|
||||
const paths: string[] = path.split('.');
|
||||
let obj: any = element;
|
||||
for (let i = 0; i < paths.length; i++) {
|
||||
if (!obj) { return null; }
|
||||
if (!obj) {
|
||||
return null;
|
||||
}
|
||||
obj = obj[paths[i]];
|
||||
}
|
||||
return obj;
|
||||
@ -17,7 +19,6 @@ export function get(element: Element | Window, path: string) {
|
||||
* @private
|
||||
*/
|
||||
export function getPromise(callback: Function = () => {}): Promise<any> {
|
||||
|
||||
const tryNativePromise = () => {
|
||||
if (window.Promise) {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
|
Loading…
Reference in New Issue
Block a user