sindresorhus / electron-better-ipc

Simplified IPC communication for Electron apps
MIT License
715 stars 60 forks source link

what's the difference with `invoke handle` in Electron v7 #25

Closed Ryeonnie closed 4 years ago

Ryeonnie commented 4 years ago

I am considering whether it is better to switch to new API in electron v7 or not ?

Laruxo commented 4 years ago

ipcRenderer.sendSync() is syncronous and as written in documentation will block the process until response is received.

Note: Sending a synchronous message will block the whole renderer process, unless you know what you are doing you should never use it.

I see no benefit of using it instead of this package.

Ryeonnie commented 4 years ago

My mistake, let me explain..

main --> renderer

main process
win.webContents.send('main-process-message', false);
renderer process
ipcRenderer.on("main-process-message", (event, arg) => {
        state.isActivated = arg;
});

renderer --> main

What I want to talk about is handle and invoke, which are new added features in electron v7.0.0: Release Notes

Related article: Electron’s ‘remote’ module considered harmful

If electron-better-ipc is based old electron apis, it'd better to use original new apis for maximizing performance. And as you can see, the code is neat, clean and dead simple.

main process

ipcMain.handle("windowOperator",async (event, ...args) => {})
renderer process
await ipcRenderer.invoke("renderer-process-message", argument);
Laruxo commented 4 years ago

That is a good point, I just discovered this API. I tried using it instead of this module in my current project, seems like it works the same way.