0

Say, I have this epic:

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

Then, somewhere I dispatched t.GET_USER multiple times:

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

How do I cancel one of them? For example:

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

AFAIK, .takeUntil(action$.ofType(t.GET_USER_CANCELLED)) will cancel all t.GET_USER actions. I need to cancel only one.

Jaye Renzo Montejo
  • 1,812
  • 2
  • 12
  • 25
  • 1
    You can use `switchMap` instead of `mergeMap` but it really depends on the logic of your app. – martin Nov 28 '17 at 10:03

1 Answers1

3

You can use a .filter() predicate to only unsubscribe your mergeMap for the exact uid you are looking for:

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)
    )
Mark van Straten
  • 9,287
  • 3
  • 38
  • 57