mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2025-01-31 18:49:43 +08:00
Use AngularJS promise if available
This commit is contained in:
parent
fc9dcc8e64
commit
0cc1a2b2ff
@ -2,13 +2,15 @@ import { CordovaOptions } from './interfaces';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import 'rxjs/add/observable/fromEvent';
|
||||
|
||||
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(callback: Function) {
|
||||
export function getPromise<T>(callback: (resolve: Function, reject?: Function) => any): Promise<T> {
|
||||
const tryNativePromise = () => {
|
||||
if (Promise) {
|
||||
return new Promise((resolve, reject) => {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
callback(resolve, reject);
|
||||
});
|
||||
} else {
|
||||
@ -16,6 +18,17 @@ export function getPromise(callback: Function) {
|
||||
}
|
||||
};
|
||||
|
||||
if (window.angular) {
|
||||
const injector = window.angular.element(document.querySelector('[ng-app]') || document.body).injector();
|
||||
if (injector) {
|
||||
let $q = injector.get('$q');
|
||||
return $q((resolve: Function, reject: Function) => {
|
||||
callback(resolve, reject);
|
||||
});
|
||||
}
|
||||
console.warn('Angular 1 was detected but $q couldn\'t be retrieved. This is usually when the app is not bootstrapped on the html or body tag. Falling back to native promises which won\'t trigger an automatic digest when promises resolve.');
|
||||
}
|
||||
|
||||
return tryNativePromise();
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Cordova, CordovaCheck, IonicNativePlugin, Plugin } from '@ionic-native/core';
|
||||
import { Cordova, CordovaCheck, IonicNativePlugin, Plugin, getPromise } from '@ionic-native/core';
|
||||
|
||||
export interface EmailComposerOptions {
|
||||
|
||||
@ -119,7 +119,7 @@ export class EmailComposer extends IonicNativePlugin {
|
||||
*/
|
||||
@CordovaCheck()
|
||||
isAvailable(app?: string): Promise<any> {
|
||||
return new Promise<boolean>((resolve, reject) => {
|
||||
return getPromise<boolean>((resolve, reject) => {
|
||||
if (app) {
|
||||
EmailComposer.getPlugin().isAvailable(app, (isAvailable: boolean) => {
|
||||
if (isAvailable) {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { CordovaCheck, CordovaProperty, IonicNativePlugin, Plugin } from '@ionic-native/core';
|
||||
import { CordovaCheck, CordovaProperty, IonicNativePlugin, Plugin, getPromise } from '@ionic-native/core';
|
||||
|
||||
export interface IFile extends Blob {
|
||||
/**
|
||||
@ -715,7 +715,7 @@ export class File extends IonicNativePlugin {
|
||||
*/
|
||||
@CordovaCheck()
|
||||
getFreeDiskSpace(): Promise<number> {
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
return getPromise<any>((resolve, reject) => {
|
||||
cordova.exec(resolve, reject, 'File', 'getFreeDiskSpace', []);
|
||||
});
|
||||
}
|
||||
@ -1150,7 +1150,7 @@ export class File extends IonicNativePlugin {
|
||||
*/
|
||||
@CordovaCheck()
|
||||
resolveLocalFilesystemUrl(fileUrl: string): Promise<Entry> {
|
||||
return new Promise<Entry>((resolve, reject) => {
|
||||
return getPromise<Entry>((resolve, reject) => {
|
||||
try {
|
||||
window.resolveLocalFileSystemURL(fileUrl, (entry: Entry) => {
|
||||
resolve(entry);
|
||||
@ -1193,7 +1193,7 @@ export class File extends IonicNativePlugin {
|
||||
*/
|
||||
@CordovaCheck()
|
||||
getDirectory(directoryEntry: DirectoryEntry, directoryName: string, flags: Flags): Promise<DirectoryEntry> {
|
||||
return new Promise<DirectoryEntry>((resolve, reject) => {
|
||||
return getPromise<DirectoryEntry>((resolve, reject) => {
|
||||
try {
|
||||
directoryEntry.getDirectory(directoryName, flags, (de) => {
|
||||
resolve(de);
|
||||
@ -1217,7 +1217,7 @@ export class File extends IonicNativePlugin {
|
||||
*/
|
||||
@CordovaCheck()
|
||||
getFile(directoryEntry: DirectoryEntry, fileName: string, flags: Flags): Promise<FileEntry> {
|
||||
return new Promise<FileEntry>((resolve, reject) => {
|
||||
return getPromise<FileEntry>((resolve, reject) => {
|
||||
try {
|
||||
directoryEntry.getFile(fileName, flags, resolve, (err) => {
|
||||
this.fillErrorMessage(err);
|
||||
@ -1267,7 +1267,7 @@ export class File extends IonicNativePlugin {
|
||||
})
|
||||
.then((fileEntry: FileEntry) => {
|
||||
const reader = new FileReader();
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
return getPromise<T>((resolve, reject) => {
|
||||
reader.onloadend = () => {
|
||||
if (reader.result !== undefined || reader.result !== null) {
|
||||
resolve(<T><any>reader.result);
|
||||
@ -1302,7 +1302,7 @@ export class File extends IonicNativePlugin {
|
||||
* @hidden
|
||||
*/
|
||||
private remove(fe: Entry): Promise<RemoveResult> {
|
||||
return new Promise<RemoveResult>((resolve, reject) => {
|
||||
return getPromise<RemoveResult>((resolve, reject) => {
|
||||
fe.remove(() => {
|
||||
resolve({ success: true, fileRemoved: fe });
|
||||
}, (err) => {
|
||||
@ -1316,7 +1316,7 @@ export class File extends IonicNativePlugin {
|
||||
* @hidden
|
||||
*/
|
||||
private move(srce: Entry, destdir: DirectoryEntry, newName: string): Promise<Entry> {
|
||||
return new Promise<Entry>((resolve, reject) => {
|
||||
return getPromise<Entry>((resolve, reject) => {
|
||||
srce.moveTo(destdir, newName, (deste) => {
|
||||
resolve(deste);
|
||||
}, (err) => {
|
||||
@ -1330,7 +1330,7 @@ export class File extends IonicNativePlugin {
|
||||
* @hidden
|
||||
*/
|
||||
private copy(srce: Entry, destdir: DirectoryEntry, newName: string): Promise<Entry> {
|
||||
return new Promise<Entry>((resolve, reject) => {
|
||||
return getPromise<Entry>((resolve, reject) => {
|
||||
srce.copyTo(destdir, newName, (deste) => {
|
||||
resolve(deste);
|
||||
}, (err) => {
|
||||
@ -1344,7 +1344,7 @@ export class File extends IonicNativePlugin {
|
||||
* @hidden
|
||||
*/
|
||||
private readEntries(dr: DirectoryReader): Promise<Entry[]> {
|
||||
return new Promise<Entry[]>((resolve, reject) => {
|
||||
return getPromise<Entry[]>((resolve, reject) => {
|
||||
dr.readEntries((entries) => {
|
||||
resolve(entries);
|
||||
}, (err) => {
|
||||
@ -1358,7 +1358,7 @@ export class File extends IonicNativePlugin {
|
||||
* @hidden
|
||||
*/
|
||||
private rimraf(de: DirectoryEntry): Promise<RemoveResult> {
|
||||
return new Promise<RemoveResult>((resolve, reject) => {
|
||||
return getPromise<RemoveResult>((resolve, reject) => {
|
||||
de.removeRecursively(() => {
|
||||
resolve({ success: true, fileRemoved: de });
|
||||
}, (err) => {
|
||||
@ -1372,7 +1372,7 @@ export class File extends IonicNativePlugin {
|
||||
* @hidden
|
||||
*/
|
||||
private createWriter(fe: FileEntry): Promise<FileWriter> {
|
||||
return new Promise<FileWriter>((resolve, reject) => {
|
||||
return getPromise<FileWriter>((resolve, reject) => {
|
||||
fe.createWriter((writer) => {
|
||||
resolve(writer);
|
||||
}, (err) => {
|
||||
@ -1390,7 +1390,7 @@ export class File extends IonicNativePlugin {
|
||||
return this.writeFileInChunks(writer, gu);
|
||||
}
|
||||
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
return getPromise<any>((resolve, reject) => {
|
||||
writer.onwriteend = (evt) => {
|
||||
if (writer.error) {
|
||||
reject(writer.error);
|
||||
@ -1417,7 +1417,7 @@ export class File extends IonicNativePlugin {
|
||||
writer.write(chunk);
|
||||
}
|
||||
|
||||
return new Promise<any>((resolve, reject) => {
|
||||
return getPromise<any>((resolve, reject) => {
|
||||
writer.onerror = reject;
|
||||
writer.onwrite = () => {
|
||||
if (writtenSize < file.size) {
|
||||
|
@ -6,7 +6,8 @@ import {
|
||||
InstanceCheck,
|
||||
InstanceProperty,
|
||||
IonicNativePlugin,
|
||||
Plugin
|
||||
Plugin,
|
||||
getPromise
|
||||
} from '@ionic-native/core';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
import 'rxjs/add/observable/fromEvent';
|
||||
@ -1013,7 +1014,7 @@ export class BaseClass {
|
||||
*/
|
||||
@InstanceCheck()
|
||||
addListenerOnce(eventName: string): Promise<any> {
|
||||
return new Promise<any>((resolve) => {
|
||||
return getPromise<any>((resolve) => {
|
||||
this._objectInstance.one(eventName, (...args: any[]) => {
|
||||
if (args[args.length - 1] instanceof GoogleMaps.getPlugin().BaseClass) {
|
||||
if (args[args.length - 1].type === 'Map') {
|
||||
@ -1110,7 +1111,7 @@ export class BaseClass {
|
||||
*/
|
||||
@InstanceCheck()
|
||||
one(eventName: string): Promise<any> {
|
||||
return new Promise<any>((resolve) => {
|
||||
return getPromise<any>((resolve) => {
|
||||
this._objectInstance.one(eventName, (...args: any[]) => {
|
||||
if (args[args.length - 1] instanceof GoogleMaps.getPlugin().BaseClass) {
|
||||
if (args[args.length - 1].type === 'Map') {
|
||||
@ -1210,7 +1211,7 @@ export class BaseArrayClass<T> extends BaseClass {
|
||||
*/
|
||||
@CordovaCheck()
|
||||
forEachAsync(fn: ((element: T, callback: () => void) => void)): Promise<void> {
|
||||
return new Promise<void>((resolve) => {
|
||||
return getPromise<void>((resolve) => {
|
||||
this._objectInstance.forEach(fn, resolve);
|
||||
});
|
||||
}
|
||||
@ -1236,7 +1237,7 @@ export class BaseArrayClass<T> extends BaseClass {
|
||||
*/
|
||||
@CordovaCheck()
|
||||
mapAsync(fn: ((element: T, callback: (newElement: any) => void) => void)): Promise<any[]> {
|
||||
return new Promise<any[]>((resolve) => {
|
||||
return getPromise<any[]>((resolve) => {
|
||||
this._objectInstance.map(fn, resolve);
|
||||
});
|
||||
}
|
||||
@ -1260,7 +1261,7 @@ export class BaseArrayClass<T> extends BaseClass {
|
||||
*/
|
||||
@CordovaCheck()
|
||||
filterAsync(fn: (element: T, callback: (result: boolean) => void) => void): Promise<T[]> {
|
||||
return new Promise<any[]>((resolve) => {
|
||||
return getPromise<any[]>((resolve) => {
|
||||
this._objectInstance.filter(fn, resolve);
|
||||
});
|
||||
}
|
||||
@ -1566,7 +1567,7 @@ export class Environment {
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
static getLicenseInfo(): Promise<any> {
|
||||
return new Promise<any>((resolve) => {
|
||||
return getPromise<any>((resolve) => {
|
||||
GoogleMaps.getPlugin().environment.getLicenseInfo((text: string) => resolve(text));
|
||||
});
|
||||
}
|
||||
@ -1626,7 +1627,7 @@ export class Geocoder {
|
||||
// ]
|
||||
// })
|
||||
// -------------------------
|
||||
return new Promise<BaseArrayClass<GeocoderResult>>((resolve, reject) => {
|
||||
return getPromise<BaseArrayClass<GeocoderResult>>((resolve, reject) => {
|
||||
GoogleMaps.getPlugin().Geocoder.geocode(request, (mvcArray: any) => {
|
||||
if (mvcArray) {
|
||||
resolve(new BaseArrayClass(mvcArray));
|
||||
@ -1641,7 +1642,7 @@ export class Geocoder {
|
||||
// address: "Kyoto, Japan"
|
||||
// })
|
||||
// -------------------------
|
||||
return new Promise<GeocoderResult[]>((resolve, reject) => {
|
||||
return getPromise<GeocoderResult[]>((resolve, reject) => {
|
||||
GoogleMaps.getPlugin().Geocoder.geocode(request, (results: GeocoderResult[]) => {
|
||||
if (results) {
|
||||
resolve(results);
|
||||
@ -1945,7 +1946,7 @@ export class GoogleMap extends BaseClass {
|
||||
}
|
||||
};
|
||||
this._objectInstance.one = this._objectInstance.addEventListenerOnce;
|
||||
(new Promise<any>((resolve, reject) => {
|
||||
(getPromise<any>((resolve, reject) => {
|
||||
let count: number = 0;
|
||||
let timer: any = setInterval(() => {
|
||||
let target = document.querySelector('.show-page #' + element);
|
||||
@ -2193,7 +2194,7 @@ export class GoogleMap extends BaseClass {
|
||||
delete this.get('_overlays')[overlayId];
|
||||
});
|
||||
}
|
||||
return new Promise<any>((resolve) => {
|
||||
return getPromise<any>((resolve) => {
|
||||
this._objectInstance.remove(() => resolve());
|
||||
});
|
||||
}
|
||||
@ -2210,7 +2211,7 @@ export class GoogleMap extends BaseClass {
|
||||
delete this.get('_overlays')[overlayId];
|
||||
});
|
||||
}
|
||||
return new Promise<any>((resolve) => {
|
||||
return getPromise<any>((resolve) => {
|
||||
this._objectInstance.clear(() => resolve());
|
||||
});
|
||||
}
|
||||
@ -2315,7 +2316,7 @@ export class GoogleMap extends BaseClass {
|
||||
*/
|
||||
@InstanceCheck()
|
||||
addMarker(options: MarkerOptions): Promise<Marker | any> {
|
||||
return new Promise<Marker>((resolve, reject) => {
|
||||
return getPromise<Marker>((resolve, reject) => {
|
||||
this._objectInstance.addMarker(options, (marker: any) => {
|
||||
if (marker) {
|
||||
let overlayId: string = marker.getId();
|
||||
@ -2337,7 +2338,7 @@ export class GoogleMap extends BaseClass {
|
||||
|
||||
@InstanceCheck()
|
||||
addMarkerCluster(options: MarkerClusterOptions): Promise<MarkerCluster | any> {
|
||||
return new Promise<MarkerCluster>((resolve, reject) => {
|
||||
return getPromise<MarkerCluster>((resolve, reject) => {
|
||||
this._objectInstance.addMarkerCluster(options, (markerCluster: any) => {
|
||||
if (markerCluster) {
|
||||
let overlayId = markerCluster.getId();
|
||||
@ -2364,7 +2365,7 @@ export class GoogleMap extends BaseClass {
|
||||
*/
|
||||
@InstanceCheck()
|
||||
addCircle(options: CircleOptions): Promise<Circle | any> {
|
||||
return new Promise<Circle>((resolve, reject) => {
|
||||
return getPromise<Circle>((resolve, reject) => {
|
||||
this._objectInstance.addCircle(options, (circle: any) => {
|
||||
if (circle) {
|
||||
let overlayId: string = circle.getId();
|
||||
@ -2390,7 +2391,7 @@ export class GoogleMap extends BaseClass {
|
||||
*/
|
||||
@InstanceCheck()
|
||||
addPolygon(options: PolygonOptions): Promise<Polygon | any> {
|
||||
return new Promise<Polygon>((resolve, reject) => {
|
||||
return getPromise<Polygon>((resolve, reject) => {
|
||||
this._objectInstance.addPolygon(options, (polygon: any) => {
|
||||
if (polygon) {
|
||||
let overlayId: string = polygon.getId();
|
||||
@ -2416,7 +2417,7 @@ export class GoogleMap extends BaseClass {
|
||||
*/
|
||||
@InstanceCheck()
|
||||
addPolyline(options: PolylineOptions): Promise<Polyline | any> {
|
||||
return new Promise<Polyline>((resolve, reject) => {
|
||||
return getPromise<Polyline>((resolve, reject) => {
|
||||
this._objectInstance.addPolyline(options, (polyline: any) => {
|
||||
if (polyline) {
|
||||
let overlayId: string = polyline.getId();
|
||||
@ -2441,7 +2442,7 @@ export class GoogleMap extends BaseClass {
|
||||
*/
|
||||
@InstanceCheck()
|
||||
addTileOverlay(options: TileOverlayOptions): Promise<TileOverlay | any> {
|
||||
return new Promise<TileOverlay>((resolve, reject) => {
|
||||
return getPromise<TileOverlay>((resolve, reject) => {
|
||||
this._objectInstance.addTileOverlay(options, (tileOverlay: any) => {
|
||||
if (tileOverlay) {
|
||||
let overlayId: string = tileOverlay.getId();
|
||||
@ -2466,7 +2467,7 @@ export class GoogleMap extends BaseClass {
|
||||
*/
|
||||
@InstanceCheck()
|
||||
addGroundOverlay(options: GroundOverlayOptions): Promise<GroundOverlay | any> {
|
||||
return new Promise<GroundOverlay>((resolve, reject) => {
|
||||
return getPromise<GroundOverlay>((resolve, reject) => {
|
||||
this._objectInstance.addGroundOverlay(options, (groundOverlay: any) => {
|
||||
if (groundOverlay) {
|
||||
let overlayId: string = groundOverlay.getId();
|
||||
@ -2507,7 +2508,7 @@ export class GoogleMap extends BaseClass {
|
||||
// */
|
||||
// @InstanceCheck()
|
||||
// addKmlOverlay(options: KmlOverlayOptions): Promise<KmlOverlay | any> {
|
||||
// return new Promise<KmlOverlay>((resolve, reject) => {
|
||||
// return getPromise<KmlOverlay>((resolve, reject) => {
|
||||
// this._objectInstance.addKmlOverlay(options, (kmlOverlay: any) => {
|
||||
// if (kmlOverlay) {
|
||||
// resolve(new KmlOverlay(kmlOverlay));
|
||||
@ -3530,7 +3531,7 @@ export class TileOverlay extends BaseClass {
|
||||
// if (!this._objectInstance) {
|
||||
// return Promise.reject({ error: 'plugin_not_installed' });
|
||||
// }
|
||||
// return new Promise<any>(
|
||||
// return getPromise<any>(
|
||||
// resolve => this._objectInstance.addListenerOnce(eventName, resolve)
|
||||
// );
|
||||
// }
|
||||
@ -3579,7 +3580,7 @@ export class TileOverlay extends BaseClass {
|
||||
// if (!this._objectInstance) {
|
||||
// return Promise.reject({ error: 'plugin_not_installed' });
|
||||
// }
|
||||
// return new Promise<any>(
|
||||
// return getPromise<any>(
|
||||
// resolve => this._objectInstance.one(eventName, resolve)
|
||||
// );
|
||||
// }
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Cordova, CordovaCheck, IonicNativePlugin, Plugin } from '@ionic-native/core';
|
||||
import { Cordova, CordovaCheck, IonicNativePlugin, Plugin, getPromise } from '@ionic-native/core';
|
||||
|
||||
export interface PrintOptions {
|
||||
/**
|
||||
@ -90,7 +90,7 @@ export class Printer extends IonicNativePlugin {
|
||||
*/
|
||||
@CordovaCheck()
|
||||
check(): Promise<any> {
|
||||
return new Promise<any>((resolve: Function) => {
|
||||
return getPromise<any>((resolve: Function) => {
|
||||
Printer.getPlugin()
|
||||
.check((avail: boolean, count: any) => {
|
||||
resolve({ avail, count });
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { IonicNativePlugin, Plugin } from '@ionic-native/core';
|
||||
import { IonicNativePlugin, Plugin, getPromise } from '@ionic-native/core';
|
||||
|
||||
declare const navigator: any;
|
||||
|
||||
@ -42,7 +42,7 @@ export class Screenshot extends IonicNativePlugin {
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
save(format?: string, quality?: number, filename?: string): Promise<any> {
|
||||
return new Promise<any>(
|
||||
return getPromise<any>(
|
||||
(resolve, reject) => {
|
||||
navigator.screenshot.save(
|
||||
(error: any, result: any) => {
|
||||
@ -68,7 +68,7 @@ export class Screenshot extends IonicNativePlugin {
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
URI(quality?: number): Promise<any> {
|
||||
return new Promise<any>(
|
||||
return getPromise<any>(
|
||||
(resolve, reject) => {
|
||||
navigator.screenshot.URI(
|
||||
(error: any, result: any) => {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { CordovaCheck, CordovaInstance, IonicNativePlugin, Plugin } from '@ionic-native/core';
|
||||
import { CordovaCheck, CordovaInstance, IonicNativePlugin, Plugin, getPromise } from '@ionic-native/core';
|
||||
|
||||
/**
|
||||
* @hidden
|
||||
@ -142,7 +142,7 @@ export class SecureStorage extends IonicNativePlugin {
|
||||
*/
|
||||
@CordovaCheck()
|
||||
create(store: string): Promise<SecureStorageObject> {
|
||||
return new Promise((res: Function, rej: Function) => {
|
||||
return getPromise<SecureStorageObject>((res: Function, rej: Function) => {
|
||||
const instance = new (SecureStorage.getPlugin())(() => res(new SecureStorageObject(instance)), rej, store);
|
||||
});
|
||||
}
|
||||
|
@ -5,7 +5,8 @@ import {
|
||||
CordovaInstance,
|
||||
InstanceProperty,
|
||||
IonicNativePlugin,
|
||||
Plugin
|
||||
Plugin,
|
||||
getPromise
|
||||
} from '@ionic-native/core';
|
||||
|
||||
declare const sqlitePlugin: any;
|
||||
@ -192,7 +193,7 @@ export class SQLite extends IonicNativePlugin {
|
||||
*/
|
||||
@CordovaCheck()
|
||||
create(config: SQLiteDatabaseConfig): Promise<SQLiteObject> {
|
||||
return new Promise((resolve, reject) => {
|
||||
return getPromise<SQLiteObject>((resolve, reject) => {
|
||||
sqlitePlugin.openDatabase(config, (db: any) => resolve(new SQLiteObject(db)), reject);
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user