ToAsyncEnumerable.Observable.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Concurrent;
  5. using System.Collections.Generic;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  13. // Moved to AsyncEnumerableEx in System.Interactive.Async.
  14. // System.Linq.AsyncEnumerable has chosen not to implement this. We continue to implement this because
  15. // we believe it is a useful feature, but since it's now in the category of LINQ-adjacent functionality
  16. // not built into the .NET runtime libraries, it now lives in System.Interactive.Async.
  17. /// <summary>
  18. /// Converts an observable sequence to an async-enumerable sequence.
  19. /// </summary>
  20. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  21. /// <param name="source">Observable sequence to convert to an async-enumerable sequence.</param>
  22. /// <returns>The async-enumerable sequence whose elements are pulled from the given observable sequence.</returns>
  23. /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
  24. public static IAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this IObservable<TSource> source)
  25. {
  26. if (source == null)
  27. throw Error.ArgumentNull(nameof(source));
  28. return new ObservableAsyncEnumerable<TSource>(source);
  29. }
  30. private sealed class ObservableAsyncEnumerable<TSource> : AsyncIterator<TSource>, IObserver<TSource>
  31. {
  32. private readonly IObservable<TSource> _source;
  33. private ConcurrentQueue<TSource>? _values = new();
  34. private Exception? _error;
  35. private bool _completed;
  36. private TaskCompletionSource<bool>? _signal;
  37. private IDisposable? _subscription;
  38. private CancellationTokenRegistration _ctr;
  39. public ObservableAsyncEnumerable(IObservable<TSource> source) => _source = source;
  40. public override AsyncIteratorBase<TSource> Clone() => new ObservableAsyncEnumerable<TSource>(_source);
  41. public override ValueTask DisposeAsync()
  42. {
  43. Dispose();
  44. return base.DisposeAsync();
  45. }
  46. protected override async ValueTask<bool> MoveNextCore()
  47. {
  48. //
  49. // REVIEW: How often should we check? At the very least, we want to prevent
  50. // subscribing if cancellation is requested. A case may be made to
  51. // check for each iteration, namely because this operator is a bridge
  52. // with another interface. However, we also wire up cancellation to
  53. // the observable subscription, so there's redundancy here.
  54. //
  55. _cancellationToken.ThrowIfCancellationRequested();
  56. switch (_state)
  57. {
  58. case AsyncIteratorState.Allocated:
  59. //
  60. // NB: Breaking change to align with lazy nature of async iterators.
  61. //
  62. // In previous implementations, the Subscribe call happened during
  63. // the call to GetAsyncEnumerator.
  64. //
  65. // REVIEW: Confirm this design point. This implementation is compatible
  66. // with an async iterator using "yield return", e.g. subscribing
  67. // to the observable sequence and yielding values out of a local
  68. // queue filled by observer callbacks. However, it departs from
  69. // the dual treatment of Subscribe/GetEnumerator.
  70. //
  71. _subscription = _source.Subscribe(this);
  72. _ctr = _cancellationToken.Register(OnCanceled, state: null);
  73. _state = AsyncIteratorState.Iterating;
  74. goto case AsyncIteratorState.Iterating;
  75. case AsyncIteratorState.Iterating:
  76. while (true)
  77. {
  78. var completed = Volatile.Read(ref _completed);
  79. if (_values!.TryDequeue(out _current!))
  80. {
  81. return true;
  82. }
  83. else if (completed)
  84. {
  85. var error = _error;
  86. if (error != null)
  87. {
  88. throw error;
  89. }
  90. return false;
  91. }
  92. await Resume().ConfigureAwait(false);
  93. Volatile.Write(ref _signal, null);
  94. }
  95. }
  96. await DisposeAsync().ConfigureAwait(false);
  97. return false;
  98. }
  99. public void OnCompleted()
  100. {
  101. Volatile.Write(ref _completed, true);
  102. DisposeSubscription();
  103. OnNotification();
  104. }
  105. public void OnError(Exception error)
  106. {
  107. _error = error;
  108. Volatile.Write(ref _completed, true);
  109. DisposeSubscription();
  110. OnNotification();
  111. }
  112. public void OnNext(TSource value)
  113. {
  114. _values?.Enqueue(value);
  115. OnNotification();
  116. }
  117. private void OnNotification()
  118. {
  119. while (true)
  120. {
  121. var signal = Volatile.Read(ref _signal);
  122. if (signal == TaskExt.True)
  123. {
  124. return;
  125. }
  126. if (signal != null)
  127. {
  128. signal.TrySetResult(true);
  129. return;
  130. }
  131. if (Interlocked.CompareExchange(ref _signal, TaskExt.True, null) == null)
  132. {
  133. return;
  134. }
  135. }
  136. }
  137. private void Dispose()
  138. {
  139. _ctr.Dispose();
  140. DisposeSubscription();
  141. _values = null;
  142. _error = null;
  143. }
  144. private void DisposeSubscription() => Interlocked.Exchange(ref _subscription, null)?.Dispose();
  145. private void OnCanceled(object? state)
  146. {
  147. var cancelledTcs = default(TaskCompletionSource<bool>);
  148. Dispose();
  149. while (true)
  150. {
  151. var signal = Volatile.Read(ref _signal);
  152. if (signal != null)
  153. {
  154. if (signal.TrySetCanceled(_cancellationToken))
  155. return;
  156. }
  157. if (cancelledTcs == null)
  158. {
  159. cancelledTcs = new TaskCompletionSource<bool>();
  160. cancelledTcs.TrySetCanceled(_cancellationToken);
  161. }
  162. if (Interlocked.CompareExchange(ref _signal, cancelledTcs, signal) == signal)
  163. return;
  164. }
  165. }
  166. private Task Resume()
  167. {
  168. TaskCompletionSource<bool>? newSignal = null;
  169. while (true)
  170. {
  171. var signal = Volatile.Read(ref _signal);
  172. if (signal != null)
  173. {
  174. return signal.Task;
  175. }
  176. newSignal ??= new TaskCompletionSource<bool>();
  177. if (Interlocked.CompareExchange(ref _signal, newSignal, null) == null)
  178. {
  179. return newSignal.Task;
  180. }
  181. }
  182. }
  183. }
  184. #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
  185. }
  186. }