Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果渲染器进程关闭,是否会收集全局变量垃圾?_Javascript_Html_Garbage Collection_Global Variables_Electron - Fatal编程技术网

Javascript 如果渲染器进程关闭,是否会收集全局变量垃圾?

Javascript 如果渲染器进程关闭,是否会收集全局变量垃圾?,javascript,html,garbage-collection,global-variables,electron,Javascript,Html,Garbage Collection,Global Variables,Electron,在Electron中,我有一个打开浏览器窗口的主进程。BrowserWindow加载一个html页面,然后同一个窗口最终加载另一个html页面 main.js var mainWindow; global.mainState = { settings: {} } mainWindow = createWindow('main', { width: 1000, height: 800, }); if (curState == 'load') { mainWindow.loadURL

在Electron中,我有一个打开浏览器窗口的主进程。BrowserWindow加载一个html页面,然后同一个窗口最终加载另一个html页面

main.js

var mainWindow;
global.mainState = {
  settings: {}
}
mainWindow = createWindow('main', {
  width: 1000,
  height: 800,
});
if (curState == 'load') {
  mainWindow.loadURL(`file://${__dirname}/interface/load.html`, {})
}
if (curState == 'login') {
  mainWindow.loadURL(`file://${__dirname}/interface/login.html`, {})
}
load.html

const remote = require('electron').remote;
var testGlobal = remote.getGlobal('mainState')
testGlobal.settings = 'test value'
testGlobal.settings.inner = 'test value2'

当main.js加载第二个页面(login.html)时,全局变量是否会被删除/取消引用?文档中说,如果渲染器进程取消引用一个全局变量,那么该变量将是gc'd。当我尝试测试这一点时,我得到了不一致的结果,我只想从比我更聪明的人那里得到一些解释。

testGlobal
将被垃圾收集,因为站点发生了变化
global.mainState
不会被删除,但是当您调用
testGlobal.settings='testvalue'
时,它也不会更改,因为
remote.getGlobal()
只会给您一个
mainState
的副本,而不是一个引用

我建议您自己使用和同步全局变量。

使用IPC设置全局变量的值。 @RoyalBingBong的正确之处在于“
remote.getGlobal()
只提供了
mainState
的副本,而不是引用。”

看起来您正在更改全局变量的值,但实际上并非如此。因此,例如,当我刷新我的电子浏览器窗口时,该全局变量的值将恢复为原来的值

我发现正确更改全局变量值的唯一方法是使用
ipcMain
ipcrender
(详细方式)

main.js

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

ipcMain.on( "setMyGlobalVariable", ( event, myGlobalVariable ) => {
  global.myGlobalVariable = myGlobalVariable;
} );
const { ipcRenderer, remote } = require( "electron" );

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

// Read MyGlobalVariable.
remote.getGlobal( "MyGlobalVariable" ); // => "Hi There!"
renderer.js

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

ipcMain.on( "setMyGlobalVariable", ( event, myGlobalVariable ) => {
  global.myGlobalVariable = myGlobalVariable;
} );
const { ipcRenderer, remote } = require( "electron" );

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

// Read MyGlobalVariable.
remote.getGlobal( "MyGlobalVariable" ); // => "Hi There!"
现在,我可以刷新我的Electron窗口或旋转一个新的渲染器进程,全局变量的值将正确地设置为我设置的值,在本例中,“您好!”


您还可以看到,为了阅读它,我使用了更简单的
remote.getGlobal
。我还没有测试当值发生变化并且该值没有得到更新时,这是否会导致问题。如果是这样的话,我可能不得不回到这个答案,并使用另一个
ipcMain
ipcrender
手动读取全局变量。

感谢您提供了有用的答案。我开始使用你的想法,但在同步变量时遇到了麻烦。现在我注意到,如果我从渲染器向main发送一条消息来更改变量,那么在执行渲染器中的下一行时,该变量不会更改。我如何同步这两个函数以使值保持一致—这正是javascript中异步函数的工作方式。您可以使用
ipcRenderer.sendSync
,但这将完全阻止渲染器。