Merge pull request #8122 from electron/use-spread-instead-of-apply

Use spread syntax instead of apply
This commit is contained in:
Kevin Sawicki 2016-12-01 17:15:22 -08:00 committed by GitHub
commit d0643250f7
15 changed files with 60 additions and 44 deletions

View file

@ -81,7 +81,7 @@ app.allowNTLMCredentialsForAllDomains = function (allow) {
const events = ['login', 'certificate-error', 'select-client-certificate'] const events = ['login', 'certificate-error', 'select-client-certificate']
for (let name of events) { for (let name of events) {
app.on(name, (event, webContents, ...args) => { app.on(name, (event, webContents, ...args) => {
webContents.emit.apply(webContents, [name, event].concat(args)) webContents.emit(name, event, ...args)
}) })
} }

View file

@ -152,19 +152,19 @@ BrowserWindow.fromDevToolsWebContents = (webContents) => {
// Helpers. // Helpers.
Object.assign(BrowserWindow.prototype, { Object.assign(BrowserWindow.prototype, {
loadURL (...args) { loadURL (...args) {
return this.webContents.loadURL.apply(this.webContents, args) return this.webContents.loadURL(...args)
}, },
getURL (...args) { getURL (...args) {
return this.webContents.getURL() return this.webContents.getURL()
}, },
reload (...args) { reload (...args) {
return this.webContents.reload.apply(this.webContents, args) return this.webContents.reload(...args)
}, },
send (...args) { send (...args) {
return this.webContents.send.apply(this.webContents, args) return this.webContents.send(...args)
}, },
openDevTools (...args) { openDevTools (...args) {
return this.webContents.openDevTools.apply(this.webContents, args) return this.webContents.openDevTools(...args)
}, },
closeDevTools () { closeDevTools () {
return this.webContents.closeDevTools() return this.webContents.closeDevTools()
@ -179,7 +179,7 @@ Object.assign(BrowserWindow.prototype, {
return this.webContents.toggleDevTools() return this.webContents.toggleDevTools()
}, },
inspectElement (...args) { inspectElement (...args) {
return this.webContents.inspectElement.apply(this.webContents, args) return this.webContents.inspectElement(...args)
}, },
inspectServiceWorker () { inspectServiceWorker () {
return this.webContents.inspectServiceWorker() return this.webContents.inspectServiceWorker()
@ -188,7 +188,7 @@ Object.assign(BrowserWindow.prototype, {
return this.webContents.showDefinitionForSelection() return this.webContents.showDefinitionForSelection()
}, },
capturePage (...args) { capturePage (...args) {
return this.webContents.capturePage.apply(this.webContents, args) return this.webContents.capturePage(...args)
} }
}) })

View file

@ -50,7 +50,7 @@ module.exports = {
showOpenDialog: function (...args) { showOpenDialog: function (...args) {
var prop, properties, value, wrappedCallback var prop, properties, value, wrappedCallback
checkAppInitialized() checkAppInitialized()
let [window, options, callback] = parseArgs.apply(null, args) let [window, options, callback] = parseArgs(...args)
if (options == null) { if (options == null) {
options = { options = {
title: 'Open', title: 'Open',
@ -97,7 +97,7 @@ module.exports = {
showSaveDialog: function (...args) { showSaveDialog: function (...args) {
var wrappedCallback var wrappedCallback
checkAppInitialized() checkAppInitialized()
let [window, options, callback] = parseArgs.apply(null, args) let [window, options, callback] = parseArgs(...args)
if (options == null) { if (options == null) {
options = { options = {
title: 'Save' title: 'Save'
@ -130,7 +130,7 @@ module.exports = {
showMessageBox: function (...args) { showMessageBox: function (...args) {
var flags, i, j, len, messageBoxType, ref2, ref3, text var flags, i, j, len, messageBoxType, ref2, ref3, text
checkAppInitialized() checkAppInitialized()
let [window, options, callback] = parseArgs.apply(null, args) let [window, options, callback] = parseArgs(...args)
if (options == null) { if (options == null) {
options = { options = {
type: 'none' type: 'none'
@ -185,7 +185,7 @@ module.exports = {
}, },
showErrorBox: function (...args) { showErrorBox: function (...args) {
return binding.showErrorBox.apply(binding, args) return binding.showErrorBox(...args)
} }
} }

View file

@ -1,14 +1,16 @@
const EventEmitter = require('events').EventEmitter const EventEmitter = require('events').EventEmitter
module.exports = new EventEmitter() const emitter = new EventEmitter()
const removeAllListeners = module.exports.removeAllListeners const removeAllListeners = emitter.removeAllListeners.bind(emitter)
module.exports.removeAllListeners = function (...args) { emitter.removeAllListeners = function (...args) {
if (args.length === 0) { if (args.length === 0) {
throw new Error('Removing all listeners from ipcMain will make Electron internals stop working. Please specify a event name') throw new Error('Removing all listeners from ipcMain will make Electron internals stop working. Please specify a event name')
} }
removeAllListeners.apply(this, args) removeAllListeners(...args)
} }
// Do not throw exception when channel name is "error". // Do not throw exception when channel name is "error".
module.exports.on('error', () => {}) emitter.on('error', () => {})
module.exports = emitter

View file

@ -4,13 +4,11 @@ const {ipcMain} = require('electron')
// The history operation in renderer is redirected to browser. // The history operation in renderer is redirected to browser.
ipcMain.on('ELECTRON_NAVIGATION_CONTROLLER', function (event, method, ...args) { ipcMain.on('ELECTRON_NAVIGATION_CONTROLLER', function (event, method, ...args) {
var ref event.sender[method](...args)
(ref = event.sender)[method].apply(ref, args)
}) })
ipcMain.on('ELECTRON_SYNC_NAVIGATION_CONTROLLER', function (event, method, ...args) { ipcMain.on('ELECTRON_SYNC_NAVIGATION_CONTROLLER', function (event, method, ...args) {
var ref event.returnValue = event.sender[method](...args)
event.returnValue = (ref = event.sender)[method].apply(ref, args)
}) })
// JavaScript implementation of Chromium's NavigationController. // JavaScript implementation of Chromium's NavigationController.

View file

@ -83,20 +83,20 @@ class IncomingMessage extends Readable {
URLRequest.prototype._emitRequestEvent = function (isAsync, ...rest) { URLRequest.prototype._emitRequestEvent = function (isAsync, ...rest) {
if (isAsync) { if (isAsync) {
process.nextTick(() => { process.nextTick(() => {
this.clientRequest.emit.apply(this.clientRequest, rest) this.clientRequest.emit(...rest)
}) })
} else { } else {
this.clientRequest.emit.apply(this.clientRequest, rest) this.clientRequest.emit(...rest)
} }
} }
URLRequest.prototype._emitResponseEvent = function (isAsync, ...rest) { URLRequest.prototype._emitResponseEvent = function (isAsync, ...rest) {
if (isAsync) { if (isAsync) {
process.nextTick(() => { process.nextTick(() => {
this._response.emit.apply(this._response, rest) this._response.emit(...rest)
}) })
} else { } else {
this._response.emit.apply(this._response, rest) this._response.emit(...rest)
} }
} }

View file

@ -246,7 +246,7 @@ WebContents.prototype._init = function () {
// Delays the page-title-updated event to next tick. // Delays the page-title-updated event to next tick.
this.on('-page-title-updated', function (...args) { this.on('-page-title-updated', function (...args) {
setImmediate(() => { setImmediate(() => {
this.emit.apply(this, ['page-title-updated'].concat(args)) this.emit('page-title-updated', ...args)
}) })
}) })

View file

@ -211,7 +211,7 @@ ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', function (event, guest
const guestWindow = getGuestWindow(guestContents) const guestWindow = getGuestWindow(guestContents)
if (guestWindow != null) { if (guestWindow != null) {
event.returnValue = guestWindow[method].apply(guestWindow, args) event.returnValue = guestWindow[method](...args)
} else { } else {
event.returnValue = null event.returnValue = null
} }
@ -235,7 +235,7 @@ ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', function (event,
if (guestContents == null) return if (guestContents == null) return
if (canAccessWindow(event.sender, guestContents)) { if (canAccessWindow(event.sender, guestContents)) {
guestContents[method].apply(guestContents, args) guestContents[method](...args)
} else { } else {
console.error(`Blocked ${event.sender.getURL()} from calling ${method} on its opener.`) console.error(`Blocked ${event.sender.getURL()} from calling ${method} on its opener.`)
} }
@ -249,7 +249,7 @@ ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', function (e
} }
if (canAccessWindow(event.sender, guestContents)) { if (canAccessWindow(event.sender, guestContents)) {
event.returnValue = guestContents[method].apply(guestContents, args) event.returnValue = guestContents[method](...args)
} else { } else {
console.error(`Blocked ${event.sender.getURL()} from calling ${method} on its opener.`) console.error(`Blocked ${event.sender.getURL()} from calling ${method} on its opener.`)
event.returnValue = null event.returnValue = null

View file

@ -26,7 +26,7 @@ if (process.platform === 'win32') {
// Redirect node's console to use our own implementations, since node can not // Redirect node's console to use our own implementations, since node can not
// handle console output when running as GUI program. // handle console output when running as GUI program.
var consoleLog = function (...args) { var consoleLog = function (...args) {
return process.log(util.format.apply(util, args) + '\n') return process.log(util.format(...args) + '\n')
} }
var streamWrite = function (chunk, encoding, callback) { var streamWrite = function (chunk, encoding, callback) {
if (Buffer.isBuffer(chunk)) { if (Buffer.isBuffer(chunk)) {

View file

@ -44,14 +44,8 @@ class CallbacksRegistry {
return (ref = this.callbacks[id]) != null ? ref : function () {} return (ref = this.callbacks[id]) != null ? ref : function () {}
} }
call (id, ...args) {
var ref
return (ref = this.get(id)).call.apply(ref, [global].concat(args))
}
apply (id, ...args) { apply (id, ...args) {
var ref return this.get(id).apply(global, ...args)
return (ref = this.get(id)).apply.apply(ref, [global].concat(args))
} }
remove (id) { remove (id) {

View file

@ -30,11 +30,11 @@ module.exports = function (ipcRenderer, binding) {
ipcRenderer.send('ELECTRON_BROWSER_SEND_TO', true, webContentsId, channel, ...args) ipcRenderer.send('ELECTRON_BROWSER_SEND_TO', true, webContentsId, channel, ...args)
} }
const removeAllListeners = ipcRenderer.removeAllListeners const removeAllListeners = ipcRenderer.removeAllListeners.bind(ipcRenderer)
ipcRenderer.removeAllListeners = function (...args) { ipcRenderer.removeAllListeners = function (...args) {
if (args.length === 0) { if (args.length === 0) {
throw new Error('Removing all listeners from ipcRenderer will make Electron internals stop working. Please specify a event name') throw new Error('Removing all listeners from ipcRenderer will make Electron internals stop working. Please specify a event name')
} }
removeAllListeners.apply(this, args) removeAllListeners(...args)
} }
} }

View file

@ -29,11 +29,11 @@ const electron = require('electron')
// Call webFrame method. // Call webFrame method.
electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', (event, method, args) => { electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', (event, method, args) => {
electron.webFrame[method].apply(electron.webFrame, args) electron.webFrame[method](...args)
}) })
electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_SYNC_WEB_FRAME_METHOD', (event, requestId, method, args) => { electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_SYNC_WEB_FRAME_METHOD', (event, requestId, method, args) => {
const result = electron.webFrame[method].apply(electron.webFrame, args) const result = electron.webFrame[method](...args)
event.sender.send(`ELECTRON_INTERNAL_BROWSER_SYNC_WEB_FRAME_RESPONSE_${requestId}`, result) event.sender.send(`ELECTRON_INTERNAL_BROWSER_SYNC_WEB_FRAME_RESPONSE_${requestId}`, result)
}) })

View file

@ -74,7 +74,7 @@ var BrowserWindowProxy = (function () {
} }
BrowserWindowProxy.prototype['eval'] = function (...args) { BrowserWindowProxy.prototype['eval'] = function (...args) {
ipcRenderer.send.apply(ipcRenderer, ['ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, 'executeJavaScript'].concat(args)) ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, 'executeJavaScript', ...args)
} }
return BrowserWindowProxy return BrowserWindowProxy
@ -207,11 +207,11 @@ ipcRenderer.on('ELECTRON_GUEST_WINDOW_POSTMESSAGE', function (event, sourceId, m
// Forward history operations to browser. // Forward history operations to browser.
var sendHistoryOperation = function (...args) { var sendHistoryOperation = function (...args) {
ipcRenderer.send.apply(ipcRenderer, ['ELECTRON_NAVIGATION_CONTROLLER'].concat(args)) ipcRenderer.send('ELECTRON_NAVIGATION_CONTROLLER', ...args)
} }
var getHistoryOperation = function (...args) { var getHistoryOperation = function (...args) {
return ipcRenderer.sendSync.apply(ipcRenderer, ['ELECTRON_SYNC_NAVIGATION_CONTROLLER'].concat(args)) return ipcRenderer.sendSync('ELECTRON_SYNC_NAVIGATION_CONTROLLER', ...args)
} }
window.history.back = function () { window.history.back = function () {

View file

@ -14,7 +14,7 @@ const geval = eval
require('../renderer/api/ipc-renderer-setup')(ipcRenderer, binding) require('../renderer/api/ipc-renderer-setup')(ipcRenderer, binding)
binding.onMessage = function (channel, args) { binding.onMessage = function (channel, args) {
ipcRenderer.emit.apply(ipcRenderer, [channel].concat(args)) ipcRenderer.emit(channel, ...args)
} }
binding.onExit = function () { binding.onExit = function () {

View file

@ -495,4 +495,26 @@ describe('ipc module', function () {
assert.equal(w.listenerCount('test'), 0) assert.equal(w.listenerCount('test'), 0)
}) })
}) })
it('throws an error when removing all the listeners', () => {
ipcMain.on('test-event', () => {})
assert.equal(ipcMain.listenerCount('test-event'), 1)
ipcRenderer.on('test-event', () => {})
assert.equal(ipcRenderer.listenerCount('test-event'), 1)
assert.throws(() => {
ipcMain.removeAllListeners()
}, /Removing all listeners from ipcMain will make Electron internals stop working/)
assert.throws(() => {
ipcRenderer.removeAllListeners()
}, /Removing all listeners from ipcRenderer will make Electron internals stop working/)
ipcMain.removeAllListeners('test-event')
assert.equal(ipcMain.listenerCount('test-event'), 0)
ipcRenderer.removeAllListeners('test-event')
assert.equal(ipcRenderer.listenerCount('test-event'), 0)
})
}) })