AsyncEnumerable.Conversions.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static IAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this IEnumerable<TSource> source)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException("source");
  15. return Create(() =>
  16. {
  17. var e = source.GetEnumerator();
  18. return Create(
  19. ct => Task.Factory.StartNew(() =>
  20. {
  21. var res = default(bool);
  22. try
  23. {
  24. res = e.MoveNext();
  25. }
  26. finally
  27. {
  28. if (!res)
  29. e.Dispose();
  30. }
  31. return res;
  32. }, ct),
  33. () => e.Current,
  34. () => e.Dispose()
  35. );
  36. });
  37. }
  38. public static IEnumerable<TSource> ToEnumerable<TSource>(this IAsyncEnumerable<TSource> source)
  39. {
  40. if (source == null)
  41. throw new ArgumentNullException("source");
  42. return ToEnumerable_(source);
  43. }
  44. private static IEnumerable<TSource> ToEnumerable_<TSource>(IAsyncEnumerable<TSource> source)
  45. {
  46. using (var e = source.GetEnumerator())
  47. {
  48. while (true)
  49. {
  50. var t = e.MoveNext(CancellationToken.None);
  51. t.Wait();
  52. if (!t.Result)
  53. break;
  54. var c = e.Current;
  55. yield return c;
  56. }
  57. }
  58. }
  59. #if !NO_RXINTERFACES
  60. public static IAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(this IObservable<TSource> source)
  61. {
  62. if (source == null)
  63. throw new ArgumentNullException("source");
  64. return Create(() =>
  65. {
  66. var observer = new ToAsyncEnumerableObserver<TSource>();
  67. observer.Queue = new Queue<Either<TSource, Exception, bool>>();
  68. var subscription = source.Subscribe(observer);
  69. return Create(
  70. (ct, tcs) =>
  71. {
  72. lock (observer.Queue)
  73. {
  74. if (observer.Queue.Count > 0)
  75. {
  76. var n = observer.Queue.Dequeue();
  77. n.Switch(
  78. x =>
  79. {
  80. observer.Current = x;
  81. tcs.TrySetResult(true);
  82. },
  83. ex =>
  84. {
  85. tcs.TrySetException(ex);
  86. },
  87. _ =>
  88. {
  89. tcs.TrySetResult(false);
  90. }
  91. );
  92. }
  93. else
  94. observer.TaskCompletionSource = tcs;
  95. }
  96. return tcs.Task;
  97. },
  98. () => observer.Current,
  99. () =>
  100. {
  101. subscription.Dispose();
  102. // Should we cancel in-flight operations somehow?
  103. });
  104. });
  105. }
  106. class ToAsyncEnumerableObserver<T> : IObserver<T>
  107. {
  108. public Queue<Either<T, Exception, bool>> Queue { get; set; }
  109. public T Current { get; set; }
  110. public TaskCompletionSource<bool> TaskCompletionSource { get; set; }
  111. public void OnCompleted()
  112. {
  113. lock (Queue)
  114. {
  115. if (TaskCompletionSource == null)
  116. Queue.Enqueue(new Either<T, Exception, bool>.Choice3(true));
  117. else
  118. {
  119. TaskCompletionSource.SetResult(false);
  120. TaskCompletionSource = null;
  121. }
  122. }
  123. }
  124. public void OnError(Exception error)
  125. {
  126. lock (Queue)
  127. {
  128. if (TaskCompletionSource == null)
  129. Queue.Enqueue(new Either<T, Exception, bool>.Choice2(error));
  130. else
  131. {
  132. TaskCompletionSource.SetException(error);
  133. TaskCompletionSource = null;
  134. }
  135. }
  136. }
  137. public void OnNext(T value)
  138. {
  139. lock (Queue)
  140. {
  141. if (TaskCompletionSource == null)
  142. Queue.Enqueue(new Either<T, Exception, bool>.Choice1(value));
  143. else
  144. {
  145. Current = value;
  146. TaskCompletionSource.SetResult(true);
  147. TaskCompletionSource = null;
  148. }
  149. }
  150. }
  151. }
  152. abstract class Either<T, U, V>
  153. {
  154. public abstract void Switch(Action<T> choice1, Action<U> choice2, Action<V> choice3);
  155. public class Choice1 : Either<T, U, V>
  156. {
  157. public Choice1(T value) { Value = value; }
  158. public T Value { get; private set; }
  159. public override void Switch(Action<T> choice1, Action<U> choice2, Action<V> choice3)
  160. {
  161. choice1(Value);
  162. }
  163. }
  164. public class Choice2 : Either<T, U, V>
  165. {
  166. public Choice2(U value) { Value = value; }
  167. public U Value { get; private set; }
  168. public override void Switch(Action<T> choice1, Action<U> choice2, Action<V> choice3)
  169. {
  170. choice2(Value);
  171. }
  172. }
  173. public class Choice3 : Either<T, U, V>
  174. {
  175. public Choice3(V value) { Value = value; }
  176. public V Value { get; private set; }
  177. public override void Switch(Action<T> choice1, Action<U> choice2, Action<V> choice3)
  178. {
  179. choice3(Value);
  180. }
  181. }
  182. }
  183. public static IObservable<TSource> ToObservable<TSource>(this IAsyncEnumerable<TSource> source)
  184. {
  185. if (source == null)
  186. throw new ArgumentNullException("source");
  187. return new ToObservableObservable<TSource>(source);
  188. }
  189. class ToObservableObservable<T> : IObservable<T>
  190. {
  191. private readonly IAsyncEnumerable<T> source;
  192. public ToObservableObservable(IAsyncEnumerable<T> source)
  193. {
  194. this.source = source;
  195. }
  196. public IDisposable Subscribe(IObserver<T> observer)
  197. {
  198. var ctd = new CancellationTokenDisposable();
  199. var e = source.GetEnumerator();
  200. var f = default(Action);
  201. f = () => e.MoveNext(ctd.Token).ContinueWith(t =>
  202. {
  203. if (t.IsFaulted)
  204. {
  205. observer.OnError(t.Exception);
  206. e.Dispose();
  207. }
  208. else if (t.IsCanceled)
  209. {
  210. e.Dispose();
  211. }
  212. else if (t.IsCompleted)
  213. {
  214. if (t.Result)
  215. {
  216. observer.OnNext(e.Current);
  217. f();
  218. }
  219. else
  220. {
  221. observer.OnCompleted();
  222. e.Dispose();
  223. }
  224. }
  225. }, ctd.Token);
  226. f();
  227. return new CompositeDisposable(ctd, e);
  228. }
  229. }
  230. #endif
  231. }
  232. }