首页 > 解决方案 > 如何根据条件逻辑触发 RxJS 中的操作?

问题描述

我正在尝试做什么 - 我需要在触发 api 调用(getterFunc)之前添加条件检查,它检查数据是否已经存在于存储中,如果没有,则从后端获取数据。但如果可用,则模拟成功事件并在有效负载中提供存储数据。你能帮我解决我在这个实现中做错了什么吗

const getTemplateEpic = (getterFunc, initial, success, cancel, failure) => (action$, store) => action$.pipe(
  ofType(initial),
  mergeMap(action => iif(() => Object.keys(store.value.getIn(['whatsApp'].templateListData)).length,
    of({
      type: success,
      payload: { res: store.value.getIn(['whatsApp'].templateListData) }
    }),
    getterFunc('GET', action.payload)
  ).pipe(
    map(response => ({
      type: success,
      payload: { res: response.response },
    })),
    takeUntil(action$.pipe(ofType(cancel))),
    catchError(error => of({
      type: failure,
      payload: { res: error },
    })
    )
  )
  )
);

标签: reactjsrxjsrxjs-observables

解决方案


推荐阅读