Странное поведение Electron.ipcMain - PullRequest
1 голос
/ 29 апреля 2020

Ну, это скорее 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.

Однако я не могу объяснить, почему это происходит.

Кто-нибудь есть идея?

1 Ответ

0 голосов
/ 29 апреля 2020

Я открыл вопрос на странице электронного github и получил следующее объяснение:

Так работает javascript, когда вы отключаете метод on из ipcMain вы удаляете область действия this при вызове этого метода в будущем. т.е. если вы явно привязываете его к ipcMain, он будет работать

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...