rxjs - Redux Observable 史诗中的 IF

标签 rxjs observable redux-observable

我有一个史诗,可以捕获每次获取状态的调度(只是来自状态的项目,例如 state.process:{ status: failed, success, inWork},而不是像 200、500 等这样的请求状态)。 当状态==成功时(通过从状态获取状态)我需要调度另一个操作,例如SET_STATUS_SUCCESS

const getStatus = (action, state) =>
    action.pipe(
        ofType(GET_STATUS),
        withLatestFrom(state),
        mergeMap(([action, state]) => {
            const { status } = state.api.process; //here is what i need, there is no problem with status.
            if (status === "success") {
              return mapTo(SET_STATUS_SUCCESS) //got nothing and error.
}
        })
    );

现在我收到错误:

Uncaught TypeError: You provided 'function (source) { return source.lift(new MapToOperator(value)); }' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. at subscribeTo (subscribeTo.js:41)

我该怎么办?我尝试只返回 setStatusSuccess 操作,但它也不起作用。

最佳答案

您需要从传递给 mergeMap 的函数返回一个可观察值。试试这个:

const getStatus = (action, state) =>
  action.pipe(
    ofType(GET_STATUS),
    withLatestFrom(state),
    mergeMap(([action, state]) => {
      const { status } = state.api.process;

      if (status === 'success') {
        return of({ type: SET_STATUS_SUCCESS });
      } else {
        return EMPTY;
      }
    }),
  );

ofEMPTY是从 rxjs 进口的.

关于rxjs - Redux Observable 史诗中的 IF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53374557/

相关文章:

javascript - SnapshotChanges 订阅未检测到子集合项目的最终删除

angular - map 运算符调用的方法中的可观察范围变化

java - 当每个元素上的操作产生不同的结果计数/持续时间时,保持初始可迭代排序顺序稳定

javascript - Redux-Observable 中具有依赖关系的多个操作

rxjs5 - RxJS 5 - 函数被调用两次

rxjs - rxjs 中的 Bacon.Bus.plug 相当于什么

typescript - Angular 2 : switchMap not canceling previous http calls

angular - 将数据从一个可观察对象传递到另一个可观察对象的正确方法

http - Angular 2 - 可观察和异步 http 加载

javascript - 为什么在 Redux-Saga 上使用 Redux-Observable?