123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the Apache 2.0 License.
- // See the LICENSE file in the project root for more information.
- using System.Reactive.Concurrency;
- using System.Reactive.Disposables;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Reactive.Linq
- {
- partial class AsyncObservable
- {
- public static IAsyncObservable<TSource> Timeout<TSource>(this IAsyncObservable<TSource> source, TimeSpan dueTime)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- return Create<TSource>(async observer =>
- {
- var sourceSubscription = new SingleAssignmentAsyncDisposable();
- var (sink, disposable) = await AsyncObserver.Timeout(observer, sourceSubscription, dueTime).ConfigureAwait(false);
- var sourceSubscriptionInner = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
- await sourceSubscription.AssignAsync(sourceSubscriptionInner).ConfigureAwait(false);
- return disposable;
- });
- }
- public static IAsyncObservable<TSource> Timeout<TSource>(this IAsyncObservable<TSource> source, TimeSpan dueTime, IAsyncScheduler scheduler)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (scheduler == null)
- throw new ArgumentNullException(nameof(scheduler));
- return Create<TSource>(async observer =>
- {
- var sourceSubscription = new SingleAssignmentAsyncDisposable();
- var (sink, disposable) = await AsyncObserver.Timeout(observer, sourceSubscription, dueTime, scheduler).ConfigureAwait(false);
- var sourceSubscriptionInner = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
- await sourceSubscription.AssignAsync(sourceSubscriptionInner).ConfigureAwait(false);
- return disposable;
- });
- }
- public static IAsyncObservable<TSource> Timeout<TSource>(this IAsyncObservable<TSource> source, TimeSpan dueTime, IAsyncObservable<TSource> other)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (other == null)
- throw new ArgumentNullException(nameof(other));
- return Create<TSource>(async observer =>
- {
- var sourceSubscription = new SingleAssignmentAsyncDisposable();
- var (sink, disposable) = await AsyncObserver.Timeout(observer, sourceSubscription, dueTime, other).ConfigureAwait(false);
- var sourceSubscriptionInner = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
- await sourceSubscription.AssignAsync(sourceSubscriptionInner).ConfigureAwait(false);
- return disposable;
- });
- }
- public static IAsyncObservable<TSource> Timeout<TSource>(this IAsyncObservable<TSource> source, TimeSpan dueTime, IAsyncObservable<TSource> other, IAsyncScheduler scheduler)
- {
- if (source == null)
- throw new ArgumentNullException(nameof(source));
- if (other == null)
- throw new ArgumentNullException(nameof(other));
- if (scheduler == null)
- throw new ArgumentNullException(nameof(scheduler));
- return Create<TSource>(async observer =>
- {
- var sourceSubscription = new SingleAssignmentAsyncDisposable();
- var (sink, disposable) = await AsyncObserver.Timeout(observer, sourceSubscription, dueTime, other, scheduler).ConfigureAwait(false);
- var sourceSubscriptionInner = await source.SubscribeSafeAsync(sink).ConfigureAwait(false);
- await sourceSubscription.AssignAsync(sourceSubscriptionInner).ConfigureAwait(false);
- return disposable;
- });
- }
- }
- partial class AsyncObserver
- {
- public static Task<(IAsyncObserver<TSource>, IAsyncDisposable)> Timeout<TSource>(IAsyncObserver<TSource> observer, IAsyncDisposable sourceSubscription, TimeSpan dueTime)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (sourceSubscription == null)
- throw new ArgumentNullException(nameof(sourceSubscription));
- return Timeout(observer, sourceSubscription, dueTime, AsyncObservable.Throw<TSource>(new TimeoutException()), TaskPoolAsyncScheduler.Default);
- }
- public static Task<(IAsyncObserver<TSource>, IAsyncDisposable)> Timeout<TSource>(IAsyncObserver<TSource> observer, IAsyncDisposable sourceSubscription, TimeSpan dueTime, IAsyncScheduler scheduler)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (sourceSubscription == null)
- throw new ArgumentNullException(nameof(sourceSubscription));
- if (scheduler == null)
- throw new ArgumentNullException(nameof(scheduler));
- return Timeout(observer, sourceSubscription, dueTime, AsyncObservable.Throw<TSource>(new TimeoutException()), scheduler);
- }
- public static Task<(IAsyncObserver<TSource>, IAsyncDisposable)> Timeout<TSource>(IAsyncObserver<TSource> observer, IAsyncDisposable sourceSubscription, TimeSpan dueTime, IAsyncObservable<TSource> other)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (sourceSubscription == null)
- throw new ArgumentNullException(nameof(sourceSubscription));
- if (other == null)
- throw new ArgumentNullException(nameof(other));
- return Timeout(observer, sourceSubscription, dueTime, other, TaskPoolAsyncScheduler.Default);
- }
- public static Task<(IAsyncObserver<TSource>, IAsyncDisposable)> Timeout<TSource>(IAsyncObserver<TSource> observer, IAsyncDisposable sourceSubscription, TimeSpan dueTime, IAsyncObservable<TSource> other, IAsyncScheduler scheduler)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (sourceSubscription == null)
- throw new ArgumentNullException(nameof(sourceSubscription));
- if (other == null)
- throw new ArgumentNullException(nameof(other));
- if (scheduler == null)
- throw new ArgumentNullException(nameof(scheduler));
- return CoreAsync();
- async Task<(IAsyncObserver<TSource>, IAsyncDisposable)> CoreAsync()
- {
- var gate = new AsyncLock();
- var switched = false;
- var id = 0UL;
- var timer = new SerialAsyncDisposable();
- var subscription = new SerialAsyncDisposable();
- var d = StableCompositeAsyncDisposable.Create(timer, subscription);
- await subscription.AssignAsync(sourceSubscription).ConfigureAwait(false);
- async Task<bool> OnAsync()
- {
- var hasWon = false;
- using (await gate.LockAsync().ConfigureAwait(false))
- {
- if (!switched)
- {
- unchecked
- {
- ++id;
- }
- hasWon = true;
- }
- }
- return hasWon;
- }
- async Task CreateTimerAsync()
- {
- var timerId = id;
- var timeout = await scheduler.ScheduleAsync(async ct =>
- {
- var hasWon = false;
- using (await gate.LockAsync().ConfigureAwait(false))
- {
- hasWon = switched = timerId == id;
- }
- if (hasWon)
- {
- var otherSubscription = await other.SubscribeSafeAsync(observer).RendezVous(scheduler, ct);
- await subscription.AssignAsync(otherSubscription).RendezVous(scheduler, ct);
- }
- }, dueTime).ConfigureAwait(false);
- await timer.AssignAsync(timeout).ConfigureAwait(false);
- }
- var sink = Create<TSource>(
- async x =>
- {
- if (await OnAsync().ConfigureAwait(false))
- {
- await observer.OnNextAsync(x).ConfigureAwait(false);
- await CreateTimerAsync().ConfigureAwait(false);
- }
- },
- async ex =>
- {
- if (await OnAsync().ConfigureAwait(false))
- {
- await observer.OnErrorAsync(ex).ConfigureAwait(false);
- }
- },
- async () =>
- {
- if (await OnAsync().ConfigureAwait(false))
- {
- await observer.OnCompletedAsync().ConfigureAwait(false);
- }
- }
- );
- await CreateTimerAsync().ConfigureAwait(false);
- return (sink, d);
- }
- }
- }
- }
|