Merge pull request #5820 from electron/session-check
Throw error when session module is used before app is ready
This commit is contained in:
commit
15d32a9096
2 changed files with 25 additions and 17 deletions
|
@ -1,18 +1,26 @@
|
|||
const {EventEmitter} = require('events')
|
||||
const electron = require('electron')
|
||||
const bindings = process.atomBinding('session')
|
||||
|
||||
const PERSIST_PREFIX = 'persist:'
|
||||
|
||||
// Wrapper of binding.fromPartition that checks for ready event.
|
||||
const fromPartition = function (partition, persist) {
|
||||
if (!electron.app.isReady()) {
|
||||
throw new Error('session module can only be used when app is ready')
|
||||
}
|
||||
|
||||
return bindings.fromPartition(partition, persist)
|
||||
}
|
||||
|
||||
// Returns the Session from |partition| string.
|
||||
exports.fromPartition = function (partition) {
|
||||
if (partition == null) {
|
||||
partition = ''
|
||||
}
|
||||
if (partition === '') {
|
||||
return exports.defaultSession
|
||||
}
|
||||
exports.fromPartition = function (partition = '') {
|
||||
if (partition === '') return exports.defaultSession
|
||||
|
||||
if (partition.startsWith(PERSIST_PREFIX)) {
|
||||
return bindings.fromPartition(partition.substr(PERSIST_PREFIX.length), false)
|
||||
return fromPartition(partition.substr(PERSIST_PREFIX.length), false)
|
||||
} else {
|
||||
return bindings.fromPartition(partition, true)
|
||||
return fromPartition(partition, true)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,6 +28,13 @@ exports.fromPartition = function (partition) {
|
|||
Object.defineProperty(exports, 'defaultSession', {
|
||||
enumerable: true,
|
||||
get: function () {
|
||||
return bindings.fromPartition('', false)
|
||||
return fromPartition('', false)
|
||||
}
|
||||
})
|
||||
|
||||
const wrapSession = function (session) {
|
||||
// Session is an EventEmitter.
|
||||
Object.setPrototypeOf(session, EventEmitter.prototype)
|
||||
}
|
||||
|
||||
bindings._setWrapSession(wrapSession)
|
||||
|
|
|
@ -5,7 +5,6 @@ const {ipcMain, Menu, NavigationController} = require('electron')
|
|||
|
||||
const binding = process.atomBinding('web_contents')
|
||||
const debuggerBinding = process.atomBinding('debugger')
|
||||
const sessionBinding = process.atomBinding('session')
|
||||
|
||||
let nextId = 0
|
||||
|
||||
|
@ -212,14 +211,8 @@ let wrapDebugger = function (webContentsDebugger) {
|
|||
Object.setPrototypeOf(webContentsDebugger, EventEmitter.prototype)
|
||||
}
|
||||
|
||||
var wrapSession = function (session) {
|
||||
// session is an EventEmitter.
|
||||
Object.setPrototypeOf(session, EventEmitter.prototype)
|
||||
}
|
||||
|
||||
binding._setWrapWebContents(wrapWebContents)
|
||||
debuggerBinding._setWrapDebugger(wrapDebugger)
|
||||
sessionBinding._setWrapSession(wrapSession)
|
||||
|
||||
module.exports = {
|
||||
create (options = {}) {
|
||||
|
|
Loading…
Reference in a new issue