我目前在接收从我的主进程到我的渲染器进程(在 Angular 中)的活动窗口的恒定流时遇到一些问题。我知道接收每秒更新会破坏我的应用程序,所以我试图使用 setTimeout 来限制它。但这仍然行不通。我怎样才能解决这个问题?
渲染器 - Angular 中的组件
ipc: IpcRenderer | undefined | null;
constructor(private electronService: ElectronService) { }
ngAfterViewInit(): void {
this.getActiveWindow();
}
getActiveWindow() {
while(this.electronService.isElectronApp) {
this.ipc = this.electronService.ipcRenderer;
this.ipc.send('get-active-window');
this.ipc.on('get-active-window-reply', (_event, reply) => {
console.log(reply);
});
}
}
main.js
const activeWindows = require('electron-active-window');
ipcMain.on('get-active-window', (_event, _arg) => {
let i = 0;
setTimeout(function () {
activeWindows().getActiveWindow().then((result) => {
win.webContents.send("get-active-window-reply", result)
})
}, 10000 * i)
});
到目前为止,我已经尝试了以下方法,但这只会显示一次活动窗口。我想跟踪所有的变化:
渲染器 - Angular 中的组件
ngOnInit(): void {
this.getActiveWindow();
}
getActiveWindow() {
if(this.electronService.isElectronApp) {
this.ipc = this.electronService.ipcRenderer;
this.ipc.send('get-active-window');
this.ipc.on('get-active-window-reply', (_event, reply) => {
console.log(reply);
});
}
}
main.js
ipcMain.on('get-active-window', (_event, _arg) => {
activeWindows().getActiveWindow().then((result) => {
win.webContents.send("get-active-window-reply", result)
});
});