preface

The development of the rendering process becomes very simple with Electron. Most of the styling and business logic can be implemented using familiar front-end technologies. But inevitably there will be times when the renderer communicates with the main process. Today, we are going to talk about the main and render processes.

What is the Main Process

Typically, scripts run in the main process control the life cycle of the application, display the graphical user interface and its elements, perform native operating system interactions, and create renderers in web pages. The Electron application can have only one main process.

In short, the main process controls the entire application carrier and all renderers, so if you want to perform application closure, minimization, window topping, etc., you need to do this in the main process.

What is a Renderer Process

The renderer is our application window. Multiple browser Windows can be created, each using its own separate rendering process. The renderer process is where we usually do front-end development

The main process communicates with the renderer

The Electron module is divided into three types: Main Process module, Renderer Process module and both processes available module.

Event listeners

Module methods in the main thread that the renderer cannot call directly. Therefore, the most common method is to listen for events through the ipcMain module in the main process

Events are sent in the renderer process through the send method in ipcRenderer

This allows the renderer to call methods in the main process.

Module calls

There is also a case where we can’t handle communication with event listeners alone, we want to operate directly from the renderer to the main process module. Let’s say one scenario, cookies.

Developing applications with Electron can no longer use JS-cookies for cookie operations. The main process has a module called session through which cookies can be manipulated.

An HTTP request is made in the render process to get, set, and remove cookies. Do I have to put event listeners in the main process and event listeners in the renderer to call back and forth? That is absolutely anti-human.

Fortunately, Electron has a remote module. The main process module can be used in the renderer process. This provides an easy way for the renderer to communicate with the main process. Before using the Remote module, you need to enable the Remote module by setting enableRemoteModule in webPreferences to true.

And then we can go through

let session = require('electron').remote.session;
Copy the code

I got the session module. The specific operation of cookies and what fields cookie objects have can be found in the official website documents.

conclusion

Above, the renderer process calls the main process. The main process can also call the renderer process. The same variety of ways, how to operate can refer to the official website. P.S. is best to check the document on its website, where other documents may not be known. “Said one victim who read the Chinese document online and didn’t know when callback was changed to promise.