我正在为聊天室订阅2个频道。代码将停留在C
,直到第二个频道收到新消息。然后停留在A
,直到第一个频道收到新消息。
如何使2通道单独工作。
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);
}