dotnet / reactive

The Reactive Extensions for .NET
http://reactivex.io
MIT License
6.69k stars 751 forks source link

Consider adding FromAsyncWithPostCancelNotifications #1966

Open idg10 opened 1 year ago

idg10 commented 1 year ago

The ReactiveUI project uses Rx's ability to wrap a Task<T> as an IObservable<T>, but there's a capability they have wanted for a long time that Rx currently does not offer: the ability both to cancel the underlying task and to observe its progress after cancellation.

There's no built in way to do this, because the mechanism by which you cancel a Task<T> wrapped in an IObservable<T> is to unsubscribe (i.e. to call Dispose on the IDisposable returned by Subscribe). The problem with this is that unsubscribing tells Rx that you don't want to receive any more notifications.

https://github.com/reactiveui/ReactiveUI/issues/2153 describes a problem in ReactiveUI that can be attributed to this missing Rx capability.

Although this is not insurmountable, as the PR at https://github.com/reactiveui/ReactiveUI/pull/3556 shows, it would would be useful if Rx provided an out-of-the-box solution to this. We could do this by providing something like the FromAsyncWithPostCancelNotifications extension method defined by that PR:

https://github.com/reactiveui/ReactiveUI/pull/3556/files#diff-41fb8479ae2ebc5305bca72fa394bd5bdf8d13b1806c76ca65a6878e8954f075R103-R122

As @ChrisPulman points out at https://github.com/reactiveui/ReactiveUI/pull/3556#issuecomment-1575278125 we would need a set of overloads to cover all the variations.

FromAsyncWithPostCancelNotifications might not be the ideal name for this, but we can give that some thought later.

ChrisPulman commented 10 months ago

@idg10 Any progress on the new Function, amongst the thousand other things you have going on? Perhaps FromCancellationAsync would be a relevant name.