4

I'm new to RxJS. In my app I need independent cancellation of deferred action. Here's a working example (the delay is 3 seconds). But when I choose to delete multiple items and cancel one of them, then canceled all at once.

Epic code:

const itemsEpic = action$ =>
  action$.ofType('WILL_DELETE')
    .flatMap(action =>
      Observable.of({type: 'DELETE', id: action.id})
        .delay(3000)
        .takeUntil(action$.ofType('UNDO_DELETE'))
  )

I think I need to pass an id to takeUntil operator, but I don't know how to do it.

Andy Pro
  • 55
  • 7

1 Answers1

4

If I understand the takeUntil operator correctly, it stops emitting new items from the Observable it was called on, once the argument Observable emits it's first item. With this in mind you could do something like this:

const itemsEpic = action$ => action$.ofType('WILL_DELETE')
  .flatMap(action => Observable.of({ type: 'DELETE', id: action.id })
    .delay(3000)
    .takeUntil(action$.ofType('UNDO_DELETE').filter(({id}) => id === action.id))
  )
Balázs Édes
  • 13,452
  • 6
  • 54
  • 89