问题
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