Skip to content

Commit f68ac8a

Browse files
committed
feat(platform): add a readySource as ready resolved value
1 parent 95ee2bb commit f68ac8a

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

ionic/components/modal/test/basic/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class E2EPage {
2525
console.log('android', platform.is('android'));
2626
console.log('windows phone', platform.is('windows'));
2727

28-
platform.ready().then(() => {
29-
console.log('platform.ready');
28+
platform.ready().then((readySource) => {
29+
console.log('platform.ready, readySource:', readySource);
3030
});
3131

3232
this.platforms = platform.platforms();

ionic/platform/platform.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -175,48 +175,59 @@ export class Platform {
175175
* Returns a promise when the platform is ready and native functionality
176176
* can be called. If the app is running from within a web browser, then
177177
* the promise will resolve when the DOM is ready. When the app is running
178-
* from an application engine such as Cordova, then the promise
179-
* will resolve when Cordova triggers the `deviceready` event.
178+
* from an application engine such as Cordova, then the promise will
179+
* resolve when Cordova triggers the `deviceready` event.
180+
*
181+
* The resolved value is the `readySource`, which states which platform
182+
* ready was used. For example, when Cordova is ready, the resolved ready
183+
* source is `cordova`. The default ready source value will be `dom`. The
184+
* `readySource` is useful if different logic should run depending on the
185+
* platform the app is running from. For example, only Cordova can execute
186+
* the status bar plugin, so the web should not run status bar plugin logic.
180187
*
181188
* ```
182-
* import {Platform} from 'ionic-angular';
189+
* import {App, Platform} from 'ionic-angular';
183190
*
184-
* @Page({...})
185-
* export MyPage {
191+
* @App({...})
192+
* export MyApp {
186193
* constructor(platform: Platform) {
187-
* platform.ready().then(() => {
188-
* console.log('Platform ready');
189-
* // The platform is now ready, execute any native code you want
194+
* platform.ready().then((readySource) => {
195+
* console.log('Platform ready from', readySource);
196+
* // Platform now ready, execute any required native code
190197
* });
191198
* }
192199
* }
193200
* ```
194201
* @returns {promise}
195202
*/
196-
ready(): Promise<any> {
197-
// this is the default if it's not replaced by the engine
198-
// if there was no custom ready method from the engine
199-
// then use the default DOM ready
203+
ready(): Promise<string> {
200204
return this._readyPromise;
201205
}
202206

203207
/**
204208
* @private
209+
* This should be triggered by the engine when the platform is
210+
* ready. If there was no custom prepareReady method from the engine,
211+
* such as Cordova or Electron, then it uses the default DOM ready.
205212
*/
206-
triggerReady() {
213+
triggerReady(readySource: string) {
207214
this._zone.run(() => {
208-
this._readyResolve();
215+
this._readyResolve(readySource);
209216
});
210217
}
211218

212219
/**
213220
* @private
221+
* This is the default prepareReady if it's not replaced by an engine,
222+
* such as Cordova or Electron. If there was no custom prepareReady
223+
* method from an engine then it uses the method below, which triggers
224+
* the platform ready on the DOM ready event, and the default resolved
225+
* value is `dom`.
214226
*/
215227
prepareReady() {
216-
// this is the default prepareReady if it's not replaced by the engine
217-
// if there was no custom ready method from the engine
218-
// then use the default DOM ready
219-
ready(this.triggerReady.bind(this));
228+
ready(() => {
229+
this.triggerReady('dom');
230+
});
220231
}
221232

222233
/**

ionic/platform/registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ Platform.register({
192192
};
193193

194194
// cordova has fully loaded and we've added listeners
195-
p.triggerReady();
195+
p.triggerReady('cordova');
196196
});
197197
});
198198
};

0 commit comments

Comments
 (0)