No longer needs cache for methods of remote

Refs #4517.
This commit is contained in:
Cheng Zhao 2016-02-22 12:26:41 +08:00
parent c65cfadd09
commit fe7462b352

View file

@ -141,7 +141,7 @@ let setObjectPrototype = function(object, metaId, descriptor) {
}; };
// Convert meta data from browser into real value. // Convert meta data from browser into real value.
var metaToValue = function(meta) { let metaToValue = function(meta) {
var el, i, len, ref1, results, ret; var el, i, len, ref1, results, ret;
switch (meta.type) { switch (meta.type) {
case 'value': case 'value':
@ -257,73 +257,33 @@ for (var name in browserModules) {
} }
// Get remote module. // Get remote module.
// (Just like node's require, the modules are cached permanently, note that this
// is safe leak since the object is not expected to get freed in browser)
var moduleCache = {};
exports.require = function(module) { exports.require = function(module) {
var meta; return metaToValue(ipcRenderer.sendSync('ATOM_BROWSER_REQUIRE', module));
if (moduleCache[module] != null) {
return moduleCache[module];
}
meta = ipcRenderer.sendSync('ATOM_BROWSER_REQUIRE', module);
return moduleCache[module] = metaToValue(meta);
}; };
// Optimize require('electron').
moduleCache.electron = exports;
// Alias to remote.require('electron').xxx. // Alias to remote.require('electron').xxx.
var builtinCache = {};
exports.getBuiltin = function(module) { exports.getBuiltin = function(module) {
var meta; return metaToValue(ipcRenderer.sendSync('ATOM_BROWSER_GET_BUILTIN', module));
if (builtinCache[module] != null) {
return builtinCache[module];
}
meta = ipcRenderer.sendSync('ATOM_BROWSER_GET_BUILTIN', module);
return builtinCache[module] = metaToValue(meta);
}; };
// Get current BrowserWindow object. // Get current BrowserWindow.
var windowCache = null;
exports.getCurrentWindow = function() { exports.getCurrentWindow = function() {
var meta; return metaToValue(ipcRenderer.sendSync('ATOM_BROWSER_CURRENT_WINDOW'));
if (windowCache != null) {
return windowCache;
}
meta = ipcRenderer.sendSync('ATOM_BROWSER_CURRENT_WINDOW');
return windowCache = metaToValue(meta);
}; };
// Get current WebContents object. // Get current WebContents object.
var webContentsCache = null;
exports.getCurrentWebContents = function() { exports.getCurrentWebContents = function() {
var meta; return metaToValue(ipcRenderer.sendSync('ATOM_BROWSER_CURRENT_WEB_CONTENTS'));
if (webContentsCache != null) {
return webContentsCache;
}
meta = ipcRenderer.sendSync('ATOM_BROWSER_CURRENT_WEB_CONTENTS');
return webContentsCache = metaToValue(meta);
}; };
// Get a global object in browser. // Get a global object in browser.
exports.getGlobal = function(name) { exports.getGlobal = function(name) {
var meta; return metaToValue(ipcRenderer.sendSync('ATOM_BROWSER_GLOBAL', name));
meta = ipcRenderer.sendSync('ATOM_BROWSER_GLOBAL', name);
return metaToValue(meta);
}; };
// Get the process object in browser. // Get the process object in browser.
var processCache = null;
exports.__defineGetter__('process', function() { exports.__defineGetter__('process', function() {
if (processCache == null) { return exports.getGlobal('process');
processCache = exports.getGlobal('process');
}
return processCache;
}); });
// Create a funtion that will return the specifed value when called in browser. // Create a funtion that will return the specifed value when called in browser.