Amb.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Reactive.Disposables;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Reactive.Linq
  8. {
  9. partial class AsyncObservable
  10. {
  11. public static IAsyncObservable<TSource> Amb<TSource>(this IAsyncObservable<TSource> first, IAsyncObservable<TSource> second)
  12. {
  13. if (first == null)
  14. throw new ArgumentNullException(nameof(first));
  15. if (second == null)
  16. throw new ArgumentNullException(nameof(second));
  17. return Create<TSource>(async observer =>
  18. {
  19. var firstSubscription = new SingleAssignmentAsyncDisposable();
  20. var secondSubscription = new SingleAssignmentAsyncDisposable();
  21. var (firstObserver, secondObserver) = AsyncObserver.Amb(observer, firstSubscription, secondSubscription);
  22. var firstTask = first.SubscribeAsync(firstObserver).ContinueWith(d => firstSubscription.AssignAsync(d.Result));
  23. var secondTask = second.SubscribeAsync(secondObserver).ContinueWith(d => secondSubscription.AssignAsync(d.Result));
  24. await Task.WhenAll(firstTask, secondTask).ConfigureAwait(false);
  25. return StableCompositeAsyncDisposable.Create(firstSubscription, secondSubscription);
  26. });
  27. }
  28. }
  29. partial class AsyncObserver
  30. {
  31. public static (IAsyncObserver<TSource>, IAsyncObserver<TSource>) Amb<TSource>(IAsyncObserver<TSource> observer, IAsyncDisposable first, IAsyncDisposable second)
  32. {
  33. if (observer == null)
  34. throw new ArgumentNullException(nameof(observer));
  35. if (first == null)
  36. throw new ArgumentNullException(nameof(first));
  37. if (second == null)
  38. throw new ArgumentNullException(nameof(second));
  39. var gate = new AsyncLock();
  40. var state = AmbState.None;
  41. return
  42. (
  43. Create<TSource>(
  44. async x =>
  45. {
  46. using (await gate.LockAsync().ConfigureAwait(false))
  47. {
  48. if (state == AmbState.None)
  49. {
  50. state = AmbState.First;
  51. await second.DisposeAsync().ConfigureAwait(false);
  52. }
  53. if (state == AmbState.First)
  54. {
  55. await observer.OnNextAsync(x).ConfigureAwait(false);
  56. }
  57. }
  58. },
  59. async ex =>
  60. {
  61. using (await gate.LockAsync().ConfigureAwait(false))
  62. {
  63. if (state == AmbState.None)
  64. {
  65. state = AmbState.First;
  66. await second.DisposeAsync().ConfigureAwait(false);
  67. }
  68. if (state == AmbState.First)
  69. {
  70. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  71. }
  72. }
  73. },
  74. async () =>
  75. {
  76. using (await gate.LockAsync().ConfigureAwait(false))
  77. {
  78. if (state == AmbState.None)
  79. {
  80. state = AmbState.First;
  81. await second.DisposeAsync().ConfigureAwait(false);
  82. }
  83. if (state == AmbState.First)
  84. {
  85. await observer.OnCompletedAsync().ConfigureAwait(false);
  86. }
  87. }
  88. }
  89. ),
  90. Create<TSource>(
  91. async x =>
  92. {
  93. using (await gate.LockAsync().ConfigureAwait(false))
  94. {
  95. if (state == AmbState.None)
  96. {
  97. state = AmbState.Second;
  98. await first.DisposeAsync().ConfigureAwait(false);
  99. }
  100. if (state == AmbState.Second)
  101. {
  102. await observer.OnNextAsync(x).ConfigureAwait(false);
  103. }
  104. }
  105. },
  106. async ex =>
  107. {
  108. using (await gate.LockAsync().ConfigureAwait(false))
  109. {
  110. if (state == AmbState.None)
  111. {
  112. state = AmbState.Second;
  113. await first.DisposeAsync().ConfigureAwait(false);
  114. }
  115. if (state == AmbState.Second)
  116. {
  117. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  118. }
  119. }
  120. },
  121. async () =>
  122. {
  123. using (await gate.LockAsync().ConfigureAwait(false))
  124. {
  125. if (state == AmbState.None)
  126. {
  127. state = AmbState.Second;
  128. await first.DisposeAsync().ConfigureAwait(false);
  129. }
  130. if (state == AmbState.Second)
  131. {
  132. await observer.OnCompletedAsync().ConfigureAwait(false);
  133. }
  134. }
  135. }
  136. )
  137. );
  138. }
  139. private enum AmbState
  140. {
  141. None,
  142. First,
  143. Second,
  144. }
  145. }
  146. }