ToObservable.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace System.Linq
  9. {
  10. public static partial class AsyncEnumerable
  11. {
  12. public static IAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this IObservable<TSource> source)
  13. {
  14. if (source == null)
  15. throw new ArgumentNullException(nameof(source));
  16. return Create(() =>
  17. {
  18. var observer = new ToAsyncEnumerableObserver<TSource>();
  19. var subscription = source.Subscribe(observer);
  20. return Create(
  21. (ct, tcs) =>
  22. {
  23. var hasValue = false;
  24. var hasCompleted = false;
  25. var error = default(Exception);
  26. lock (observer.SyncRoot)
  27. {
  28. if (observer.Values.Count > 0)
  29. {
  30. hasValue = true;
  31. observer.Current = observer.Values.Dequeue();
  32. }
  33. else if (observer.HasCompleted)
  34. {
  35. hasCompleted = true;
  36. }
  37. else if (observer.Error != null)
  38. {
  39. error = observer.Error;
  40. }
  41. else
  42. {
  43. observer.TaskCompletionSource = tcs;
  44. }
  45. }
  46. if (hasValue)
  47. {
  48. tcs.TrySetResult(true);
  49. }
  50. else if (hasCompleted)
  51. {
  52. tcs.TrySetResult(false);
  53. }
  54. else if (error != null)
  55. {
  56. tcs.TrySetException(error);
  57. }
  58. return tcs.Task;
  59. },
  60. () => observer.Current,
  61. () =>
  62. {
  63. subscription.Dispose();
  64. // Should we cancel in-flight operations somehow?
  65. });
  66. });
  67. }
  68. public static IObservable<TSource> ToObservable<TSource>(this IAsyncEnumerable<TSource> source)
  69. {
  70. if (source == null)
  71. throw new ArgumentNullException(nameof(source));
  72. return new ToObservableObservable<TSource>(source);
  73. }
  74. private class ToAsyncEnumerableObserver<T> : IObserver<T>
  75. {
  76. public readonly Queue<T> Values;
  77. public T Current;
  78. public Exception Error;
  79. public bool HasCompleted;
  80. public TaskCompletionSource<bool> TaskCompletionSource;
  81. public ToAsyncEnumerableObserver()
  82. {
  83. Values = new Queue<T>();
  84. }
  85. public object SyncRoot
  86. {
  87. get { return Values; }
  88. }
  89. public void OnCompleted()
  90. {
  91. var tcs = default(TaskCompletionSource<bool>);
  92. lock (SyncRoot)
  93. {
  94. HasCompleted = true;
  95. if (TaskCompletionSource != null)
  96. {
  97. tcs = TaskCompletionSource;
  98. TaskCompletionSource = null;
  99. }
  100. }
  101. if (tcs != null)
  102. {
  103. tcs.SetResult(false);
  104. }
  105. }
  106. public void OnError(Exception error)
  107. {
  108. var tcs = default(TaskCompletionSource<bool>);
  109. lock (SyncRoot)
  110. {
  111. Error = error;
  112. if (TaskCompletionSource != null)
  113. {
  114. tcs = TaskCompletionSource;
  115. TaskCompletionSource = null;
  116. }
  117. }
  118. if (tcs != null)
  119. {
  120. tcs.SetException(error);
  121. }
  122. }
  123. public void OnNext(T value)
  124. {
  125. var tcs = default(TaskCompletionSource<bool>);
  126. lock (SyncRoot)
  127. {
  128. if (TaskCompletionSource == null)
  129. {
  130. Values.Enqueue(value);
  131. }
  132. else
  133. {
  134. Current = value;
  135. tcs = TaskCompletionSource;
  136. TaskCompletionSource = null;
  137. }
  138. }
  139. if (tcs != null)
  140. {
  141. tcs.SetResult(true);
  142. }
  143. }
  144. }
  145. private class ToObservableObservable<T> : IObservable<T>
  146. {
  147. private readonly IAsyncEnumerable<T> source;
  148. public ToObservableObservable(IAsyncEnumerable<T> source)
  149. {
  150. this.source = source;
  151. }
  152. public IDisposable Subscribe(IObserver<T> observer)
  153. {
  154. var ctd = new CancellationTokenDisposable();
  155. var e = source.GetEnumerator();
  156. var f = default(Action);
  157. f = () => e.MoveNext(ctd.Token)
  158. .ContinueWith(t =>
  159. {
  160. if (t.IsFaulted)
  161. {
  162. observer.OnError(t.Exception);
  163. e.Dispose();
  164. }
  165. else if (t.IsCanceled)
  166. {
  167. e.Dispose();
  168. }
  169. else if (t.IsCompleted)
  170. {
  171. if (t.Result)
  172. {
  173. observer.OnNext(e.Current);
  174. f();
  175. }
  176. else
  177. {
  178. observer.OnCompleted();
  179. e.Dispose();
  180. }
  181. }
  182. }, ctd.Token);
  183. f();
  184. return Disposable.Create(ctd, e);
  185. }
  186. }
  187. }
  188. }