Javascript get result from emit event

Viewed 60

I'm writing an app for electron.js using this structure to communicate between the main and the render process.

On main process I have established a context bridge like this: preload.js (main process = electron/node.js):

 const {ipcRenderer, contextBridge} = require("electron");
 contextBridge.exposeInMainWorld("electron", {
        ipcRenderer_invoke: (channel, ...params) => ipcRenderer.invoke(channel, ...params)});

main.js (main process = electron/node.js):

const {ipcMain} =require ("electron");
ipcMain.handle('test', async (event,a) => {return a*5;});

browser.js (render process = browser)

var number= await window.electron.ipcRenderer_invoke("test",12);
console.log(number); // returns 60

This works all fine, but I now want everything adapt for cordova, which means I will browserify the necessary modules and run all code inside of the browser/ web kit. The problem is that I don't know how I can search and replace the handle from main.js and search and replace the invoke from browser.js in pure browser javascript (or with a library) so that I can leave my code structure the same. I have already tried events, but these don't give me a return value.They return true instead of the expected value 60 from the example above.

0 Answers
Related