2017-12-28 29 views
0

チャットルームの2つのチャンネルを購読しています。 2番目のチャンネルが新しいメッセージを受信するまで、コードはCに固定されます。そして、最初のチャンネルが新しいメッセージを受信するまで、Aで立ち往生してください。redux-saga複数のイベントチャンネルを聞く方法

どうすれば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'); 
    } 
} 

答えて

0

ウォッチャを別の関数として作成し、それを2回呼び出すことをお勧めします。例:

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); 
} 
関連する問題