Redux Observable:如何从回调中返回一个动作? [英] Redux Observable: How to return an action from a callback?

查看:38
本文介绍了Redux Observable:如何从回调中返回一个动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the WebRTC library which has a very specific API. The peerConnection.setRemoteDescription method's 2nd argument is supposed to be a callback for when it finishes setting the remote description:

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

This is one of my wrapper functions for my WebRTC class:

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.有没有办法做到这一点?

This doesn't work since I'm not returning an Observable. Is there a way to do this?

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

P.S. this is the WebRTC API: https://github.com/oney/react-native-webrtc/blob/master/RTCPeerConnection.js#L176

推荐答案

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

So the problem is that setRemoteSdp doesn't return an Observable while myReducer.myAction() does and that's the Observable you want to merge?

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

You can use Observable.create and wrap the WebRTC.setRemoteSdp call:

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

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

The Observable.create returns an Observable that emits another Observable from myReducer.myAction(). Now I have in fact so-called higher-order that I want to flatten using mergeAll() (concatAll would work as well).

这篇关于Redux Observable:如何从回调中返回一个动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆