ToAsyncEnumerable.Observable.cs 7.9 KB

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