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