'Electron - Menu template on main, calling function, requires information from renderer
On the main Electron process, I have a menu template. On that menu template, I have a close function. My application is using a database to store information. On the close function, I would like call the renderer to get the current id of the record that is open, so that I can save it, before I close the database. I am wondering if there is a way to call a function on the main process and return a id value from the renderer process. Only way I can determine to do this is the following BrowserWindow.getFocusedWindow().webcontents.send("closing"), then do ipcRenderer.send back to main with id. Seems like there should an easier way to do this.
Thanks
Solution 1:[1]
Unfortunately, at this moment in time, there is nothing similar to the ipcRenderer.invoke(channel, ...args) for the main thread.
As you indicated, you would need to use the app.on('before-quit', () => {...} ); method and within it, poll the render thread for the current id and await its return.
Alternatively, to keep your render thread free and responsive, you could manage the current id from within your main thread, so there is no need to call the render thread prior to closing. This is what I do. All the heavy lifting is done in the main thread (such as DB calls, file system reads / writes, API calls) and the render thread just reflects the state via IPC messages / data updates.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | midnight-coding |
