Use Proxy to provide protocol APIs

In this way we can avoid initializing defaultSession when protocol
module is used.
This commit is contained in:
Cheng Zhao 2016-07-12 19:27:15 +09:00
parent 4ebb83e999
commit fcd3357fb8

View file

@ -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)
}