electron.ipcMain的怪异行为

这比电子问题更像是JavaScript。

我有以下代码:

var electron = require("electron")

var registerTestHandler = function(onFn) {
    onFn("test", console.log)
}

electron.app.whenReady()
.then(function() {
    registerTestHandler(electron.ipcMain.on)
})

运行应用程序会引发以下错误:

(node:14139) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_events' of undefined
    at _addListener (events.js:228:19)
    at addListener (events.js:284:10)
    at registerTestHandler (/Users/marco/Desktop/electron-ipc-test/index.js:4:2)
    at /Users/marco/Desktop/electron-ipc-test/index.js:9:2
(node:14139) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_events' of undefined
    at _addListener (events.js:228:19)
    at addListener (events.js:284:10)
    at registerTestHandler (/Users/marco/Desktop/electron-ipc-test/index.js:4:2)
    at /Users/marco/Desktop/electron-ipc-test/index.js:9:2
(node:14139) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14139) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14139) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:14139) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

现在将代码更改为:

var electron = require("electron")

var registerTestHandler = function(ipc) {
    ipc.on("test", console.log)
}

electron.app.whenReady()
.then(function() {
    registerTestHandler(electron.ipcMain)
})

而且效果很好!

我认为这与电子无关,而与javascript有关。

但是我无法解释为什么会这样。

有人有想法吗?

回答如下:

我在电子github页面上打开了issue,并得到以下解释:

这只是javascript的工作方式,当您从ipcMain中剥离on方法时,将来在调用该方法时将删除此范围。即,如果您将其明确绑定到ipcMain,它将可以正常工作

electron.ipcMain的怪异行为

这比电子问题更像是JavaScript。

我有以下代码:

var electron = require("electron")

var registerTestHandler = function(onFn) {
    onFn("test", console.log)
}

electron.app.whenReady()
.then(function() {
    registerTestHandler(electron.ipcMain.on)
})

运行应用程序会引发以下错误:

(node:14139) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_events' of undefined
    at _addListener (events.js:228:19)
    at addListener (events.js:284:10)
    at registerTestHandler (/Users/marco/Desktop/electron-ipc-test/index.js:4:2)
    at /Users/marco/Desktop/electron-ipc-test/index.js:9:2
(node:14139) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_events' of undefined
    at _addListener (events.js:228:19)
    at addListener (events.js:284:10)
    at registerTestHandler (/Users/marco/Desktop/electron-ipc-test/index.js:4:2)
    at /Users/marco/Desktop/electron-ipc-test/index.js:9:2
(node:14139) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14139) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14139) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:14139) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

现在将代码更改为:

var electron = require("electron")

var registerTestHandler = function(ipc) {
    ipc.on("test", console.log)
}

electron.app.whenReady()
.then(function() {
    registerTestHandler(electron.ipcMain)
})

而且效果很好!

我认为这与电子无关,而与javascript有关。

但是我无法解释为什么会这样。

有人有想法吗?

回答如下:

我在电子github页面上打开了issue,并得到以下解释:

这只是javascript的工作方式,当您从ipcMain中剥离on方法时,将来在调用该方法时将删除此范围。即,如果您将其明确绑定到ipcMain,它将可以正常工作