3

我正在使用具有非常特定 API 的 WebRTC 库。该peerConnection.setRemoteDescription方法的第二个参数应该是完成设置远程描述时的回调:

这是我的 WebRTC 类的包装函数之一:

export function setRemoteSdp(peerConnection, sdp, callback) {
  if (!sdp) return;
  return peerConnection.setRemoteDescription(
    new RTCSessionDescription(sdp),
    callback, // <-------------
  );
}

这是我想做的草图:

function receivedSdp(action$, store) {
  return action$.ofType(VideoStream.RECEIVED_SDP)
    .mergeMap(action => {
      const {peerConnection} = store.getState().videoStreams;
      const {sdp} = action.payload;

      return WebRTC.setRemoteSdp(peerConnection, sdp, () => {
        return myReducer.myAction(); // <------ return action as the callback
      })
    })
};

这不起作用,因为我没有返回 Observable。有没有办法做到这一点?

PS这是WebRTC API:https ://github.com/oney/react-native-webrtc/blob/master/RTCPeerConnection.js#L176

4

2 回答 2

4

马丁的回答是正确的使用Observable.createor new Observable--same 东西(除了我不清楚为什么你需要,mergeAll()因为mergeMap会变平?)

作为奖励,您也可以使用Observable.bindCallback它。

// bindCallback is a factory factory, it creates a function that
// when called with any arguments will return an Observable that
// wraps setRemoteSdp, handling the callback portion for you.
// I'm using setRemoteSdp.bind(WebRTC) because I don't know
// if setRemoteSdp requires its calling context to be WebRTC
// so it's "just in case". It might not be needed.
const setRemoteSdpObservable = Observable.bindCallback(WebRTC.setRemoteSdp.bind(WebRTC));

setRemoteSdpObservable(peerConnection, sdp)
  .subscribe(d => console.log(d));

你的史诗中的用法是这样的

// observables are lazy, so defining this outside of our epic
// is totally cool--it only sets up the factory
const setRemoteSdpObservable = Observable.bindCallback(WebRTC.setRemoteSdp.bind(WebRTC));

function receivedSdp(action$, store) {
  return action$.ofType(VideoStream.RECEIVED_SDP)
    .mergeMap(action => {
      const {peerConnection} = store.getState().videoStreams;
      const {sdp} = action.payload;

      return setRemoteSdpObservable(peerConnection)
        .map(result => myReducer.myAction());
    })
};

您可以使用它为所有 WebRTC api 创建 Observable 包装器。

于 2017-05-09T17:35:04.517 回答
3

所以问题是它setRemoteSdp不返回一个 Observable 而myReducer.myAction()它是你想要合并的 Observable?

您可以使用Observable.create和包装WebRTC.setRemoteSdp调用:

.mergeMap(action => {
  return Observable.create(observer => {
    WebRTC.setRemoteSdp(peerConnection, sdp, () => {
      observer.next(myReducer.myAction());
      observer.complete();
    })
  });
}
.mergeAll()

Observable.create返回一个从 发射另一个 Observable 的Observable myReducer.myAction()。现在我实际上有所谓的高阶,我想使用mergeAll()(concatAll也可以) 进行展平。

于 2017-05-09T06:34:15.140 回答