最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using ipc in Electron to set global variable from renderer - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Apr 8, 2018 at 8:30 Ian W asked Apr 8, 2018 at 8:16 Ian WIan W 9904 gold badges12 silver badges30 bronze badges 2
  • Are you sure you absolutely need these variables to be global? Normally you could do all this with events since its a form of message passing which is an alternative to sharing memory through these global variables, so maybe find a neater, better architected solution. – Rami Awar Commented Apr 9, 2018 at 17:54
  • @T Mack: Did you ever come up with a good solution for this that doesn't rely on getGlobal and the issue of not being able to actually change the Global values? – Joshua Pinter Commented Sep 4, 2018 at 2:34
Add a comment  | 

2 Answers 2

Reset to default 14

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!"

Little late to answer but hopefully this will help our future visitors. So, based on the following IPC approach, I was able to create, access and update the value of global variable :

1) Add this code in the main.js file :

   global.MyGlobalObject = {
      variable_1: '12345'
   }

2) Use this on your 1st page to update global variable value :

require('electron').remote.getGlobal('MyGlobalObject').variable_1= '4567'

3) Lastly, use something like this on your 2nd page where you'll access the modified global variable and print it :

console.log(require('electron').remote.getGlobal('MyGlobalObject').variable_1)

You can find the same thing in electron's documentation.

发布评论

评论列表(0)

  1. 暂无评论