【发布时间】:2016-06-29 09:29:45
【问题描述】:
我正在使用 Electron、React 和 Redux 开发一个应用程序。在程序启动时,我在渲染进程和主进程之间进行了几次异步 ipc 调用,并将结果保存在存储中。
// in the main app component
componentWillMount() {
const { store } = this.context;
store.dispatch(fetchOptions());
store.dispatch(fetchRequirements());
store.dispatch(fetchStats());
/* miracle function */
},
// actions.js
export function fetchOptions() {
return dispatch => {
dispatch(requestOptions());
ipcRenderer.on('sendOptions', function(event, arg) {
dispatch(receiveOptions(arg));
});
ipcRenderer.send('requestOptions', '');
};
}
// others accordingly
receiveOptions(arg)、receiveRequirements(arg) 和receiveStats(arg) 是动作创建者,最后reducer 会将响应保存在商店中。
在store.dispatch(fetchStats()) 之后,我想直接调度另一个操作,以根据加载到存储中的值进行一些计算。但是,此操作通常会在来自 ipc 的响应到达之前发送。
我发现this discussion 有类似的问题,但他们使用fetch 而不是ipc 消息进行api 调用,我不知道如何将他们的想法应用于我的问题。
所以这是我的问题:如何让程序在继续之前等待所有渠道的响应?
编辑:当我在 ipc 调用后为调度设置一个长度为 0 的时间时,它至少可以立即响应,但是当响应需要更长的时间时它当然没有帮助。
store.dispatch(fetchOptions());
store.dispatch(fetchRequirements());
store.dispatch(fetchStats());
setTimeout(function() {
store.dispatch(calculateThis());
store.dispatch(calculateThat());
}, 0);
【问题讨论】:
-
获取函数的返回类型是什么?如果它们是承诺,你可以使用Promise.all() 做一些事情——比如派发一个动作——当它们都被解决时。如果他们不返回承诺,也许你可以让他们这样做?
-
获取函数不返回承诺,我真的不知道如何让它们这样做。
标签: javascript reactjs ipc redux electron