Prepend.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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.Collections.Generic;
  5. using System.Reactive.Concurrency;
  6. using System.Reactive.Disposables;
  7. using System.Threading.Tasks;
  8. namespace System.Reactive.Linq
  9. {
  10. partial class AsyncObservable
  11. {
  12. public static IAsyncObservable<TSource> StartWith<TSource>(this IAsyncObservable<TSource> source, params TSource[] values) => Prepend(source, values);
  13. public static IAsyncObservable<TSource> StartWith<TSource>(this IAsyncObservable<TSource> source, IEnumerable<TSource> values) => Prepend(source, values);
  14. public static IAsyncObservable<TSource> StartWith<TSource>(this IAsyncObservable<TSource> source, IAsyncScheduler scheduler, params TSource[] values) => Prepend(source, scheduler, values);
  15. public static IAsyncObservable<TSource> StartWith<TSource>(this IAsyncObservable<TSource> source, IAsyncScheduler scheduler, IEnumerable<TSource> values) => Prepend(source, scheduler, values);
  16. public static IAsyncObservable<TSource> Prepend<TSource>(this IAsyncObservable<TSource> source, TSource value)
  17. {
  18. if (source == null)
  19. throw new ArgumentNullException(nameof(source));
  20. return Create<TSource>(async observer => await source.SubscribeSafeAsync(await AsyncObserver.Prepend(observer, value).ConfigureAwait(false)).ConfigureAwait(false));
  21. }
  22. public static IAsyncObservable<TSource> Prepend<TSource>(this IAsyncObservable<TSource> source, TSource value, IAsyncScheduler scheduler)
  23. {
  24. if (source == null)
  25. throw new ArgumentNullException(nameof(source));
  26. if (scheduler == null)
  27. throw new ArgumentNullException(nameof(scheduler));
  28. return Create<TSource>(observer => AsyncObserver.Prepend(observer, source, value, scheduler));
  29. }
  30. public static IAsyncObservable<TSource> Prepend<TSource>(this IAsyncObservable<TSource> source, params TSource[] values)
  31. {
  32. if (source == null)
  33. throw new ArgumentNullException(nameof(source));
  34. if (values == null)
  35. throw new ArgumentNullException(nameof(values));
  36. return Create<TSource>(async observer => await source.SubscribeSafeAsync(await AsyncObserver.Prepend(observer, values).ConfigureAwait(false)).ConfigureAwait(false));
  37. }
  38. public static IAsyncObservable<TSource> Prepend<TSource>(this IAsyncObservable<TSource> source, IAsyncScheduler scheduler, params TSource[] values)
  39. {
  40. if (source == null)
  41. throw new ArgumentNullException(nameof(source));
  42. if (scheduler == null)
  43. throw new ArgumentNullException(nameof(scheduler));
  44. if (values == null)
  45. throw new ArgumentNullException(nameof(values));
  46. return Create<TSource>(observer => AsyncObserver.Prepend(observer, source, scheduler, values));
  47. }
  48. public static IAsyncObservable<TSource> Prepend<TSource>(this IAsyncObservable<TSource> source, IEnumerable<TSource> values)
  49. {
  50. if (source == null)
  51. throw new ArgumentNullException(nameof(source));
  52. if (values == null)
  53. throw new ArgumentNullException(nameof(values));
  54. return Create<TSource>(async observer => await source.SubscribeSafeAsync(await AsyncObserver.Prepend(observer, values).ConfigureAwait(false)).ConfigureAwait(false));
  55. }
  56. public static IAsyncObservable<TSource> Prepend<TSource>(this IAsyncObservable<TSource> source, IAsyncScheduler scheduler, IEnumerable<TSource> values)
  57. {
  58. if (source == null)
  59. throw new ArgumentNullException(nameof(source));
  60. if (scheduler == null)
  61. throw new ArgumentNullException(nameof(scheduler));
  62. if (values == null)
  63. throw new ArgumentNullException(nameof(values));
  64. return Create<TSource>(observer => AsyncObserver.Prepend(observer, source, scheduler, values));
  65. }
  66. }
  67. partial class AsyncObserver
  68. {
  69. // REVIEW: There's some asymmetry on these overloads. Should standardize to Concat style.
  70. public static Task<IAsyncObserver<TSource>> Prepend<TSource>(IAsyncObserver<TSource> observer, TSource value)
  71. {
  72. if (observer == null)
  73. throw new ArgumentNullException(nameof(observer));
  74. return Core();
  75. async Task<IAsyncObserver<TSource>> Core()
  76. {
  77. await observer.OnNextAsync(value).ConfigureAwait(false);
  78. return observer;
  79. }
  80. }
  81. public static Task<IAsyncDisposable> Prepend<TSource>(IAsyncObserver<TSource> observer, IAsyncObservable<TSource> source, TSource value, IAsyncScheduler scheduler)
  82. {
  83. if (observer == null)
  84. throw new ArgumentNullException(nameof(observer));
  85. if (source == null)
  86. throw new ArgumentNullException(nameof(source));
  87. if (scheduler == null)
  88. throw new ArgumentNullException(nameof(scheduler));
  89. return Core();
  90. async Task<IAsyncDisposable> Core()
  91. {
  92. var subscription = new SingleAssignmentAsyncDisposable();
  93. var task = await scheduler.ScheduleAsync(async ct =>
  94. {
  95. if (ct.IsCancellationRequested)
  96. return;
  97. await observer.OnNextAsync(value).RendezVous(scheduler, ct);
  98. if (ct.IsCancellationRequested)
  99. return;
  100. var inner = await source.SubscribeSafeAsync(observer).ConfigureAwait(false);
  101. await subscription.AssignAsync(inner).RendezVous(scheduler, ct);
  102. }).ConfigureAwait(false);
  103. return StableCompositeAsyncDisposable.Create(task, subscription);
  104. }
  105. }
  106. public static Task<IAsyncObserver<TSource>> Prepend<TSource>(IAsyncObserver<TSource> observer, params TSource[] values)
  107. {
  108. if (observer == null)
  109. throw new ArgumentNullException(nameof(observer));
  110. if (values == null)
  111. throw new ArgumentNullException(nameof(values));
  112. return Core();
  113. async Task<IAsyncObserver<TSource>> Core()
  114. {
  115. foreach (var value in values)
  116. {
  117. await observer.OnNextAsync(value).ConfigureAwait(false);
  118. }
  119. return observer;
  120. }
  121. }
  122. public static Task<IAsyncDisposable> Prepend<TSource>(IAsyncObserver<TSource> observer, IAsyncObservable<TSource> source, IAsyncScheduler scheduler, params TSource[] values)
  123. {
  124. if (observer == null)
  125. throw new ArgumentNullException(nameof(observer));
  126. if (source == null)
  127. throw new ArgumentNullException(nameof(source));
  128. if (scheduler == null)
  129. throw new ArgumentNullException(nameof(scheduler));
  130. if (values == null)
  131. throw new ArgumentNullException(nameof(values));
  132. return Core();
  133. async Task<IAsyncDisposable> Core()
  134. {
  135. var subscription = new SingleAssignmentAsyncDisposable();
  136. var task = await scheduler.ScheduleAsync(async ct =>
  137. {
  138. if (ct.IsCancellationRequested)
  139. return;
  140. for (var i = 0; i < values.Length && !ct.IsCancellationRequested; i++)
  141. {
  142. await observer.OnNextAsync(values[i]).RendezVous(scheduler, ct);
  143. }
  144. if (ct.IsCancellationRequested)
  145. return;
  146. var inner = await source.SubscribeSafeAsync(observer).ConfigureAwait(false);
  147. await subscription.AssignAsync(inner).RendezVous(scheduler, ct);
  148. }).ConfigureAwait(false);
  149. return StableCompositeAsyncDisposable.Create(task, subscription);
  150. }
  151. }
  152. public static Task<IAsyncObserver<TSource>> Prepend<TSource>(IAsyncObserver<TSource> observer, IEnumerable<TSource> values)
  153. {
  154. if (observer == null)
  155. throw new ArgumentNullException(nameof(observer));
  156. if (values == null)
  157. throw new ArgumentNullException(nameof(values));
  158. return Core();
  159. async Task<IAsyncObserver<TSource>> Core()
  160. {
  161. foreach (var value in values)
  162. {
  163. await observer.OnNextAsync(value).ConfigureAwait(false);
  164. }
  165. return observer;
  166. }
  167. }
  168. public static Task<IAsyncDisposable> Prepend<TSource>(IAsyncObserver<TSource> observer, IAsyncObservable<TSource> source, IAsyncScheduler scheduler, IEnumerable<TSource> values)
  169. {
  170. if (observer == null)
  171. throw new ArgumentNullException(nameof(observer));
  172. if (source == null)
  173. throw new ArgumentNullException(nameof(source));
  174. if (scheduler == null)
  175. throw new ArgumentNullException(nameof(scheduler));
  176. if (values == null)
  177. throw new ArgumentNullException(nameof(values));
  178. return Core();
  179. async Task<IAsyncDisposable> Core()
  180. {
  181. var subscription = new SingleAssignmentAsyncDisposable();
  182. var task = await scheduler.ScheduleAsync(async ct =>
  183. {
  184. if (ct.IsCancellationRequested)
  185. return;
  186. var e = default(IEnumerator<TSource>);
  187. try
  188. {
  189. e = values.GetEnumerator();
  190. }
  191. catch (Exception ex)
  192. {
  193. await observer.OnErrorAsync(ex).RendezVous(scheduler, ct);
  194. return;
  195. }
  196. using (e)
  197. {
  198. while (!ct.IsCancellationRequested)
  199. {
  200. var b = default(bool);
  201. var value = default(TSource);
  202. try
  203. {
  204. b = e.MoveNext();
  205. if (b)
  206. {
  207. value = e.Current;
  208. }
  209. }
  210. catch (Exception ex)
  211. {
  212. await observer.OnErrorAsync(ex).RendezVous(scheduler, ct);
  213. return;
  214. }
  215. if (b)
  216. {
  217. await observer.OnNextAsync(value).RendezVous(scheduler, ct);
  218. }
  219. else
  220. {
  221. break;
  222. }
  223. }
  224. }
  225. if (ct.IsCancellationRequested)
  226. return;
  227. var inner = await source.SubscribeSafeAsync(observer).ConfigureAwait(false);
  228. await subscription.AssignAsync(inner).RendezVous(scheduler, ct);
  229. }).ConfigureAwait(false);
  230. return StableCompositeAsyncDisposable.Create(task, subscription);
  231. }
  232. }
  233. }
  234. }