Independent chain cancellation in redux-observable

2019-05-12 17:52发布

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.

1条回答
Lonely孤独者°
2楼-- · 2019-05-12 18:43

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))
  )
查看更多
登录 后发表回答