Redux Observable:如果多次分派相同的动作,我该如何取消其中一个?

Redux Observable: If the same action is dispatched multiple times, how do I cancel one of them?

说,我有这部史诗:

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' })

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

您可以使用 .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)
    )