Josh Branchaud wrote a small file detailing some bit of programming knowledge he learned every day for 328 days. I really like this idea and I think I might just do it. I certainly won’t be posting as often as he is – I don’t learn quickly enough – but it’d be nice to be able to look back on everything I’ve learned in one year.
So what did I learn today?
Electron has two processes – a main and a renderer. Both have access to node.js but almost all of your code should sit in the renderer. This is not what I expected. I assumed my code would go in main.js, but apparently this is really only for very low level stuff like manipulating the window and menu. Even database / file access code belongs in the renderer!
Differences Between Main Process and Renderer Process
The main process creates web pages by creating
BrowserWindowinstance runs the web page in its own renderer process. When a
BrowserWindowinstance is destroyed, the corresponding renderer process is also terminated.
The main process manages all web pages and their corresponding renderer processes. Each renderer process is isolated and only cares about the web page running in it.
In web pages, calling native GUI related APIs is not allowed because managing native GUI resources in web pages is very dangerous and it is easy to leak resources. If you want to perform GUI operations in a web page, the renderer process of the web page must communicate with the main process to request that the main process perform those operations.
Note: If I do start a series of TIL’s I’ll probably follow Josh’s example and stick it on github or somewhere. I’ll keep it out of the blog.