在redux-observable AJAX史诗中结合rxjs takeUntil,catchError

时间:2018-04-05 22:44:13

标签: rxjs redux-observable

我试图在以下redux-observable史诗中'取消'(takeUntil)AJAX调用,同时保留catchError效果。

const someEpic = action$ =>
  action$.pipe(
    ofType(REQUEST_MAPPED_ESTABLISHMENTS),
    map( action => ( // transform data to url )),
    mergeMap( url  =>
      ajax({ url }).pipe(
        mergeMap( establishments => {
          const { error, data } = establishments.body
          if (error) throw 'some error';
          return of(actions.receiveMappedEstablishments(data))
        }),
        catchError(error =>
          (of(actions.requestMappedEstablishmentsFailure(error)))
        ),
      )
    ),
  )

如何重新构建上述摘录以包含takeUntil,例如,以下内容?

takeUntil(action$.ofType(UI_HAS_CHANGED_SO_NEW_DATA_NOW_REQUIRED)) 

到目前为止,在我的尝试中,我只是成功取消someEpic,例如,将takeUntil插入action$.pipe

1 个答案:

答案 0 :(得分:0)

const someEpic = action$ =>
  action$.pipe(
    ofType(REQUEST_MAPPED_ESTABLISHMENTS),
    map( action => ( // transform data to url )),
    mergeMap( url  =>
      ajax({ url }).pipe(
        mergeMap( establishments => {
          const { error, data } = establishments.body
          if (error) throw 'some error';
          return of(actions.receiveMappedEstablishments(data))
        }),
        catchError(error =>
          (of(actions.requestMappedEstablishmentsFailure(error))),
        takeUntil(action$.ofType(UI_HAS_CHANGED_SO_NEW_DATA_NOW_REQUIRED)) 
        ),
      )
    ),
  )