Use Proxy to provide protocol APIs
In this way we can avoid initializing defaultSession when protocol module is used.
This commit is contained in:
parent
4ebb83e999
commit
fcd3357fb8
1 changed files with 16 additions and 14 deletions
|
@ -1,17 +1,19 @@
|
|||
const {app, session} = require('electron')
|
||||
const {registerStandardSchemes} = process.atomBinding('protocol')
|
||||
const {session} = require('electron')
|
||||
|
||||
exports.registerStandardSchemes = registerStandardSchemes
|
||||
// Global protocol APIs.
|
||||
module.exports = process.atomBinding('protocol')
|
||||
|
||||
const setupProtocol = function () {
|
||||
let protocol = session.defaultSession.protocol
|
||||
for (let method in protocol) {
|
||||
exports[method] = protocol[method].bind(protocol)
|
||||
// Fallback protocol APIs of default session.
|
||||
Object.setPrototypeOf(module.exports, new Proxy({}, {
|
||||
get (target, property) {
|
||||
return (...args) => session.defaultSession.protocol[property](...args)
|
||||
},
|
||||
|
||||
ownKeys () {
|
||||
return Object.getOwnPropertyNames(session.defaultSession.protocol)
|
||||
},
|
||||
|
||||
getOwnPropertyDescriptor (target) {
|
||||
return { configurable: true, enumerable: true }
|
||||
}
|
||||
}
|
||||
|
||||
if (app.isReady()) {
|
||||
setupProtocol()
|
||||
} else {
|
||||
app.once('ready', setupProtocol)
|
||||
}
|
||||
}))
|
||||
|
|
Loading…
Reference in a new issue