Add "app.whenReady()" (#12652)
* Make "chai-as-promised" avaialble in tests * Add "app.whenReady()" Closes #9561.
This commit is contained in:
parent
c7a0b419a9
commit
fcc82ebd35
4 changed files with 41 additions and 0 deletions
|
@ -432,6 +432,12 @@ app.exit(0)
|
||||||
|
|
||||||
Returns `Boolean` - `true` if Electron has finished initializing, `false` otherwise.
|
Returns `Boolean` - `true` if Electron has finished initializing, `false` otherwise.
|
||||||
|
|
||||||
|
### `app.whenReady()`
|
||||||
|
|
||||||
|
Returns `Promise` - fulfilled when Electron is initialized.
|
||||||
|
May be used as a convenient alternative to checking `app.isReady()`
|
||||||
|
and subscribing to the `ready` event if the app is not ready yet.
|
||||||
|
|
||||||
### `app.focus()`
|
### `app.focus()`
|
||||||
|
|
||||||
On Linux, focuses on the first visible window. On macOS, makes the application
|
On Linux, focuses on the first visible window. On macOS, makes the application
|
||||||
|
|
|
@ -11,12 +11,30 @@ const {deprecate, Menu} = electron
|
||||||
const {EventEmitter} = require('events')
|
const {EventEmitter} = require('events')
|
||||||
|
|
||||||
let dockMenu = null
|
let dockMenu = null
|
||||||
|
let readyPromise = null
|
||||||
|
|
||||||
// App is an EventEmitter.
|
// App is an EventEmitter.
|
||||||
Object.setPrototypeOf(App.prototype, EventEmitter.prototype)
|
Object.setPrototypeOf(App.prototype, EventEmitter.prototype)
|
||||||
EventEmitter.call(app)
|
EventEmitter.call(app)
|
||||||
|
|
||||||
Object.assign(app, {
|
Object.assign(app, {
|
||||||
|
whenReady () {
|
||||||
|
if (readyPromise !== null) {
|
||||||
|
return readyPromise
|
||||||
|
}
|
||||||
|
|
||||||
|
if (app.isReady()) {
|
||||||
|
readyPromise = Promise.resolve()
|
||||||
|
} else {
|
||||||
|
readyPromise = new Promise(resolve => {
|
||||||
|
// XXX(alexeykuzmin): Explicitly ignore any data
|
||||||
|
// passed to the event handler to avoid memory leaks.
|
||||||
|
app.once('ready', () => resolve())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return readyPromise
|
||||||
|
},
|
||||||
setApplicationMenu (menu) {
|
setApplicationMenu (menu) {
|
||||||
return Menu.setApplicationMenu(menu)
|
return Menu.setApplicationMenu(menu)
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
const assert = require('assert')
|
const assert = require('assert')
|
||||||
|
const chai = require('chai')
|
||||||
|
const chaiAsPromised = require('chai-as-promised')
|
||||||
const ChildProcess = require('child_process')
|
const ChildProcess = require('child_process')
|
||||||
const https = require('https')
|
const https = require('https')
|
||||||
const net = require('net')
|
const net = require('net')
|
||||||
|
@ -7,10 +9,13 @@ const path = require('path')
|
||||||
const {ipcRenderer, remote} = require('electron')
|
const {ipcRenderer, remote} = require('electron')
|
||||||
const {closeWindow} = require('./window-helpers')
|
const {closeWindow} = require('./window-helpers')
|
||||||
|
|
||||||
|
const {expect} = chai
|
||||||
const {app, BrowserWindow, Menu, ipcMain} = remote
|
const {app, BrowserWindow, Menu, ipcMain} = remote
|
||||||
|
|
||||||
const isCI = remote.getGlobal('isCi')
|
const isCI = remote.getGlobal('isCi')
|
||||||
|
|
||||||
|
chai.use(chaiAsPromised)
|
||||||
|
|
||||||
describe('electron module', () => {
|
describe('electron module', () => {
|
||||||
it('does not expose internal modules to require', () => {
|
it('does not expose internal modules to require', () => {
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
|
@ -903,4 +908,15 @@ describe('app module', () => {
|
||||||
v8Util.requestGarbageCollectionForTesting()
|
v8Util.requestGarbageCollectionForTesting()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('whenReady', () => {
|
||||||
|
it('returns a Promise', () => {
|
||||||
|
expect(app.whenReady()).to.be.an.instanceOf(Promise)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('becomes fulfilled if the app is already ready', () => {
|
||||||
|
assert(app.isReady())
|
||||||
|
return expect(app.whenReady()).to.be.eventually.fulfilled
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
"basic-auth": "^1.0.4",
|
"basic-auth": "^1.0.4",
|
||||||
"bluebird": "^3.5.1",
|
"bluebird": "^3.5.1",
|
||||||
"chai": "^4.1.2",
|
"chai": "^4.1.2",
|
||||||
|
"chai-as-promised": "^7.1.1",
|
||||||
"coffee-script": "1.12.7",
|
"coffee-script": "1.12.7",
|
||||||
"dbus-native": "^0.2.3",
|
"dbus-native": "^0.2.3",
|
||||||
"graceful-fs": "^4.1.9",
|
"graceful-fs": "^4.1.9",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue