2

Can we register the same channel for ipcMain.on method and ipcMain.handle()?

For eg:

ipcMain.handle('test', async(event,args) => {
    let result = await somePromise();
    return result;
});

ipcMain.on('test', async(event,args) => {
    event.returnValue = await somePromise();
});

Will the above code, give the error No handler register for 'test'? if ipcRenderer called this via invoke and sendSync in an order?

for eg:

ipcRenderer.invoke('test', data).then(result => {
    console.log(result);
    return result;
});

someFunction(data) {
    return ipcRenderer.sendSync('test', data);
}
Akash Sethiya
  • 73
  • 1
  • 4

1 Answers1

2

This is one of those things that you can easily test out.

Looking at their code for ipcMain.handle, they store the channel name in an _invokeHandlers map that seems isolated from the rest of the module (meaning from ipcMain.on).

In fact, ipcMain extends an EventEmitter, which is a Node class that maintains its own internal structure for managing events (This is the module where on and once are defined).

So you should be able to safely use both ipcMain.on("test", ...) and ipcMain.handle("test", ...) as long as you trigger them using the appropriate mechanism: send/sendSync corresponds with on/once and invoke corresponds with handle/handleOnce.

pushkin
  • 9,575
  • 15
  • 51
  • 95