How to use CompositeDisposable with IAsyncObservable subscriptions? #2160
Replies: 1 comment 1 reply
-
That's up to the observable source. For example, if you call For example: IAsyncObservable<int> slow = AsyncObservable.Create(
async (IAsyncObserver<int> obs)
{
// We can take as long as we like...
await Task.Delay(5000);
return AsyncDisposable.Nop;
}); If you call So the answer to the question "when does this task complete?" is: "That's outside of Async.Rx's control." I think (but I'm not 100% sure because we inherited this code, and I've not yet had time to look at every single line of code in Rx.Async) that as long as no external code causes subscription to block, it will complete immediately. As far as I know, all operators just pass the call down to their source and will block only if their source's I think So essentially what you want is something which waits for the public static async void DisposeWithAsync(this ValueTask<IDisposable> dt, CompositeDisposable compositeDisposable)
where T : IDisposable
{
compositeDisposable.ArgumentNullExceptionThrowIfNull(nameof(compositeDisposable));
compositeDisposable.Add(await dt);
} However, this is nasty because it's To avoid that you'd need some mechanism for handling errors that occur during asynchronous subscription. You can't just block, because the fact that you're using Rx.Async means you're in a world of async. So you would need to make an application-level decision about how you actually want to handle that. What would you want to do if a subscription like this fails, say, several seconds after the call to If you're expecting the subscription to be synchronous in practice, you could inspect the |
Beta Was this translation helpful? Give feedback.
-
For our application, we've been abusing
async void
to execute subscriptions asynchronously when needed. We then dispose of these subscriptions with aCompositeDisposable
object when a navigation event occurs. For example:We see the new
System.Reactive.Async
is in preview, and we'd like to use it. However, I'm having trouble figuring out how to bridge the gap between theValueTask<IDisposable>
(returned fromSubscribeAsync
) andDisposeWith
. When does this "task" complete? How do I cancel the subscription through it?Can anyone provide some guidance here?
Beta Was this translation helpful? Give feedback.
All reactions