Redux Observable:如果多次调度相同的操作,我该如何取消其中一个?

时间:2017-11-28 09:58:46

标签: rxjs redux-observable

说,我有这个史诗:

export const getUserEpic = action$ => action$
  .ofType(t.GET_USER)
  .mergeMap(action => Observable
    .from(query.findUser(action.uid))
    .map(user => ({ type: t.GET_USER_SUCCESS, user }))
    .takeUntil(action$.ofType(t.GET_USER_CANCELLED))

然后,我多次派遣t.GET_USER的某个地方:

dispatch({ type: t.GET_USER, uid: 'uid1' })
dispatch({ type: t.GET_USER, uid: 'uid2' })
dispatch({ type: t.GET_USER, uid: 'uid3' })

如何取消其中一个?例如:

dispatch({ type: t.GET_USER_CANCELLED, uid: 'uid2' })

AFAIK,.takeUntil(action$.ofType(t.GET_USER_CANCELLED))将取消所有t.GET_USER次操作。我只需要取消一个。

1 个答案:

答案 0 :(得分:3)

您可以使用.filter()谓词仅取消订阅您正在寻找的uid的mergeMap:

export const getUserEpic = action$ => action$
  .ofType(t.GET_USER)
  .mergeMap(action => Observable
    .from(query.findUser(action.uid))
    .map(user => ({ type: t.GET_USER_SUCCESS, user }))
    .takeUntil(
      action$
        .ofType(t.GET_USER_CANCELLED)
        .filter(act => act.uid === action.uid)
    )