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

View file

@ -34,10 +34,11 @@ Returns:
* `launchInfo` unknown _macOS_ * `launchInfo` unknown _macOS_
Emitted when Electron has finished initializing. On macOS, `launchInfo` holds Emitted once, when Electron has finished initializing. On macOS, `launchInfo`
the `userInfo` of the `NSUserNotification` that was used to open the application, holds the `userInfo` of the `NSUserNotification` that was used to open the
if it was launched from Notification Center. You can call `app.isReady()` to application, if it was launched from Notification Center. You can also call
check if this event has already fired. `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' ### Event: 'window-all-closed'
@ -545,6 +546,7 @@ app.exit(0)
### `app.isReady()` ### `app.isReady()`
Returns `Boolean` - `true` if Electron has finished initializing, `false` otherwise. Returns `Boolean` - `true` if Electron has finished initializing, `false` otherwise.
See also `app.whenReady()`.
### `app.whenReady()` ### `app.whenReady()`
@ -935,7 +937,7 @@ if (!gotTheLock) {
}) })
// Create myWindow, load the rest of the app, etc... // 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('remote-debugging-port', '8315')
app.commandLine.appendSwitch('host-rules', 'MAP * 127.0.0.1') app.commandLine.appendSwitch('host-rules', 'MAP * 127.0.0.1')
app.on('ready', () => { app.whenReady().then(() => {
// Your code here // Your code here
}) })
``` ```

View file

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

View file

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

View file

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

View file

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

View file

@ -13,7 +13,7 @@ For example:
```javascript ```javascript
const { app, powerMonitor } = require('electron') const { app, powerMonitor } = require('electron')
app.on('ready', () => { app.whenReady().then(() => {
powerMonitor.on('suspend', () => { powerMonitor.on('suspend', () => {
console.log('The system is going to sleep') 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 { app, protocol } = require('electron')
const path = require('path') const path = require('path')
app.on('ready', () => { app.whenReady().then(() => {
protocol.registerFileProtocol('atom', (request, callback) => { protocol.registerFileProtocol('atom', (request, callback) => {
const url = request.url.substr(7) const url = request.url.substr(7)
callback({ path: path.normalize(`${__dirname}/${url}`) }) callback({ path: path.normalize(`${__dirname}/${url}`) })
@ -47,7 +47,7 @@ to register it to that session explicitly.
const { session, app, protocol } = require('electron') const { session, app, protocol } = require('electron')
const path = require('path') const path = require('path')
app.on('ready', () => { app.whenReady().then(() => {
const partition = 'persist:example' const partition = 'persist:example'
const ses = session.fromPartition(partition) 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 { app, protocol } = require('electron')
const path = require('path') const path = require('path')
app.on('ready', () => { app.whenReady().then(() => {
protocol.registerFileProtocol('atom', (request, callback) => { protocol.registerFileProtocol('atom', (request, callback) => {
const url = request.url.substr(7) const url = request.url.substr(7)
callback({ path: path.normalize(`${__dirname}/${url}`) }) 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 { session, app, protocol } = require('electron')
const path = require('path') const path = require('path')
app.on('ready', () => { app.whenReady().then(() => {
const partition = 'persist:example' const partition = 'persist:example'
const ses = session.fromPartition(partition) const ses = session.fromPartition(partition)

View file

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

View file

@ -39,7 +39,7 @@ To create a sandboxed window, pass `sandbox: true` to `webPreferences`:
```js ```js
let win let win
app.on('ready', () => { app.whenReady().then(() => {
win = new BrowserWindow({ win = new BrowserWindow({
webPreferences: { webPreferences: {
sandbox: true sandbox: true
@ -59,7 +59,7 @@ and returns a proxy to this via `window.open`).
```js ```js
let win let win
app.enableSandbox() app.enableSandbox()
app.on('ready', () => { app.whenReady().then(() => {
// no need to pass `sandbox: true` since `app.enableSandbox()` was called. // no need to pass `sandbox: true` since `app.enableSandbox()` was called.
win = new BrowserWindow() win = new BrowserWindow()
win.loadURL('http://google.com') win.loadURL('http://google.com')
@ -73,7 +73,7 @@ Here's an example:
```js ```js
let win let win
app.on('ready', () => { app.whenReady().then(() => {
win = new BrowserWindow({ win = new BrowserWindow({
webPreferences: { webPreferences: {
sandbox: true, 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') const { app, BrowserWindow, screen } = require('electron')
let win let win
app.on('ready', () => { app.whenReady().then(() => {
const { width, height } = screen.getPrimaryDisplay().workAreaSize const { width, height } = screen.getPrimaryDisplay().workAreaSize
win = new BrowserWindow({ width, height }) win = new BrowserWindow({ width, height })
win.loadURL('https://github.com') win.loadURL('https://github.com')
@ -32,7 +32,7 @@ const { app, BrowserWindow, screen } = require('electron')
let win let win
app.on('ready', () => { app.whenReady().then(() => {
let displays = screen.getAllDisplays() let displays = screen.getAllDisplays()
let externalDisplay = displays.find((display) => { let externalDisplay = displays.find((display) => {
return display.bounds.x !== 0 || display.bounds.y !== 0 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 { app, session } = require('electron')
const path = require('path') const path = require('path')
app.on('ready', () => { app.whenReady().then(() => {
const protocol = session.fromPartition('some-partition').protocol const protocol = session.fromPartition('some-partition').protocol
protocol.registerFileProtocol('atom', (request, callback) => { protocol.registerFileProtocol('atom', (request, callback) => {
let url = request.url.substr(7) let url = request.url.substr(7)
@ -597,7 +597,7 @@ A [`NetLog`](net-log.md) object for this session.
```javascript ```javascript
const { app, session } = require('electron') const { app, session } = require('electron')
app.on('ready', async () => { app.whenReady().then(async () => {
const netLog = session.fromPartition('some-partition').netLog const netLog = session.fromPartition('some-partition').netLog
netLog.startLogging('/path/to/net-log') netLog.startLogging('/path/to/net-log')
// After some network events // 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') const { app, BrowserWindow } = require('electron')
let win = null let win = null
app.on('ready', () => { app.whenReady().then(() => {
win = new BrowserWindow({ width: 800, height: 600 }) win = new BrowserWindow({ width: 800, height: 600 })
win.loadURL('https://github.com') win.loadURL('https://github.com')
}) })
@ -56,7 +56,7 @@ const { app, BrowserWindow } = require('electron')
let win let win
app.on('ready', () => { app.whenReady().then(() => {
win = new BrowserWindow() win = new BrowserWindow()
win.loadURL('https://github.com') win.loadURL('https://github.com')
}) })
@ -71,7 +71,7 @@ const { app, BrowserWindow } = electron
let win let win
app.on('ready', () => { app.whenReady().then(() => {
win = new BrowserWindow() win = new BrowserWindow()
win.loadURL('https://github.com') win.loadURL('https://github.com')
}) })
@ -85,7 +85,7 @@ const app = electron.app
const BrowserWindow = electron.BrowserWindow const BrowserWindow = electron.BrowserWindow
let win let win
app.on('ready', () => { app.whenReady().then(() => {
win = new BrowserWindow() win = new BrowserWindow()
win.loadURL('https://github.com') win.loadURL('https://github.com')
}) })

View file

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

View file

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

View file

@ -624,7 +624,7 @@ const { app, BrowserWindow } = require('electron')
let win = null let win = null
app.commandLine.appendSwitch('enable-experimental-web-platform-features') app.commandLine.appendSwitch('enable-experimental-web-platform-features')
app.on('ready', () => { app.whenReady().then(() => {
win = new BrowserWindow({ width: 800, height: 600 }) win = new BrowserWindow({ width: 800, height: 600 })
win.webContents.on('select-bluetooth-device', (event, deviceList, callback) => { win.webContents.on('select-bluetooth-device', (event, deviceList, callback) => {
event.preventDefault() event.preventDefault()
@ -1444,7 +1444,7 @@ const { app, BrowserWindow } = require('electron')
let win = null let win = null
let devtools = null let devtools = null
app.once('ready', () => { app.whenReady().then(() => {
win = new BrowserWindow() win = new BrowserWindow()
devtools = new BrowserWindow() devtools = new BrowserWindow()
win.loadURL('https://github.com') 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') const { app, BrowserWindow } = require('electron')
let win = null let win = null
app.on('ready', () => { app.whenReady().then(() => {
win = new BrowserWindow({ width: 800, height: 600 }) win = new BrowserWindow({ width: 800, height: 600 })
win.loadURL(`file://${__dirname}/index.html`) win.loadURL(`file://${__dirname}/index.html`)
win.webContents.on('did-finish-load', () => { win.webContents.on('did-finish-load', () => {

View file

@ -79,7 +79,7 @@ code from this:
```javascript ```javascript
const { app, Tray } = require('electron') const { app, Tray } = require('electron')
app.on('ready', () => { app.whenReady().then(() => {
const tray = new Tray('/path/to/icon.png') const tray = new Tray('/path/to/icon.png')
tray.setTitle('hello world') tray.setTitle('hello world')
}) })
@ -90,7 +90,7 @@ to this:
```javascript ```javascript
const { app, Tray } = require('electron') const { app, Tray } = require('electron')
let tray = null let tray = null
app.on('ready', () => { app.whenReady().then(() => {
tray = new Tray('/path/to/icon.png') tray = new Tray('/path/to/icon.png')
tray.setTitle('hello world') tray.setTitle('hello world')
}) })

View file

@ -20,7 +20,7 @@ function createWindow () {
}) })
} }
app.on('ready', () => { app.whenReady().then(() => {
createWindow() createWindow()
}) })

View file

@ -20,7 +20,7 @@ function createWindow () {
}) })
} }
app.on('ready', () => { app.whenReady().then(() => {
createWindow() createWindow()
}) })

View file

@ -20,6 +20,6 @@ function createWindow () {
}) })
} }
app.on('ready', () => { app.whenReady().then(() => {
createWindow() createWindow()
}) })

View file

@ -316,7 +316,7 @@ function createWindow () {
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.on('ready', () => { app.whenReady().then(() => {
createWindow() createWindow()
const menu = Menu.buildFromTemplate(template) const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu) Menu.setApplicationMenu(menu)

View file

@ -42,7 +42,7 @@ function createWindow () {
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.on('ready', createWindow) app.whenReady().then(createWindow)
// Quit when all windows are closed. // Quit when all windows are closed.
app.on('window-all-closed', function () { app.on('window-all-closed', function () {

View file

@ -33,7 +33,7 @@ function createWindow () {
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.on('ready', createWindow) app.whenReady().then(createWindow)
// Quit when all windows are closed. // Quit when all windows are closed.
app.on('window-all-closed', function () { app.on('window-all-closed', function () {

View file

@ -33,7 +33,7 @@ function createWindow () {
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.on('ready', createWindow) app.whenReady().then(createWindow)
// Quit when all windows are closed. // Quit when all windows are closed.
app.on('window-all-closed', function () { app.on('window-all-closed', function () {

View file

@ -33,7 +33,7 @@ function createWindow () {
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.on('ready', createWindow) app.whenReady().then(createWindow)
// Quit when all windows are closed. // Quit when all windows are closed.
app.on('window-all-closed', function () { app.on('window-all-closed', function () {

View file

@ -33,7 +33,7 @@ function createWindow () {
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.on('ready', createWindow) app.whenReady().then(createWindow)
// Quit when all windows are closed. // Quit when all windows are closed.
app.on('window-all-closed', function () { app.on('window-all-closed', function () {

View file

@ -32,7 +32,7 @@ function createWindow () {
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.on('ready', createWindow) app.whenReady().then(createWindow)
// Quit when all windows are closed. // Quit when all windows are closed.
app.on('window-all-closed', function () { app.on('window-all-closed', function () {

View file

@ -20,6 +20,6 @@ function createWindow () {
}) })
} }
app.on('ready', () => { app.whenReady().then(() => {
createWindow() createWindow()
}) })

View file

@ -33,7 +33,7 @@ function createWindow () {
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.on('ready', createWindow) app.whenReady().then(createWindow)
// Quit when all windows are closed. // Quit when all windows are closed.
app.on('window-all-closed', function () { app.on('window-all-closed', function () {

View file

@ -20,6 +20,6 @@ function createWindow () {
}) })
} }
app.on('ready', () => { app.whenReady().then(() => {
createWindow() createWindow()
}) })

View file

@ -20,6 +20,6 @@ function createWindow () {
}) })
} }
app.on('ready', () => { app.whenReady().then(() => {
createWindow() createWindow()
}) })

View file

@ -33,7 +33,7 @@ function createWindow () {
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.on('ready', createWindow) app.whenReady().then(createWindow)
// Quit when all windows are closed. // Quit when all windows are closed.
app.on('window-all-closed', function () { app.on('window-all-closed', function () {

View file

@ -20,6 +20,6 @@ function createWindow () {
}) })
} }
app.on('ready', () => { app.whenReady().then(() => {
createWindow() createWindow()
}) })

View file

@ -34,7 +34,7 @@ function createWindow () {
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.on('ready', createWindow) app.whenReady().then(createWindow)
// Quit when all windows are closed. // Quit when all windows are closed.
app.on('window-all-closed', function () { app.on('window-all-closed', function () {

View file

@ -7,7 +7,7 @@ const { app, BrowserWindow } = require('electron')
let mainWindow = null let mainWindow = null
app.on('ready', () => { app.whenReady().then(() => {
// We cannot require the screen module until the app is ready. // We cannot require the screen module until the app is ready.
const { screen } = require('electron') const { screen } = require('electron')

View file

@ -20,6 +20,6 @@ function createWindow () {
}) })
} }
app.on('ready', () => { app.whenReady().then(() => {
createWindow() createWindow()
}) })

View file

@ -20,6 +20,6 @@ function createWindow () {
}) })
} }
app.on('ready', () => { app.whenReady().then(() => {
createWindow() createWindow()
}) })

View file

@ -34,7 +34,7 @@ function createWindow () {
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.on('ready', createWindow) app.whenReady().then(createWindow)
// Quit when all windows are closed. // Quit when all windows are closed.
app.on('window-all-closed', function () { app.on('window-all-closed', function () {

View file

@ -20,6 +20,6 @@ function createWindow () {
}) })
} }
app.on('ready', () => { app.whenReady().then(() => {
createWindow() createWindow()
}) })

View file

@ -20,6 +20,6 @@ function createWindow () {
}) })
} }
app.on('ready', () => { app.whenReady().then(() => {
createWindow() createWindow()
}) })

View file

@ -33,7 +33,7 @@ function createWindow () {
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.on('ready', createWindow) app.whenReady().then(createWindow)
// Quit when all windows are closed. // Quit when all windows are closed.
app.on('window-all-closed', function () { app.on('window-all-closed', function () {

View file

@ -33,7 +33,7 @@ function createWindow () {
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.on('ready', createWindow) app.whenReady().then(createWindow)
// Quit when all windows are closed. // Quit when all windows are closed.
app.on('window-all-closed', function () { app.on('window-all-closed', function () {

View file

@ -33,7 +33,7 @@ function createWindow () {
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.on('ready', createWindow) app.whenReady().then(createWindow)
// Quit when all windows are closed. // Quit when all windows are closed.
app.on('window-all-closed', function () { app.on('window-all-closed', function () {

View file

@ -33,7 +33,7 @@ function createWindow () {
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.on('ready', createWindow) app.whenReady().then(createWindow)
// Quit when all windows are closed. // Quit when all windows are closed.
app.on('window-all-closed', function () { app.on('window-all-closed', function () {

View file

@ -121,7 +121,7 @@ function createWindow () {
win.loadFile('index.html') win.loadFile('index.html')
} }
app.on('ready', createWindow) app.whenReady().then(createWindow)
``` ```
The `main.js` should create windows and handle all the system events your The `main.js` should create windows and handle all the system events your
@ -152,7 +152,7 @@ function createWindow () {
// This method will be called when Electron has finished // This method will be called when Electron has finished
// initialization and is ready to create browser windows. // initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs. // Some APIs can only be used after this event occurs.
app.on('ready', createWindow) app.whenReady().then(createWindow)
// Quit when all windows are closed. // Quit when all windows are closed.
app.on('window-all-closed', () => { app.on('window-all-closed', () => {

View file

@ -35,7 +35,7 @@ the application does not have keyboard focus.
```js ```js
const { app, globalShortcut } = require('electron') const { app, globalShortcut } = require('electron')
app.on('ready', () => { app.whenReady().then(() => {
globalShortcut.register('CommandOrControl+X', () => { globalShortcut.register('CommandOrControl+X', () => {
console.log('CommandOrControl+X is pressed') console.log('CommandOrControl+X is pressed')
}) })

View file

@ -41,7 +41,7 @@ app.disableHardwareAcceleration()
let win let win
app.once('ready', () => { app.whenReady().then(() => {
win = new BrowserWindow({ win = new BrowserWindow({
webPreferences: { webPreferences: {
offscreen: true offscreen: true

View file

@ -15,7 +15,7 @@ const { app, BrowserWindow } = require('electron')
let onlineStatusWindow let onlineStatusWindow
app.on('ready', () => { app.whenReady().then(() => {
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false }) onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false })
onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`) onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`)
}) })
@ -53,7 +53,7 @@ _main.js_
const { app, BrowserWindow, ipcMain } = require('electron') const { app, BrowserWindow, ipcMain } = require('electron')
let onlineStatusWindow let onlineStatusWindow
app.on('ready', () => { app.whenReady().then(() => {
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false, webPreferences: { nodeIntegration: true } }) onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false, webPreferences: { nodeIntegration: true } })
onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`) onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`)
}) })

View file

@ -79,7 +79,7 @@ app.commandLine.appendSwitch('widevine-cdm-path', '/path/to/widevine_library')
app.commandLine.appendSwitch('widevine-cdm-version', '1.4.8.866') app.commandLine.appendSwitch('widevine-cdm-version', '1.4.8.866')
let win = null let win = null
app.on('ready', () => { app.whenReady().then(() => {
win = new BrowserWindow() win = new BrowserWindow()
win.show() win.show()
}) })

View file

@ -41,7 +41,7 @@ app.commandLine.appendSwitch('ppapi-flash-path', path.join(__dirname, pluginName
// Optional: Specify flash version, for example, v17.0.0.169 // Optional: Specify flash version, for example, v17.0.0.169
app.commandLine.appendSwitch('ppapi-flash-version', '17.0.0.169') app.commandLine.appendSwitch('ppapi-flash-version', '17.0.0.169')
app.on('ready', () => { app.whenReady().then(() => {
let win = new BrowserWindow({ let win = new BrowserWindow({
width: 800, width: 800,
height: 600, height: 600,

View file

@ -10,7 +10,7 @@ if (!process.electronBinding('features').isExtensionsEnabled()) {
const { app, session, BrowserWindow, deprecate } = require('electron') const { app, session, BrowserWindow, deprecate } = require('electron')
app.once('ready', function () { app.whenReady().then(function () {
const addExtension = function (srcDirectory) { const addExtension = function (srcDirectory) {
return session.defaultSession.loadExtension(srcDirectory) return session.defaultSession.loadExtension(srcDirectory)
} }

View file

@ -466,7 +466,7 @@ app.on('will-quit', function () {
}) })
// We can not use protocol or BrowserWindow until app is ready. // We can not use protocol or BrowserWindow until app is ready.
app.once('ready', function () { app.whenReady().then(function () {
// The public API to add/remove extensions. // The public API to add/remove extensions.
BrowserWindow.addExtension = function (srcDirectory) { BrowserWindow.addExtension = function (srcDirectory) {
const manifest = getManifestFromPath(srcDirectory) const manifest = getManifestFromPath(srcDirectory)

View file

@ -204,7 +204,7 @@ const { setDefaultApplicationMenu } = require('@electron/internal/browser/defaul
// Note that the task must be added before loading any app, so we can make sure // Note that the task must be added before loading any app, so we can make sure
// the call is maded before any user window is created, otherwise the default // the call is maded before any user window is created, otherwise the default
// menu may show even when user explicitly hides the menu. // menu may show even when user explicitly hides the menu.
app.once('ready', setDefaultApplicationMenu) app.whenReady().then(setDefaultApplicationMenu)
if (packagePath) { if (packagePath) {
// Finally load app's main.js and transfer control to C++. // Finally load app's main.js and transfer control to C++.

View file

@ -1,6 +1,6 @@
const { app, ipcMain } = require('electron') const { app, ipcMain } = require('electron')
app.on('ready', () => { app.whenReady().then(() => {
process.stdout.write(JSON.stringify(ipcMain.eventNames())) process.stdout.write(JSON.stringify(ipcMain.eventNames()))
process.stdout.end() process.stdout.end()

View file

@ -1,5 +1,5 @@
const { BrowserView, app } = require('electron') const { BrowserView, app } = require('electron')
app.on('ready', function () { app.whenReady().then(function () {
new BrowserView({}) // eslint-disable-line new BrowserView({}) // eslint-disable-line
app.quit() app.quit()

View file

@ -1,5 +1,5 @@
const { WebContentsView, app, webContents } = require('electron') const { WebContentsView, app, webContents } = require('electron')
app.on('ready', function () { app.whenReady().then(function () {
const web = webContents.create({}) const web = webContents.create({})
new WebContentsView(web) // eslint-disable-line new WebContentsView(web) // eslint-disable-line

View file

@ -14,7 +14,7 @@ function request () {
}) })
} }
app.on('ready', async () => { app.whenReady().then(async () => {
const netLog = session.defaultSession.netLog const netLog = session.defaultSession.netLog
if (process.env.TEST_DUMP_FILE_DYNAMIC) { if (process.env.TEST_DUMP_FILE_DYNAMIC) {

View file

@ -1,7 +1,7 @@
const { app, BrowserWindow } = require('electron') const { app, BrowserWindow } = require('electron')
let win let win
app.on('ready', function () { app.whenReady().then(function () {
win = new BrowserWindow({}) win = new BrowserWindow({})
win.loadURL('about:blank') win.loadURL('about:blank')
win.setMenu(null) win.setMenu(null)

View file

@ -1,7 +1,7 @@
const { app, BrowserWindow } = require('electron') const { app, BrowserWindow } = require('electron')
let win let win
app.on('ready', function () { app.whenReady().then(function () {
win = new BrowserWindow({}) win = new BrowserWindow({})
win.loadURL('about:blank') win.loadURL('about:blank')
win.setMenuBarVisibility(false) win.setMenuBarVisibility(false)

View file

@ -1,6 +1,6 @@
const { app } = require('electron') const { app } = require('electron')
app.on('ready', () => { app.whenReady().then(() => {
const payload = { const payload = {
hasSwitch: app.commandLine.hasSwitch('foobar'), hasSwitch: app.commandLine.hasSwitch('foobar'),
getSwitchValue: app.commandLine.getSwitchValue('foobar') getSwitchValue: app.commandLine.getSwitchValue('foobar')

View file

@ -1,6 +1,6 @@
const { app, session } = require('electron') const { app, session } = require('electron')
app.on('ready', async function () { app.whenReady().then(async function () {
const url = 'http://foo.bar' const url = 'http://foo.bar'
const persistentSession = session.fromPartition('persist:ence-test') const persistentSession = session.fromPartition('persist:ence-test')
const name = 'test' const name = 'test'

View file

@ -18,7 +18,7 @@ try {
Menu.setApplicationMenu(null) Menu.setApplicationMenu(null)
} }
app.on('ready', () => { app.whenReady().then(() => {
setImmediate(() => { setImmediate(() => {
try { try {
output(Menu.getApplicationMenu() === expectedMenu) output(Menu.getApplicationMenu() === expectedMenu)

View file

@ -8,7 +8,7 @@ function createWindow (id) {
windows.push(window) windows.push(window)
} }
app.once('ready', () => { app.whenReady().then(() => {
for (let i = 1; i <= 5; i++) { for (let i = 1; i <= 5; i++) {
createWindow(i) createWindow(i)
} }

View file

@ -2,7 +2,7 @@ const { app } = require('electron')
app.commandLine.appendSwitch('--disable-software-rasterizer') app.commandLine.appendSwitch('--disable-software-rasterizer')
app.on('ready', () => { app.whenReady().then(() => {
const infoType = process.argv.pop() const infoType = process.argv.pop()
app.getGPUInfo(infoType).then( app.getGPUInfo(infoType).then(
(gpuInfo) => { (gpuInfo) => {

View file

@ -1,5 +1,5 @@
const { app, webContents } = require('electron') const { app, webContents } = require('electron')
app.on('ready', function () { app.whenReady().then(function () {
webContents.create({}) webContents.create({})
app.quit() app.quit()

View file

@ -1,6 +1,6 @@
const { app } = require('electron') const { app } = require('electron')
app.on('ready', () => { app.whenReady().then(() => {
process.stdout.write(app.getLocale()) process.stdout.write(app.getLocale())
process.stdout.end() process.stdout.end()

View file

@ -12,7 +12,7 @@ if (process.argv.includes('--app-enable-sandbox')) {
let currentWindowSandboxed = false let currentWindowSandboxed = false
app.once('ready', () => { app.whenReady().then(() => {
function testWindow (isSandboxed, callback) { function testWindow (isSandboxed, callback) {
currentWindowSandboxed = isSandboxed currentWindowSandboxed = isSandboxed
const currentWindow = new BrowserWindow({ const currentWindow = new BrowserWindow({

View file

@ -1,6 +1,6 @@
const { app } = require('electron') const { app } = require('electron')
app.on('ready', function () { app.whenReady().then(function () {
// This setImmediate call gets the spec passing on Linux // This setImmediate call gets the spec passing on Linux
setImmediate(function () { setImmediate(function () {
app.exit(123) app.exit(123)

View file

@ -7,7 +7,7 @@ process.on('uncaughtException', () => {
app.exit(1) app.exit(1)
}) })
app.once('ready', () => { app.whenReady().then(() => {
const lastArg = process.argv[process.argv.length - 1] const lastArg = process.argv[process.argv.length - 1]
const client = net.connect(socketPath) const client = net.connect(socketPath)
client.once('connect', () => { client.once('connect', () => {

View file

@ -1,6 +1,6 @@
const { app } = require('electron') const { app } = require('electron')
app.once('ready', () => { app.whenReady().then(() => {
console.log('started') // ping parent console.log('started') // ping parent
}) })

View file

@ -14,7 +14,7 @@ app.on('quit', () => {
process.stdout.end() process.stdout.end()
}) })
app.on('ready', () => { app.whenReady().then(() => {
const win = new BrowserWindow() const win = new BrowserWindow()
win.close() win.close()
}) })

View file

@ -12,7 +12,7 @@ const FIVE_MINUTES = 5 * 60 * 1000
let window let window
app.once('ready', () => { app.whenReady().then(() => {
window = new BrowserWindow({ window = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {

View file

@ -2,7 +2,7 @@
const { app } = require('electron') const { app } = require('electron')
app.on('ready', () => { app.whenReady().then(() => {
let returnCode = 0 let returnCode = 0
try { try {
const testValue = f() // eslint-disable-line no-undef const testValue = f() // eslint-disable-line no-undef

View file

@ -94,7 +94,7 @@ app.on('renderer-process-crashed', (event, contents, killed) => {
console.log(`webContents ${contents.id} crashed: ${contents.getURL()} (killed=${killed})`) console.log(`webContents ${contents.id} crashed: ${contents.getURL()} (killed=${killed})`)
}) })
app.on('ready', async function () { app.whenReady().then(async function () {
await session.defaultSession.clearCache() await session.defaultSession.clearCache()
await session.defaultSession.clearStorageData() await session.defaultSession.clearStorageData()
// Test if using protocol module would crash. // Test if using protocol module would crash.

View file

@ -54,7 +54,7 @@ if (!gotLock) {
// This method will be called when Electron has done everything // This method will be called when Electron has done everything
// initialization and ready for creating browser windows. // initialization and ready for creating browser windows.
app.on('ready', () => { app.whenReady().then(() => {
// Create the browser window. // Create the browser window.
mainWindow = new BrowserWindow({ width: 800, height: 600 }) mainWindow = new BrowserWindow({ width: 800, height: 600 })
@ -147,7 +147,7 @@ app.on('ready', () => {
app.commandLine.appendSwitch('enable-web-bluetooth') app.commandLine.appendSwitch('enable-web-bluetooth')
app.on('ready', () => { app.whenReady().then(() => {
mainWindow.webContents.on('select-bluetooth-device', (event, deviceList, callback) => { mainWindow.webContents.on('select-bluetooth-device', (event, deviceList, callback) => {
event.preventDefault() event.preventDefault()
@ -322,7 +322,7 @@ app.setAboutPanelOptions({
let onlineStatusWindow: Electron.BrowserWindow let onlineStatusWindow: Electron.BrowserWindow
app.on('ready', () => { app.whenReady().then(() => {
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false, vibrancy: 'sidebar' }) onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false, vibrancy: 'sidebar' })
onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`) onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`)
}) })
@ -335,7 +335,7 @@ ipcMain.on('online-status-changed', (event: any, status: any) => {
// Synopsis // Synopsis
// https://github.com/atom/electron/blob/master/docs/api/synopsis.md // https://github.com/atom/electron/blob/master/docs/api/synopsis.md
app.on('ready', () => { app.whenReady().then(() => {
window = new BrowserWindow({ window = new BrowserWindow({
width: 800, width: 800,
height: 600, height: 600,
@ -737,7 +737,7 @@ const template = <Electron.MenuItemConstructorOptions[]> [
menu = Menu.buildFromTemplate(template) menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu) // Must be called within app.on('ready', function(){ ... }); Menu.setApplicationMenu(menu) // Must be called within app.whenReady().then(function(){ ... });
Menu.buildFromTemplate([ Menu.buildFromTemplate([
{ label: '4', id: '4' }, { label: '4', id: '4' },
@ -807,7 +807,7 @@ Menu.buildFromTemplate([
// net // net
// https://github.com/electron/electron/blob/master/docs/api/net.md // https://github.com/electron/electron/blob/master/docs/api/net.md
app.on('ready', () => { app.whenReady().then(() => {
const request = net.request('https://github.com') const request = net.request('https://github.com')
request.setHeader('Some-Custom-Header-Name', 'Some-Custom-Header-Value') request.setHeader('Some-Custom-Header-Name', 'Some-Custom-Header-Value')
const header = request.getHeader('Some-Custom-Header-Name') const header = request.getHeader('Some-Custom-Header-Name')
@ -852,7 +852,7 @@ app.on('ready', () => {
// power-monitor // power-monitor
// https://github.com/atom/electron/blob/master/docs/api/power-monitor.md // https://github.com/atom/electron/blob/master/docs/api/power-monitor.md
app.on('ready', () => { app.whenReady().then(() => {
powerMonitor.on('suspend', () => { powerMonitor.on('suspend', () => {
console.log('The system is going to sleep') console.log('The system is going to sleep')
}) })
@ -878,7 +878,7 @@ powerSaveBlocker.stop(id)
// protocol // protocol
// https://github.com/atom/electron/blob/master/docs/api/protocol.md // https://github.com/atom/electron/blob/master/docs/api/protocol.md
app.on('ready', () => { app.whenReady().then(() => {
protocol.registerSchemesAsPrivileged([{ scheme: 'https', privileges: { standard: true, allowServiceWorkers: true } }]) protocol.registerSchemesAsPrivileged([{ scheme: 'https', privileges: { standard: true, allowServiceWorkers: true } }])
protocol.registerFileProtocol('atom', (request, callback) => { protocol.registerFileProtocol('atom', (request, callback) => {
@ -910,7 +910,7 @@ app.on('ready', () => {
// https://github.com/atom/electron/blob/master/docs/api/tray.md // https://github.com/atom/electron/blob/master/docs/api/tray.md
let appIcon: Electron.Tray = null let appIcon: Electron.Tray = null
app.on('ready', () => { app.whenReady().then(() => {
appIcon = new Tray('/path/to/my/icon') appIcon = new Tray('/path/to/my/icon')
const contextMenu = Menu.buildFromTemplate([ const contextMenu = Menu.buildFromTemplate([
{ label: 'Item1', type: 'radio' }, { label: 'Item1', type: 'radio' },
@ -1016,12 +1016,12 @@ process.setFdLimit(8192)
// screen // screen
// https://github.com/atom/electron/blob/master/docs/api/screen.md // https://github.com/atom/electron/blob/master/docs/api/screen.md
app.on('ready', () => { app.whenReady().then(() => {
const size = screen.getPrimaryDisplay().workAreaSize const size = screen.getPrimaryDisplay().workAreaSize
mainWindow = new BrowserWindow({ width: size.width, height: size.height }) mainWindow = new BrowserWindow({ width: size.width, height: size.height })
}) })
app.on('ready', () => { app.whenReady().then(() => {
const displays = screen.getAllDisplays() const displays = screen.getAllDisplays()
let externalDisplay: any = null let externalDisplay: any = null
for (const i in displays) { for (const i in displays) {
@ -1190,7 +1190,7 @@ session.defaultSession.webRequest.onBeforeSendHeaders(filter, function (details:
callback({ cancel: false, requestHeaders: details.requestHeaders }) callback({ cancel: false, requestHeaders: details.requestHeaders })
}) })
app.on('ready', function () { app.whenReady().then(function () {
const protocol = session.defaultSession.protocol const protocol = session.defaultSession.protocol
protocol.registerFileProtocol('atom', function (request, callback) { protocol.registerFileProtocol('atom', function (request, callback) {
const url = request.url.substr(7) const url = request.url.substr(7)

View file

@ -191,12 +191,12 @@ const app = remote.app
let mainWindow: Electron.BrowserWindow = null let mainWindow: Electron.BrowserWindow = null
app.on('ready', () => { app.whenReady().then(() => {
const size = screen.getPrimaryDisplay().workAreaSize const size = screen.getPrimaryDisplay().workAreaSize
mainWindow = new BrowserWindow({ width: size.width, height: size.height }) mainWindow = new BrowserWindow({ width: size.width, height: size.height })
}) })
app.on('ready', () => { app.whenReady().then(() => {
const displays = screen.getAllDisplays() const displays = screen.getAllDisplays()
let externalDisplay: any = null let externalDisplay: any = null
for (const i in displays) { for (const i in displays) {