Independent chain cancellation in redux-observable?

删除回忆录丶 提交于 2019-12-10 10:06:32

问题


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:


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


来源:https://stackoverflow.com/questions/41966806/independent-chain-cancellation-in-redux-observable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!