123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT License.
- // See the LICENSE file in the project root for more information.
- using System.Reactive.Disposables;
- using System.Reactive.Subjects;
- using System.Threading;
- namespace System.Reactive.Linq
- {
- // REVIEW: Consider using these for GetAwaiter.
- public partial class AsyncObservable
- {
- public static AsyncAsyncSubject<TSource> RunAsync<TSource>(this IAsyncObservable<TSource> source, CancellationToken token)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- var subject = new SequentialAsyncAsyncSubject<TSource>();
- if (token.IsCancellationRequested)
- {
- var ignored = subject.OnErrorAsync(new OperationCanceledException(token));
- return subject;
- }
- var subscribeTask = source.SubscribeSafeAsync(subject);
- subscribeTask.AsTask().ContinueWith(t =>
- {
- if (t.Exception != null)
- {
- subject.OnErrorAsync(t.Exception); // NB: Should not occur due to use of SubscribeSafeAsync.
- }
- });
- if (token.CanBeCanceled)
- {
- var d = new SingleAssignmentAsyncDisposable();
- subscribeTask.AsTask().ContinueWith(t =>
- {
- if (t.Exception == null)
- {
- var ignored = d.AssignAsync(t.Result);
- }
- });
- token.Register(() =>
- {
- var ignored = d.DisposeAsync();
- });
- }
- return subject;
- }
- public static AsyncAsyncSubject<TSource> RunAsync<TSource>(this IConnectableAsyncObservable<TSource> source, CancellationToken token)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- var subject = new SequentialAsyncAsyncSubject<TSource>();
- if (token.IsCancellationRequested)
- {
- var ignored = subject.OnErrorAsync(new OperationCanceledException(token));
- return subject;
- }
- var d = new CompositeAsyncDisposable();
- var subscribeTask = source.SubscribeSafeAsync(subject);
- subscribeTask.AsTask().ContinueWith(t =>
- {
- if (t.Exception != null)
- {
- subject.OnErrorAsync(t.Exception); // NB: Should not occur due to use of SubscribeSafeAsync.
- }
- else
- {
- var ignored = d.AddAsync(t.Result);
- source.ConnectAsync().AsTask().ContinueWith(t2 =>
- {
- if (t2.Exception == null)
- {
- var ignored2 = d.AddAsync(t2.Result);
- }
- });
- }
- });
- if (token.CanBeCanceled)
- {
- token.Register(() =>
- {
- var ignored = d.DisposeAsync();
- });
- }
- return subject;
- }
- }
- }
|