123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- // 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.Disposables;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Reactive.Linq
- {
- partial class AsyncObservable
- {
- public static IAsyncObservable<TSource> Amb<TSource>(this IAsyncObservable<TSource> first, IAsyncObservable<TSource> second)
- {
- if (first == null)
- throw new ArgumentNullException(nameof(first));
- if (second == null)
- throw new ArgumentNullException(nameof(second));
- return Create<TSource>(async observer =>
- {
- var firstSubscription = new SingleAssignmentAsyncDisposable();
- var secondSubscription = new SingleAssignmentAsyncDisposable();
- var (firstObserver, secondObserver) = AsyncObserver.Amb(observer, firstSubscription, secondSubscription);
- var firstTask = first.SubscribeAsync(firstObserver).ContinueWith(d => firstSubscription.AssignAsync(d.Result));
- var secondTask = second.SubscribeAsync(secondObserver).ContinueWith(d => secondSubscription.AssignAsync(d.Result));
- await Task.WhenAll(firstTask, secondTask).ConfigureAwait(false);
- return StableCompositeAsyncDisposable.Create(firstSubscription, secondSubscription);
- });
- }
- }
- partial class AsyncObserver
- {
- public static (IAsyncObserver<TSource>, IAsyncObserver<TSource>) Amb<TSource>(IAsyncObserver<TSource> observer, IAsyncDisposable first, IAsyncDisposable second)
- {
- if (observer == null)
- throw new ArgumentNullException(nameof(observer));
- if (first == null)
- throw new ArgumentNullException(nameof(first));
- if (second == null)
- throw new ArgumentNullException(nameof(second));
- var gate = new AsyncLock();
- var state = AmbState.None;
- return
- (
- Create<TSource>(
- async x =>
- {
- using (await gate.LockAsync().ConfigureAwait(false))
- {
- if (state == AmbState.None)
- {
- state = AmbState.First;
- await second.DisposeAsync().ConfigureAwait(false);
- }
- if (state == AmbState.First)
- {
- await observer.OnNextAsync(x).ConfigureAwait(false);
- }
- }
- },
- async ex =>
- {
- using (await gate.LockAsync().ConfigureAwait(false))
- {
- if (state == AmbState.None)
- {
- state = AmbState.First;
- await second.DisposeAsync().ConfigureAwait(false);
- }
- if (state == AmbState.First)
- {
- await observer.OnErrorAsync(ex).ConfigureAwait(false);
- }
- }
- },
- async () =>
- {
- using (await gate.LockAsync().ConfigureAwait(false))
- {
- if (state == AmbState.None)
- {
- state = AmbState.First;
- await second.DisposeAsync().ConfigureAwait(false);
- }
- if (state == AmbState.First)
- {
- await observer.OnCompletedAsync().ConfigureAwait(false);
- }
- }
- }
- ),
- Create<TSource>(
- async x =>
- {
- using (await gate.LockAsync().ConfigureAwait(false))
- {
- if (state == AmbState.None)
- {
- state = AmbState.Second;
- await first.DisposeAsync().ConfigureAwait(false);
- }
- if (state == AmbState.Second)
- {
- await observer.OnNextAsync(x).ConfigureAwait(false);
- }
- }
- },
- async ex =>
- {
- using (await gate.LockAsync().ConfigureAwait(false))
- {
- if (state == AmbState.None)
- {
- state = AmbState.Second;
- await first.DisposeAsync().ConfigureAwait(false);
- }
- if (state == AmbState.Second)
- {
- await observer.OnErrorAsync(ex).ConfigureAwait(false);
- }
- }
- },
- async () =>
- {
- using (await gate.LockAsync().ConfigureAwait(false))
- {
- if (state == AmbState.None)
- {
- state = AmbState.Second;
- await first.DisposeAsync().ConfigureAwait(false);
- }
- if (state == AmbState.Second)
- {
- await observer.OnCompletedAsync().ConfigureAwait(false);
- }
- }
- }
- )
- );
- }
- private enum AmbState
- {
- None,
- First,
- Second,
- }
- }
- }
|