如果渲染器进程关闭,将收集电子全局变量垃圾

Electron global variable garbage collected if renderer process closes?(如果渲染器进程关闭,将收集电子全局变量垃圾?)
本文介绍了如果渲染器进程关闭,将收集电子全局变量垃圾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Electron 中,我的主要进程打开了一个 BrowserWindow.BrowserWindow 会加载一个 html 页面,然后同一个窗口最终会加载另一个 html 页面.

In Electron, I have my main process opening a BrowserWindow. The BrowserWindow loads one html page and then the same window eventually loads another html page.

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`, {})
}

加载.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.当我尝试对此进行测试时,我得到了不一致的结果,我只想从比我更聪明的人那里得到一些解释.

When main.js loads the second page (login.html), will the global variable be deleted/dereferenced? The docs say that if the renderer process dereferences a global variable then the variable will be gc'd. When I try to test this I get inconsistent results and I would just like some explanation from someone more wise than I.

推荐答案

testGlobal 将被垃圾回收,因为站点发生了变化.global.mainState 不会被删除,但是当你调用 testGlobal.settings = 'test value' 时它也不会改变,因为 remote.getGlobal() 只是为您提供 mainState 的副本,而不是参考.

testGlobal will be garbage collected, since the site changes. global.mainState will not be deleted, however it will also not change when you call testGlobal.settings = 'test value', because remote.getGlobal() just gives you a copy of mainState and not a reference.

我建议你使用 ipcMain 和 ipcRenderer 到自己同步全局变量.

I would suggest you use ipcMain and ipcRenderer to sync the global variable yourself.

这篇关于如果渲染器进程关闭,将收集电子全局变量垃圾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How do I define global variables in CoffeeScript?(如何在 CoffeeScript 中定义全局变量?)
Is there a spec that the id of elements should be made global variable?(是否有规范将元素的 id 设为全局变量?)
How do I get fetch result from API to store as a global variable?(如何从API获取FETCH结果以存储为全局变量?)
What#39;s the difference between a global var and a window.variable in javascript?(javascript中的全局变量和window.variable有什么区别?)
How to share a global variable between test files from a test in TestCafe?(如何在 TestCafe 中的测试中共享测试文件之间的全局变量?)
What are some of the problems of quot;Implied Global variablesquot;?(“隐含全局变量有哪些问题?)