gpt4 book ai didi

redux-saga 如何监听多个 eventChannel

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

我订阅了聊天室的 2 个 channel 。代码会卡在 C直到第二个 channel 收到新消息。然后卡在 A直到第一个 channel 收到新消息。

我怎样才能让 2 个 channel 分开工作。

function* ChatRoomPageEnter() {
const chanChat = yield call($stomp.eventChannel, 'ChatRoomPage', '/topic/chat');
const chanEcho = yield call($stomp.eventChannel, 'ChatRoomPage', '/user/queue/echo');

while (true) { // eslint-disable-line no-constant-condition
console.log('A');
const messageChat = yield take(chanChat);
console.log('B');
yield put({ type: 'chatroom/newMessage', payload: messageChat });
console.log('C'); // <----
const messageEcho = yield take(chanEcho);
console.log('D');
yield put({ type: 'chatroom/newMessage', payload: messageEcho });
console.log('E');
}
}

最佳答案

为什么不创建一个观察者作为一个单独的函数并调用它两次。例如:

function * chatWatcher(chanName) {
while(true) {
const message = yield take(chanName);
yield put({ type: 'chatroom/newMessage', payload: message });
}
}

function* ChatRoomPageEnter() {
const chanChat = yield call($stomp.eventChannel, 'ChatRoomPage', '/topic/chat');
const chanEcho = yield call($stomp.eventChannel, 'ChatRoomPage', '/user/queue/echo');

yield * chatWatcher(chanChat);
yield * chatWatcher(chanEcho);
}

关于redux-saga 如何监听多个 eventChannel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48008388/

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