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

View File

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

View File

@ -1,84 +1,35 @@
import { Component, Listen, Method} from '@stencil/core'; import { Component, Listen, Method} from '@stencil/core';
let isReady = false;
let readyQueue: Function[] = [];
@Component({ @Component({
tag: 'ion-cordova-platform', tag: 'ion-cordova-platform',
styleUrls: {
ios: 'cordova-platform.ios.scss',
md: 'cordova-platform.md.scss'
},
host: {
theme: 'cordova-platform'
}
}) })
export class CordovaPlatform { export class CordovaPlatform {
private readyPromise: Promise<void>;
private readyResolve: Function;
constructor() {
this.readyPromise = new Promise(resolve => this.readyResolve = resolve);
}
@Method() @Method()
ready() { ready(): Promise<void> {
return readyImpl(); return this.readyPromise;
} }
@Listen('document:deviceready') @Listen('document:deviceready')
deviceReadyHandler() { deviceReadyHandler() {
isReady = true; this.readyResolve();
processReadyQueue();
} }
@Method()
@Listen('body:exitApp') @Listen('body:exitApp')
exitCordovaApp() { exitCordovaApp() {
// this is lifted directly from Ionic 3 // this is lifted directly from Ionic 3
((window.navigator as any).app as any).exitApp(); const app = (window.navigator as any).app;
if (app && app.exitApp) {
app.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);
});
} }
} }
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 ## Methods
#### exitCordovaApp()
#### ready() #### ready()