Emit custom events in electron app from main to renderer

拈花ヽ惹草 提交于 2021-01-27 04:47:33

问题


So I know this works because I tried it, but it's not documented anywhere so I'm asking if it's OK to use this practice, and not worry that it would stop working in the future (Electron and nodejs are known to break things from one version to another)

This is the type of practice I'm talking about:

main.js

app.emit('did-something', param1, param2);

renderer.js (browser window)

const {app} = require('electron').remote;

app.on('did-something', (param1, param2) => {
  $('#whatever').text(param1);
});

Essentially I'm trying to move all the code that doesn't deal with HTML directly, like database interactions, into main.js and I want to make sure this is the right way to do it.

Also, is it ok if I extend the app object with my own methods and properties?


回答1:


The main process should almost always only be used for creating BrowserWindows and for accessing electron APIs which are marked in the docs as only accessible via the main process.

Check out this article for more details of the differences between the main/renderer and what they are used for. The Chromium process architecture means that any blocking code in the main process will block the renderers too.

All your app code should be in render processes and if you're executing long-running processes these should be bumped into Web Workers or other renderer processes. electron-remote can help you do this.

If you want to communicate between the main and renderer processes you should use the documented API's.



来源:https://stackoverflow.com/questions/48284207/emit-custom-events-in-electron-app-from-main-to-renderer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!