在 Electron 中简化 IPC 的工具

tools to simplify IPC in Electron

他们是否有任何工具可以简化 IPC 并在单个网页和 node.js 进程之间进行数据编组?

我已阅读:http://tangiblejs.com/posts/nw-js-and-electron-compared-2016-edition

其中概述了 nw.js 和电子之间的区别。两者看起来几乎相同,但我喜欢在不需要时尽量减少复杂性,所以我倾向于 nw.js 来避免 IPC 问题。

但这可能是个错误,因为我看到这个组中关于电子的评论比 NW.JS

多 10 倍

(我的 IDE 将是 Visual Studio 代码,现在有 NW.JS 调试的扩展名,但 none 用于 Electron。

出于这个原因,我们开始使用 NWJS,也因为它支持 chrome.serial。最近我将项目转换为电子项目有几个原因:

你是对的,NWJS 没有 main/render 过程的复杂性,但我发现很少有理由必须处理 IPC。

许多 API 仅在 main 进程中可用,但可以通过 remote API 访问。因此,例如,要从 render 进程访问 main process.argv,我使用:

{process} = require('electron').remote
process.argv ...

在我的 index.js 中,我不得不做一些 IPC 事情,但是 electron 有库来简化这个:

// ensure we only have a single instance, but pass args to renderer to open any files passed by explorer
const shouldQuit = app.makeSingleInstance((argv, workingDir) => {
  win.webContents.send('open-instance', argv);
})

然后在我的渲染器代码中某处我有以下内容:

ipcRenderer.on('open-instance', (event, arg) => {
  this.restoreAndFocus();
  // TODO - handle another instance opening a file from explorer
});