ToObservable.cs 7.4 KB

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