Throw error when session module is used before app is ready

This commit is contained in:
Cheng Zhao 2016-06-01 10:53:06 +09:00
parent d105524135
commit 8dfbbcefc8

View file

@ -1,16 +1,26 @@
const {EventEmitter} = require('events') const {EventEmitter} = require('events')
const electron = require('electron')
const bindings = process.atomBinding('session') const bindings = process.atomBinding('session')
const PERSIST_PREFIX = 'persist:' 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. // Returns the Session from |partition| string.
exports.fromPartition = function (partition = '') { exports.fromPartition = function (partition = '') {
if (partition === '') return exports.defaultSession if (partition === '') return exports.defaultSession
if (partition.startsWith(PERSIST_PREFIX)) { if (partition.startsWith(PERSIST_PREFIX)) {
return bindings.fromPartition(partition.substr(PERSIST_PREFIX.length), false) return fromPartition(partition.substr(PERSIST_PREFIX.length), false)
} else { } else {
return bindings.fromPartition(partition, true) return fromPartition(partition, true)
} }
} }
@ -18,7 +28,7 @@ exports.fromPartition = function (partition = '') {
Object.defineProperty(exports, 'defaultSession', { Object.defineProperty(exports, 'defaultSession', {
enumerable: true, enumerable: true,
get: function () { get: function () {
return bindings.fromPartition('', false) return fromPartition('', false)
} }
}) })