Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ipc in Electron to set global variable from renderer

renderer.js

ipcRenderer.sendSync('setGlobal', 'globalVarName').varInner.varInner2 = 'result';

main.js

global.globalVarName = {
  varInner: {
    varInner2: ''
  },
  iWontChange: 'hi'
};

ipcMain.on('setGlobal', (event, arg) => {
  console.log(arg) // should print "result"
  // what goes here?
})

console.log(varInner2) // should print "result"

Is something like this possible, namely setting the varInner2 of globalVarName in this manner? Secondly, is there a way to optimize this so we wouldn't have to rewrite this process for every global variable (i.e. some way to do this with dynamic variable names)?

I appreciate any ideas or solutions, sorry if this is a common sense question.

like image 748
T Mack Avatar asked Apr 08 '18 08:04

T Mack


People also ask

How does IPC work in Electron?

IPC channels​ In Electron, processes communicate by passing messages through developer-defined "channels" with the ipcMain and ipcRenderer modules. These channels are arbitrary (you can name them anything you want) and bidirectional (you can use the same channel name for both modules).

What is IPC renderer?

The ipcRenderer module is an EventEmitter. It provides a few methods so you can send synchronous and asynchronous messages from the render process (web page) to the main process. You can also receive replies from the main process. See IPC tutorial for code examples.

How do I declare a global variable in node JS?

To set up a global variable, we need to create it on the global object. The global object is what gives us the scope of the entire project, rather than just the file (module) the variable was created in. In the code block below, we create a global variable called globalString and we give it a value.

What is ipcMain in Electron JS?

The ipcMain module is an Event Emitter. When used in the main process, it handles asynchronous and synchronous messages sent from a renderer process (web page). Messages sent from a renderer will be emitted to this module. For usage examples, check out the IPC tutorial.


1 Answers

Use IPC to Set the Global's Value.

Using getGlobal works great when you're only interested in reading the value of the global variable. However, I found that trying to assign or change its value using getGlobal to be problematic.

In my case, I found that the global variable on the Main process didn't actual change. Specifically, when refreshing the Electron window in development, the global variables were set back to their original value. This made restoring state in development an issue.

Not sure if this also was occurring in production, but I imagine it would, so spinning up new processes that relied on up-to-date values of global variables would be problematic.

Instead, I ended up using the more verbose method of ipcMain and ipcRenderer.

main.js

const { ipcMain } = require( "electron" );

ipcMain.on( "setMyGlobalVariable", ( event, myGlobalVariableValue ) => {
  global.myGlobalVariable = myGlobalVariableValue;
} );

renderer.js

const { ipcRenderer, remote } = require( "electron" );

// Set MyGlobalVariable.
ipcRenderer.send( "setMyGlobalVariable", "Hi There!" );

// Read MyGlobalVariable.
remote.getGlobal( "MyGlobalVariable" ); // => "Hi There!"
like image 53
Joshua Pinter Avatar answered Sep 17 '22 21:09

Joshua Pinter