redux-saga如何监听多个eventChannel

问题描述 投票:1回答:1

我正在为聊天室订阅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');
  }
}
redux-saga
1个回答
0
投票

为什么不将观察者创建为单独的函数并将其调用两次。例如:

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.