From fcd3357fb807b2666c6536c0beb68b184031cc04 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 12 Jul 2016 19:27:15 +0900 Subject: [PATCH] Use Proxy to provide protocol APIs In this way we can avoid initializing defaultSession when protocol module is used. --- lib/browser/api/protocol.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/browser/api/protocol.js b/lib/browser/api/protocol.js index f7bf48a250c..d1c26991d0e 100644 --- a/lib/browser/api/protocol.js +++ b/lib/browser/api/protocol.js @@ -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) -} +}))