我正在尝试创建一个简单的中间件来处理套接字事件。
const join = (channel) => (dispatch) => {
dispatch({
type: 'ACTION-1',
socketChannel: {...},
events: [...],
});
};我派发这个触发它的操作。现在,当在我的中间件中使用类型‘socketData -2’调用调度方法并将其作为有效负载接收时,我在控制台中看到‘socketData -1’被触发了两次,并且在最后一次它是随我的ACTION有效负载一起提供的。
我想知道为什么注册的是'ACTION-1‘而不是'ACTION-2’,我该如何修复它?我将非常感谢你的帮助。
import { socket } from 'services/socket';
const socketMiddleware = ({ dispatch }) => next => (action) => {
const {
channel,
events, // an array of events for the channel
...rest
} = action;
if (typeof action === 'function' || !channel) {
return next(action);
}
const {
type,
name,
} = channel;
const channelInstance = socket.instance[type](name);
events.forEach((event) => {
const handleEvent = (socketData) => {
dispatch({ type: 'ACTION-2', socketData, ...rest });
};
channelInstance.listen(event.name, handleEvent);
});
return next(action);
};
export {
socketMiddleware
};发布于 2019-03-14 21:45:14
看起来您在初始调度中没有为通道铺设路径,并且在以下情况下,您的中间件在以下情况下无法完成:
if (typeof action === 'function' || !channel) {
return next(action);
}为了解决这个问题,您应该在您的派单中添加通道:
const join = (channel) => (dispatch) => {
dispatch({
type: 'ACTION-1',
socketChannel: {...},
events: [...],
channel: { type: '...', name: '...' }
});
};https://stackoverflow.com/questions/55133758
复制相似问题