Redux Saga:如何在一个嵌套函数中调用yield + put?

时间:2017-02-27 10:42:56

标签: redux redux-saga

我正在使用WebRTC和redux sagas。一个要求是我必须定义peerConnection.onicecandidate

function* createPeerConnection(action) {
  ...
  peerConnection = new RTCPeerConnection(servers);

  peerConnection.onicecandidate = (event) => {
    if (event.candidate) {
      yield put({ type: videoSessionActions.SEND_LOCAL_CANDIDATE, payload: event.candidate });
    }
  }
}

但是,yield put无法使用此方法。我如何改变它以使其适用于传奇?

1 个答案:

答案 0 :(得分:1)

希望活动频道可以帮助您:

import { eventChannel, END } from 'redux-saga';
import { put, take } from 'redux-saga/effects';

import { videoSessionActions } from '../???/constants';

const rtcChannel = servers => eventChannel(emitter => {
  const peerConnection = new RTCPeerConnection(servers);
  peerConnection.onicecandidate = event => {
    if (event.candidate) {
      emitter({type: videoSessionActions.SEND_LOCAL_CANDIDATE, payload: event.candidate});
    }
  };
  return () => {
    peerConnection.close();
  };
}
// ,buffers.expanding(1024) ???
);

function* createPeerConnection(servers) {
  const ch = rtcChannel(servers);

  while(true) { // eslint-disable-line no-constant-condition
    const candidate = yield take(ch);
    yield put(candidate);
  }
}