gpt4 book ai didi

electron - Aurelia, Electron : Possible EventEmitter memory leak detected

转载 作者:行者123 更新时间:2023-12-03 12:33:06 27 4
gpt4 key购买 nike

我正在使用 Electron 和aurelia作为一个项目来构建系统资源监视器。
Main.js

  var ramInfo = {};
var result = await si.mem()
ramInfo.total = parseInt(result.total / 1024 / 1024);
ramInfo.used = parseInt(result.used / 1024 / 1024);
ramInfo.percentUsed = parseInt((ramInfo.used / ramInfo.total) * 100);
ramInfo.percentAvailable = parseInt((ramInfo.percentUsed - 100) * -1);
event.sender.send('ram-reply', ramInfo);
})
Overview.js:
  async attached () {
await this.getRamInfo();
this.startDataRefresh();

}

async getRamInfo () {
window.ipc.send('ram');
await window.ipc.on('ram-reply', (event, result) => {
this.system.ram = result;
//This line gets logged an additional time each time the setInterval function runs
console.log(this.system.ram);
this.ramData.series = [this.system.ram.percentAvailable, this.system.ram.percentUsed];
new Chartist.Pie('.ram-chart', this.ramData , this.options);
});
console.log("Break");
}

startDataRefresh() {
let scope = this;
setInterval(function() {
scope.getRamInfo();
}, 3000);
}
我在 Electron 控制台中收到以下错误:
MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 ram-reply listeners added to [EventEmitter]. Use emitter.setMaxListeners() to increase limit
我只会认为getRamInfo()函数每三秒钟运行一次,但是,该函数的console.log部分每次运行时都会被记录一次额外的时间。我相当确定这是问题所在,但我不确定为什么每个间隔运行多次。
enter image description here
编辑:
我已经将setInterval函数移到main.js中达到了部分解决方案:
ipcMain.on('ram', async (event) => {
setInterval(async function() {
var ramInfo = {};
var result = await si.mem()
ramInfo.total = parseInt(result.total / 1024 / 1024);
ramInfo.used = parseInt(result.used / 1024 / 1024);
ramInfo.percentUsed = parseInt((ramInfo.used / ramInfo.total) * 100);
ramInfo.percentAvailable = parseInt((ramInfo.percentUsed - 100) * -1);
event.sender.send('ram-reply', ramInfo)
}, 3000);
})
似乎每次向ipcMain调用原始setInterval都会创建一个新的监听器,并且每个监听器每次都返回结果。我希望它依赖于打开的 View ,因此最好通过 View 来控制它。

最佳答案

试试这个:

async getRamInfo () {
window.ipc.send('ram');
return new Promise(resolve => window.ipc.once('ram-reply', (event, result) => resolve(result));
}

async refresh() {
const ramInfo = await this.getRamInfo();
this.ramData.series = [this.system.ram.percentAvailable, this.system.ram.percentUsed];
new Chartist.Pie('.ram-chart', this.ramData , this.options);
// ...
}

startDataRefresh() {
if(!this.interval) {
this.interval = setInterval(() => this.refresh(), 3000);
}
}

stopDataRefresh() {
if(this.interval) {
clearInterval(this.interval);
delete this.interval;
}
}
主要部分- window.ipc.once('ram-reply'-使用 once进行一次性事件订阅

关于electron - Aurelia, Electron : Possible EventEmitter memory leak detected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63179865/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com