refactor(cordova): using promise instead of queue

This commit is contained in:
Manu Mtz.-Almeida
2018-02-08 18:02:26 +01:00
parent 74cce164a3
commit 8fe5742d7f
6 changed files with 21 additions and 76 deletions

View File

@ -100,7 +100,7 @@ export class App {
return null;
}
@Method()
@Listen('document:backbutton')
hardwareBackButtonPressed() {
// check if menu exists and is open
return checkIfMenuIsOpen().then((done: boolean) => {
@ -134,12 +134,12 @@ export class App {
});
}
@Method()
@Listen('document:paused')
appResume(): void {
return null;
}
@Method()
@Listen('document:resume')
appPaused(): void {
return null;
}

View File

@ -12,12 +12,6 @@
## Methods
#### appPaused()
#### appResume()
#### getNavByIdOrName()
@ -29,9 +23,6 @@ Returns an array of top level Navs
#### getTopNavs()
#### hardwareBackButtonPressed()
#### isEnabled()
Returns whether the application is enabled or not

View File

@ -1,84 +1,35 @@
import { Component, Listen, Method} from '@stencil/core';
let isReady = false;
let readyQueue: Function[] = [];
@Component({
tag: 'ion-cordova-platform',
styleUrls: {
ios: 'cordova-platform.ios.scss',
md: 'cordova-platform.md.scss'
},
host: {
theme: 'cordova-platform'
}
})
export class CordovaPlatform {
private readyPromise: Promise<void>;
private readyResolve: Function;
constructor() {
this.readyPromise = new Promise(resolve => this.readyResolve = resolve);
}
@Method()
ready() {
return readyImpl();
ready(): Promise<void> {
return this.readyPromise;
}
@Listen('document:deviceready')
deviceReadyHandler() {
isReady = true;
processReadyQueue();
this.readyResolve();
}
@Method()
@Listen('body:exitApp')
exitCordovaApp() {
// this is lifted directly from Ionic 3
((window.navigator as any).app as any).exitApp();
}
componentDidLoad() {
readyImpl().then(() => {
// okay cool, we've received the ready event, we need to listen for the other events now
document.addEventListener('backbutton', handleBackButton);
document.addEventListener('resume', handleResume);
document.addEventListener('pause', handlePause);
});
const app = (window.navigator as any).app;
if (app && app.exitApp) {
app.exitApp();
}
}
}
export function handleBackButton() {
return getHydratedApp().then((app: HTMLIonAppElement) => {
return app.hardwareBackButtonPressed();
});
}
export function handleResume() {
return getHydratedApp().then((app: HTMLIonAppElement) => {
return app.appResume();
});
}
export function handlePause() {
return getHydratedApp().then((app: HTMLIonAppElement) => {
return app.appPaused();
});
}
export function getHydratedApp() {
const app = document.querySelector('ion-app');
return (app as any).componentOnReady();
}
export function readyImpl(): Promise<any> {
if (isReady) {
return Promise.resolve();
}
return new Promise((resolve) => {
readyQueue.push(resolve);
});
}
export function processReadyQueue() {
for (const resolve of readyQueue) {
resolve();
}
readyQueue = [];
}

View File

@ -7,6 +7,9 @@
## Methods
#### exitCordovaApp()
#### ready()