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 {session} = require('electron')
|
||||||
const {registerStandardSchemes} = process.atomBinding('protocol')
|
|
||||||
|
|
||||||
exports.registerStandardSchemes = registerStandardSchemes
|
// Global protocol APIs.
|
||||||
|
module.exports = process.atomBinding('protocol')
|
||||||
|
|
||||||
const setupProtocol = function () {
|
// Fallback protocol APIs of default session.
|
||||||
let protocol = session.defaultSession.protocol
|
Object.setPrototypeOf(module.exports, new Proxy({}, {
|
||||||
for (let method in protocol) {
|
get (target, property) {
|
||||||
exports[method] = protocol[method].bind(protocol)
|
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