d739d8772c
This follows the convention of other *-created events.
41 lines
1.1 KiB
JavaScript
41 lines
1.1 KiB
JavaScript
const {EventEmitter} = require('events')
|
|
const {app} = 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 (!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 === '') return exports.defaultSession
|
|
|
|
if (partition.startsWith(PERSIST_PREFIX)) {
|
|
return fromPartition(partition.substr(PERSIST_PREFIX.length), false)
|
|
} else {
|
|
return fromPartition(partition, true)
|
|
}
|
|
}
|
|
|
|
// Returns the default session.
|
|
Object.defineProperty(exports, 'defaultSession', {
|
|
enumerable: true,
|
|
get: function () {
|
|
return fromPartition('', false)
|
|
}
|
|
})
|
|
|
|
const wrapSession = function (session) {
|
|
// Session is an EventEmitter.
|
|
Object.setPrototypeOf(session, EventEmitter.prototype)
|
|
app.emit('session-created', session)
|
|
}
|
|
|
|
bindings._setWrapSession(wrapSession)
|