No need to call sender.callbacks.remove

The IDWeakMap automatically removes the key when object is garbage
collected.
This commit is contained in:
Cheng Zhao 2016-01-13 17:07:18 +08:00
parent 3875b30fcf
commit c10c419f1d
2 changed files with 26 additions and 21 deletions

View file

@ -70,6 +70,10 @@ let wrapWebContents = function(webContents) {
var controller, method, name, ref1; var controller, method, name, ref1;
webContents.__proto__ = EventEmitter.prototype; webContents.__proto__ = EventEmitter.prototype;
// Every remote callback from renderer process would add a listenter to the
// render-view-deleted event, so ignore the listenters warning.
webContents.setMaxListeners(0);
// WebContents::send(channel, args..) // WebContents::send(channel, args..)
webContents.send = function() { webContents.send = function() {
var args, channel; var args, channel;

View file

@ -1,3 +1,5 @@
'use strict';
const path = require('path'); const path = require('path');
const electron = require('electron'); const electron = require('electron');
const ipcMain = electron.ipcMain; const ipcMain = electron.ipcMain;
@ -41,7 +43,7 @@ var valueToMeta = function(sender, value, optimizeSimpleObject) {
} }
// Treat the arguments object as array. // Treat the arguments object as array.
if (meta.type === 'object' && (value.callee != null) && (value.length != null)) { if (meta.type === 'object' && (value.hasOwnProperty('callee')) && (value.length != null)) {
meta.type = 'array'; meta.type = 'array';
} }
if (meta.type === 'array') { if (meta.type === 'array') {
@ -113,7 +115,7 @@ var exceptionToMeta = function(error) {
var unwrapArgs = function(sender, args) { var unwrapArgs = function(sender, args) {
var metaToValue; var metaToValue;
metaToValue = function(meta) { metaToValue = function(meta) {
var i, len, member, ref, rendererReleased, ret, returnValue; var i, len, member, ref, rendererReleased, returnValue;
switch (meta.type) { switch (meta.type) {
case 'value': case 'value':
return meta.value; return meta.value;
@ -130,7 +132,7 @@ var unwrapArgs = function(sender, args) {
then: metaToValue(meta.then) then: metaToValue(meta.then)
}); });
case 'object': case 'object':
ret = v8Util.createObjectWithName(meta.name); let ret = v8Util.createObjectWithName(meta.name);
ref = meta.members; ref = meta.members;
for (i = 0, len = ref.length; i < len; i++) { for (i = 0, len = ref.length; i < len; i++) {
member = ref[i]; member = ref[i];
@ -147,31 +149,30 @@ var unwrapArgs = function(sender, args) {
if (!sender.callbacks) { if (!sender.callbacks) {
sender.callbacks = new IDWeakMap; sender.callbacks = new IDWeakMap;
sender.on('render-view-deleted', function() { sender.on('render-view-deleted', function() {
return sender.callbacks.clear(); return this.callbacks.clear();
}); });
} }
if (sender.callbacks.has(meta.id)) {
if (sender.callbacks.has(meta.id))
return sender.callbacks.get(meta.id); return sender.callbacks.get(meta.id);
}
// Prevent the callback from being called when its page is gone.
rendererReleased = false; rendererReleased = false;
objectsRegistry.once("clear-" + (sender.getId()), function() { sender.once('render-view-deleted', function() {
return rendererReleased = true; rendererReleased = true;
}); });
ret = function() {
if (rendererReleased) { let callIntoRenderer = function(...args) {
throw new Error("Attempting to call a function in a renderer window that has been closed or released. Function provided here: " + meta.location + "."); if (rendererReleased)
} throw new Error(`Attempting to call a function in a renderer window that has been closed or released. Function provided here: ${meta.location}.`);
return sender.send('ATOM_RENDERER_CALLBACK', meta.id, valueToMeta(sender, arguments)); sender.send('ATOM_RENDERER_CALLBACK', meta.id, valueToMeta(sender, args));
}; };
v8Util.setDestructor(ret, function() { v8Util.setDestructor(callIntoRenderer, function() {
if (rendererReleased) { if (!rendererReleased)
return; sender.send('ATOM_RENDERER_RELEASE_CALLBACK', meta.id);
}
sender.callbacks.remove(meta.id);
return sender.send('ATOM_RENDERER_RELEASE_CALLBACK', meta.id);
}); });
sender.callbacks.set(meta.id, ret); sender.callbacks.set(meta.id, callIntoRenderer);
return ret; return callIntoRenderer;
default: default:
throw new TypeError("Unknown type: " + meta.type); throw new TypeError("Unknown type: " + meta.type);
} }