mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-23 22:17:40 +08:00
refactor(cordova): using promise instead of queue
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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();
|
||||
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 = [];
|
||||
}
|
||||
|
@ -7,6 +7,9 @@
|
||||
|
||||
## Methods
|
||||
|
||||
#### exitCordovaApp()
|
||||
|
||||
|
||||
#### ready()
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user