Rx and tasks - cancel running task when new task is spawned?

前端 未结 2 1361
醉话见心
醉话见心 2020-12-16 04:20

I have an user interaction scenario I\'d like to handle with Rx.

The scenario is similar to the canonical \"when user stops typing, do some work\" (usually, search

2条回答
  •  被撕碎了的回忆
    2020-12-16 04:50

    You can just use Observable.FromAsync which will generate tokens that are cancelled when the observer unsubcribes:

    input.Throttle(...)
         .Select(_ => Observable.FromAsync(token => CreateMyTask(..., token)))
         .Switch()
         .Subscribe(...);
    

    This will generate a new token for each unit of work and cancel it every time Switch switches to the new one.

提交回复
热议问题