Skip to content

Commit

Permalink
feat(platform): add a readySource as ready resolved value
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Apr 22, 2016
1 parent 95ee2bb commit f68ac8a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
4 changes: 2 additions & 2 deletions ionic/components/modal/test/basic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class E2EPage {
console.log('android', platform.is('android'));
console.log('windows phone', platform.is('windows'));

platform.ready().then(() => {
console.log('platform.ready');
platform.ready().then((readySource) => {
console.log('platform.ready, readySource:', readySource);
});

this.platforms = platform.platforms();
Expand Down
47 changes: 29 additions & 18 deletions ionic/platform/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,48 +175,59 @@ export class Platform {
* Returns a promise when the platform is ready and native functionality
* can be called. If the app is running from within a web browser, then
* the promise will resolve when the DOM is ready. When the app is running
* from an application engine such as Cordova, then the promise
* will resolve when Cordova triggers the `deviceready` event.
* from an application engine such as Cordova, then the promise will
* resolve when Cordova triggers the `deviceready` event.
*
* The resolved value is the `readySource`, which states which platform
* ready was used. For example, when Cordova is ready, the resolved ready
* source is `cordova`. The default ready source value will be `dom`. The
* `readySource` is useful if different logic should run depending on the
* platform the app is running from. For example, only Cordova can execute
* the status bar plugin, so the web should not run status bar plugin logic.
*
* ```
* import {Platform} from 'ionic-angular';
* import {App, Platform} from 'ionic-angular';
*
* @Page({...})
* export MyPage {
* @App({...})
* export MyApp {
* constructor(platform: Platform) {
* platform.ready().then(() => {
* console.log('Platform ready');
* // The platform is now ready, execute any native code you want
* platform.ready().then((readySource) => {
* console.log('Platform ready from', readySource);
* // Platform now ready, execute any required native code
* });
* }
* }
* ```
* @returns {promise}
*/
ready(): Promise<any> {
// this is the default if it's not replaced by the engine
// if there was no custom ready method from the engine
// then use the default DOM ready
ready(): Promise<string> {
return this._readyPromise;
}

/**
* @private
* This should be triggered by the engine when the platform is
* ready. If there was no custom prepareReady method from the engine,
* such as Cordova or Electron, then it uses the default DOM ready.
*/
triggerReady() {
triggerReady(readySource: string) {
this._zone.run(() => {
this._readyResolve();
this._readyResolve(readySource);
});
}

/**
* @private
* This is the default prepareReady if it's not replaced by an engine,
* such as Cordova or Electron. If there was no custom prepareReady
* method from an engine then it uses the method below, which triggers
* the platform ready on the DOM ready event, and the default resolved
* value is `dom`.
*/
prepareReady() {
// this is the default prepareReady if it's not replaced by the engine
// if there was no custom ready method from the engine
// then use the default DOM ready
ready(this.triggerReady.bind(this));
ready(() => {
this.triggerReady('dom');
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ionic/platform/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Platform.register({
};

// cordova has fully loaded and we've added listeners
p.triggerReady();
p.triggerReady('cordova');
});
});
};
Expand Down

0 comments on commit f68ac8a

Please sign in to comment.