refactor: prefer using app.whenReady() (#21972)

* docs: add references to app.whenReady() in isReady

* refactor: prefer app.whenReady()

In the docs, specs, and lib, replace instances of `app.once('ready')`
(seen occasionally) and `app.on('ready')` (extremely common) with
`app.whenReady()`.

It's better to encourage users to use whenReady():
1. it handles the edge case of registering for 'ready' after it's fired
2. it avoids the minor wart of leaving an active listener alive for
an event that wll never fire again
This commit is contained in:
Charles Kerr 2020-02-03 16:43:22 -06:00 committed by GitHub
parent 7a3862a1c6
commit c83f836faf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
78 changed files with 111 additions and 109 deletions

View file

@ -18,7 +18,7 @@ method, i.e.
```javascript
const { app, globalShortcut } = require('electron')
app.on('ready', () => {
app.whenReady().then(() => {
// Register a 'CommandOrControl+Y' shortcut listener.
globalShortcut.register('CommandOrControl+Y', () => {
// Do stuff when Y and either Command/Control is pressed.

View file

@ -34,10 +34,11 @@ Returns:
* `launchInfo` unknown _macOS_
Emitted when Electron has finished initializing. On macOS, `launchInfo` holds
the `userInfo` of the `NSUserNotification` that was used to open the application,
if it was launched from Notification Center. You can call `app.isReady()` to
check if this event has already fired.
Emitted once, when Electron has finished initializing. On macOS, `launchInfo`
holds the `userInfo` of the `NSUserNotification` that was used to open the
application, if it was launched from Notification Center. You can also call
`app.isReady()` to check if this event has already fired and `app.whenReady()`
to get a Promise that is fulfilled when Electron is initialized.
### Event: 'window-all-closed'
@ -545,6 +546,7 @@ app.exit(0)
### `app.isReady()`
Returns `Boolean` - `true` if Electron has finished initializing, `false` otherwise.
See also `app.whenReady()`.
### `app.whenReady()`
@ -935,7 +937,7 @@ if (!gotTheLock) {
})
// Create myWindow, load the rest of the app, etc...
app.on('ready', () => {
app.whenReady().then(() => {
})
}
```

View file

@ -11,7 +11,7 @@ const { app } = require('electron')
app.commandLine.appendSwitch('remote-debugging-port', '8315')
app.commandLine.appendSwitch('host-rules', 'MAP * 127.0.0.1')
app.on('ready', () => {
app.whenReady().then(() => {
// Your code here
})
```

View file

@ -13,7 +13,7 @@ module is emitted.
```javascript
const { app, contentTracing } = require('electron')
app.on('ready', () => {
app.whenReady().then(() => {
(async () => {
await contentTracing.startRecording({
include_categories: ['*']

View file

@ -15,7 +15,7 @@ event of the app module is emitted.
```javascript
const { app, globalShortcut } = require('electron')
app.on('ready', () => {
app.whenReady().then(() => {
// Register a 'CommandOrControl+X' shortcut listener.
const ret = globalShortcut.register('CommandOrControl+X', () => {
console.log('CommandOrControl+X is pressed')

View file

@ -7,7 +7,7 @@ Process: [Main](../glossary.md#main-process)
```javascript
const { netLog } = require('electron')
app.on('ready', async () => {
app.whenReady().then(async () => {
await netLog.startLogging('/path/to/net-log')
// After some network events
const path = await netLog.stopLogging()

View file

@ -28,7 +28,7 @@ Example usage:
```javascript
const { app } = require('electron')
app.on('ready', () => {
app.whenReady().then(() => {
const { net } = require('electron')
const request = net.request('https://github.com')
request.on('response', (response) => {

View file

@ -13,7 +13,7 @@ For example:
```javascript
const { app, powerMonitor } = require('electron')
app.on('ready', () => {
app.whenReady().then(() => {
powerMonitor.on('suspend', () => {
console.log('The system is going to sleep')
})

View file

@ -20,7 +20,7 @@ An example of implementing a protocol that has the same effect as the
const { app, protocol } = require('electron')
const path = require('path')
app.on('ready', () => {
app.whenReady().then(() => {
protocol.registerFileProtocol('atom', (request, callback) => {
const url = request.url.substr(7)
callback({ path: path.normalize(`${__dirname}/${url}`) })
@ -47,7 +47,7 @@ to register it to that session explicitly.
const { session, app, protocol } = require('electron')
const path = require('path')
app.on('ready', () => {
app.whenReady().then(() => {
const partition = 'persist:example'
const ses = session.fromPartition(partition)

View file

@ -11,7 +11,7 @@ An example of implementing a protocol that has the same effect as the
const { app, protocol } = require('electron')
const path = require('path')
app.on('ready', () => {
app.whenReady().then(() => {
protocol.registerFileProtocol('atom', (request, callback) => {
const url = request.url.substr(7)
callback({ path: path.normalize(`${__dirname}/${url}`) })
@ -34,7 +34,7 @@ To have your custom protocol work in combination with a custom session, you need
const { session, app, protocol } = require('electron')
const path = require('path')
app.on('ready', () => {
app.whenReady().then(() => {
const partition = 'persist:example'
const ses = session.fromPartition(partition)

View file

@ -163,7 +163,7 @@ project/
```js
// main process: main/index.js
const { app } = require('electron')
app.on('ready', () => { /* ... */ })
app.whenReady().then(() => { /* ... */ })
```
```js

View file

@ -39,7 +39,7 @@ To create a sandboxed window, pass `sandbox: true` to `webPreferences`:
```js
let win
app.on('ready', () => {
app.whenReady().then(() => {
win = new BrowserWindow({
webPreferences: {
sandbox: true
@ -59,7 +59,7 @@ and returns a proxy to this via `window.open`).
```js
let win
app.enableSandbox()
app.on('ready', () => {
app.whenReady().then(() => {
// no need to pass `sandbox: true` since `app.enableSandbox()` was called.
win = new BrowserWindow()
win.loadURL('http://google.com')
@ -73,7 +73,7 @@ Here's an example:
```js
let win
app.on('ready', () => {
app.whenReady().then(() => {
win = new BrowserWindow({
webPreferences: {
sandbox: true,

View file

@ -18,7 +18,7 @@ An example of creating a window that fills the whole screen:
const { app, BrowserWindow, screen } = require('electron')
let win
app.on('ready', () => {
app.whenReady().then(() => {
const { width, height } = screen.getPrimaryDisplay().workAreaSize
win = new BrowserWindow({ width, height })
win.loadURL('https://github.com')
@ -32,7 +32,7 @@ const { app, BrowserWindow, screen } = require('electron')
let win
app.on('ready', () => {
app.whenReady().then(() => {
let displays = screen.getAllDisplays()
let externalDisplay = displays.find((display) => {
return display.bounds.x !== 0 || display.bounds.y !== 0

View file

@ -579,7 +579,7 @@ A [`Protocol`](protocol.md) object for this session.
const { app, session } = require('electron')
const path = require('path')
app.on('ready', () => {
app.whenReady().then(() => {
const protocol = session.fromPartition('some-partition').protocol
protocol.registerFileProtocol('atom', (request, callback) => {
let url = request.url.substr(7)
@ -597,7 +597,7 @@ A [`NetLog`](net-log.md) object for this session.
```javascript
const { app, session } = require('electron')
app.on('ready', async () => {
app.whenReady().then(async () => {
const netLog = session.fromPartition('some-partition').netLog
netLog.startLogging('/path/to/net-log')
// After some network events

View file

@ -22,7 +22,7 @@ The main process script is like a normal Node.js script:
const { app, BrowserWindow } = require('electron')
let win = null
app.on('ready', () => {
app.whenReady().then(() => {
win = new BrowserWindow({ width: 800, height: 600 })
win.loadURL('https://github.com')
})
@ -56,7 +56,7 @@ const { app, BrowserWindow } = require('electron')
let win
app.on('ready', () => {
app.whenReady().then(() => {
win = new BrowserWindow()
win.loadURL('https://github.com')
})
@ -71,7 +71,7 @@ const { app, BrowserWindow } = electron
let win
app.on('ready', () => {
app.whenReady().then(() => {
win = new BrowserWindow()
win.loadURL('https://github.com')
})
@ -85,7 +85,7 @@ const app = electron.app
const BrowserWindow = electron.BrowserWindow
let win
app.on('ready', () => {
app.whenReady().then(() => {
win = new BrowserWindow()
win.loadURL('https://github.com')
})

View file

@ -166,7 +166,7 @@ const touchBar = new TouchBar({
let window
app.once('ready', () => {
app.whenReady().then(() => {
window = new BrowserWindow({
frame: false,
titleBarStyle: 'hiddenInset',

View file

@ -10,7 +10,7 @@ Process: [Main](../glossary.md#main-process)
const { app, Menu, Tray } = require('electron')
let tray = null
app.on('ready', () => {
app.whenReady().then(() => {
tray = new Tray('/path/to/my/icon')
const contextMenu = Menu.buildFromTemplate([
{ label: 'Item1', type: 'radio' },
@ -38,7 +38,7 @@ __Platform limitations:__
const { app, Menu, Tray } = require('electron')
let appIcon = null
app.on('ready', () => {
app.whenReady().then(() => {
appIcon = new Tray('/path/to/my/icon')
const contextMenu = Menu.buildFromTemplate([
{ label: 'Item1', type: 'radio' },

View file

@ -624,7 +624,7 @@ const { app, BrowserWindow } = require('electron')
let win = null
app.commandLine.appendSwitch('enable-experimental-web-platform-features')
app.on('ready', () => {
app.whenReady().then(() => {
win = new BrowserWindow({ width: 800, height: 600 })
win.webContents.on('select-bluetooth-device', (event, deviceList, callback) => {
event.preventDefault()
@ -1444,7 +1444,7 @@ const { app, BrowserWindow } = require('electron')
let win = null
let devtools = null
app.once('ready', () => {
app.whenReady().then(() => {
win = new BrowserWindow()
devtools = new BrowserWindow()
win.loadURL('https://github.com')
@ -1533,7 +1533,7 @@ An example of sending messages from the main process to the renderer process:
const { app, BrowserWindow } = require('electron')
let win = null
app.on('ready', () => {
app.whenReady().then(() => {
win = new BrowserWindow({ width: 800, height: 600 })
win.loadURL(`file://${__dirname}/index.html`)
win.webContents.on('did-finish-load', () => {