两个 windows 之间的 electron + react + redux 通信冻结了 PC?
electron + react + redux communication between two windows freezes PC?
我正在尝试更新另一个 window 中的状态,这取决于第一个 window 中的状态变化,但 ipc 通信对我来说没有按预期工作。
在我的第一个 window 中,我有:
onStatsSelect(event, menuItem, index) {
const selectedStatsCopy = this.state.selectedStats.slice();
const itemIndex = selectedStatsCopy.indexOf(menuItem.props.primaryText);
if (itemIndex > -1) {
delete selectedStatsCopy[itemIndex];
} else {
selectedStatsCopy[index] = menuItem.props.primaryText;
}
// Update state
this.setState({ selectedStats: selectedStatsCopy });
// Notify worker window of change
if (!(this.props.isReport)) {
console.log('in renderer main window');
ipcRenderer.send("updateSelectedStatsMain", event, selectedStatsCopy);
}
}
这是更新 selectedStats 状态的回调。如最后几行所示,它还将通知工作人员 window 此更新。 if (!(this.props.isReport))
这是一个重要的检查,因为 windows 共享相同的组件,所以我使用 属性 isReport
来区分两者。
在我的主要进程中,我有:
// Selected Stats have changed in main window
ipcMain.on('updateSelectedStatsMain', (event, selectedStatsCopy) => {
console.log('in main');
// Send to worker window
workerWindow.webContents.send('updateSelectedStatsRen', event, selectedStatsCopy);
});
此代码将使用新状态 selectedStatsCopy
与工作人员 window 通信。
在我的 comoponentDidMount
中,我有:
componentDidMount() {
// Register listening to message in case this is the worker window
if (this.props.isReport) {
console.log('in renderer worker window');
ipcRenderer.on("updateSelectedStatsRen", (event, selectedStatsCopy) => {
console.log('in renderer worker window event');
this.setState({ selectedStats: selectedStatsCopy });
});
}
}
这应该可以工作,但 electron 在 ipcRenderer.send("updateSelectedStatsMain", event, selectedStatsCopy);
行挂起,导致 main window 挂起一段时间,它会继续使用资源,直到 PC 死机。
这里有什么问题?
我弄清楚了错误,但我仍然不知道为什么它会冻结电子。
基本上我在做
ipcRenderer.send("updateSelectedStatsMain", event, selectedStatsCopy);
这完全没有意义,因为我将事件作为参数传递。我什至没有要传递的事件变量。
正在更新:
ipcRenderer.send("updateSelectedStatsMain",event, selectedStatsCopy);
对此:
ipcRenderer.send("updateSelectedStatsMain", selectedStatsCopy);
还有这个:
workerWindow.webContents.send('updateSelectedStatsRen', event, selectedStatsCopy);
对此:
workerWindow.webContents.send('updateSelectedStatsRen', selectedStatsCopy);
为我解决了这个问题。
我正在尝试更新另一个 window 中的状态,这取决于第一个 window 中的状态变化,但 ipc 通信对我来说没有按预期工作。
在我的第一个 window 中,我有:
onStatsSelect(event, menuItem, index) {
const selectedStatsCopy = this.state.selectedStats.slice();
const itemIndex = selectedStatsCopy.indexOf(menuItem.props.primaryText);
if (itemIndex > -1) {
delete selectedStatsCopy[itemIndex];
} else {
selectedStatsCopy[index] = menuItem.props.primaryText;
}
// Update state
this.setState({ selectedStats: selectedStatsCopy });
// Notify worker window of change
if (!(this.props.isReport)) {
console.log('in renderer main window');
ipcRenderer.send("updateSelectedStatsMain", event, selectedStatsCopy);
}
}
这是更新 selectedStats 状态的回调。如最后几行所示,它还将通知工作人员 window 此更新。 if (!(this.props.isReport))
这是一个重要的检查,因为 windows 共享相同的组件,所以我使用 属性 isReport
来区分两者。
在我的主要进程中,我有:
// Selected Stats have changed in main window
ipcMain.on('updateSelectedStatsMain', (event, selectedStatsCopy) => {
console.log('in main');
// Send to worker window
workerWindow.webContents.send('updateSelectedStatsRen', event, selectedStatsCopy);
});
此代码将使用新状态 selectedStatsCopy
与工作人员 window 通信。
在我的 comoponentDidMount
中,我有:
componentDidMount() {
// Register listening to message in case this is the worker window
if (this.props.isReport) {
console.log('in renderer worker window');
ipcRenderer.on("updateSelectedStatsRen", (event, selectedStatsCopy) => {
console.log('in renderer worker window event');
this.setState({ selectedStats: selectedStatsCopy });
});
}
}
这应该可以工作,但 electron 在 ipcRenderer.send("updateSelectedStatsMain", event, selectedStatsCopy);
行挂起,导致 main window 挂起一段时间,它会继续使用资源,直到 PC 死机。
这里有什么问题?
我弄清楚了错误,但我仍然不知道为什么它会冻结电子。
基本上我在做
ipcRenderer.send("updateSelectedStatsMain", event, selectedStatsCopy);
这完全没有意义,因为我将事件作为参数传递。我什至没有要传递的事件变量。
正在更新:
ipcRenderer.send("updateSelectedStatsMain",event, selectedStatsCopy);
对此:
ipcRenderer.send("updateSelectedStatsMain", selectedStatsCopy);
还有这个:
workerWindow.webContents.send('updateSelectedStatsRen', event, selectedStatsCopy);
对此:
workerWindow.webContents.send('updateSelectedStatsRen', selectedStatsCopy);
为我解决了这个问题。