Aggregate.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.Threading.Tasks;
  5. namespace System.Reactive.Linq
  6. {
  7. partial class AsyncObservable
  8. {
  9. public static IAsyncObservable<TSource> Aggregate<TSource>(this IAsyncObservable<TSource> source, Func<TSource, TSource, TSource> func)
  10. {
  11. if (source == null)
  12. throw new ArgumentNullException(nameof(source));
  13. if (func == null)
  14. throw new ArgumentNullException(nameof(func));
  15. return Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.Aggregate(observer, func)));
  16. }
  17. public static IAsyncObservable<TSource> Aggregate<TSource>(this IAsyncObservable<TSource> source, Func<TSource, TSource, Task<TSource>> func)
  18. {
  19. if (source == null)
  20. throw new ArgumentNullException(nameof(source));
  21. if (func == null)
  22. throw new ArgumentNullException(nameof(func));
  23. return Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.Aggregate(observer, func)));
  24. }
  25. public static IAsyncObservable<TResult> Aggregate<TSource, TResult>(this IAsyncObservable<TSource> source, TResult seed, Func<TResult, TSource, TResult> func)
  26. {
  27. if (source == null)
  28. throw new ArgumentNullException(nameof(source));
  29. if (func == null)
  30. throw new ArgumentNullException(nameof(func));
  31. return Create<TResult>(observer => source.SubscribeSafeAsync(AsyncObserver.Aggregate(observer, seed, func)));
  32. }
  33. public static IAsyncObservable<TResult> Aggregate<TSource, TResult>(this IAsyncObservable<TSource> source, TResult seed, Func<TResult, TSource, Task<TResult>> func)
  34. {
  35. if (source == null)
  36. throw new ArgumentNullException(nameof(source));
  37. if (func == null)
  38. throw new ArgumentNullException(nameof(func));
  39. return Create<TResult>(observer => source.SubscribeSafeAsync(AsyncObserver.Aggregate(observer, seed, func)));
  40. }
  41. public static IAsyncObservable<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncObservable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector)
  42. {
  43. if (source == null)
  44. throw new ArgumentNullException(nameof(source));
  45. if (resultSelector == null)
  46. throw new ArgumentNullException(nameof(resultSelector));
  47. if (func == null)
  48. throw new ArgumentNullException(nameof(func));
  49. return Create<TResult>(observer => source.SubscribeSafeAsync(AsyncObserver.Aggregate(observer, seed, func, resultSelector)));
  50. }
  51. public static IAsyncObservable<TResult> Aggregate<TSource, TAccumulate, TResult>(this IAsyncObservable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, Task<TAccumulate>> func, Func<TAccumulate, Task<TResult>> resultSelector)
  52. {
  53. if (source == null)
  54. throw new ArgumentNullException(nameof(source));
  55. if (resultSelector == null)
  56. throw new ArgumentNullException(nameof(resultSelector));
  57. if (func == null)
  58. throw new ArgumentNullException(nameof(func));
  59. return Create<TResult>(observer => source.SubscribeSafeAsync(AsyncObserver.Aggregate(observer, seed, func, resultSelector)));
  60. }
  61. }
  62. partial class AsyncObserver
  63. {
  64. public static IAsyncObserver<TSource> Aggregate<TSource>(IAsyncObserver<TSource> observer, Func<TSource, TSource, TSource> func)
  65. {
  66. if (observer == null)
  67. throw new ArgumentNullException(nameof(observer));
  68. if (func == null)
  69. throw new ArgumentNullException(nameof(func));
  70. return Aggregate(observer, (a, x) => Task.FromResult(func(a, x)));
  71. }
  72. public static IAsyncObserver<TSource> Aggregate<TSource>(IAsyncObserver<TSource> observer, Func<TSource, TSource, Task<TSource>> func)
  73. {
  74. if (observer == null)
  75. throw new ArgumentNullException(nameof(observer));
  76. if (func == null)
  77. throw new ArgumentNullException(nameof(func));
  78. var hasValue = false;
  79. var value = default(TSource);
  80. return Create<TSource>(
  81. async x =>
  82. {
  83. if (hasValue)
  84. {
  85. try
  86. {
  87. value = await func(value, x).ConfigureAwait(false);
  88. }
  89. catch (Exception ex)
  90. {
  91. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  92. return;
  93. }
  94. }
  95. else
  96. {
  97. value = x;
  98. hasValue = true;
  99. }
  100. },
  101. observer.OnErrorAsync,
  102. async () =>
  103. {
  104. if (!hasValue)
  105. {
  106. await observer.OnErrorAsync(new InvalidOperationException("The sequence is empty.")).ConfigureAwait(false);
  107. }
  108. else
  109. {
  110. await observer.OnNextAsync(value).ConfigureAwait(false);
  111. await observer.OnCompletedAsync().ConfigureAwait(false);
  112. }
  113. }
  114. );
  115. }
  116. public static IAsyncObserver<TSource> Aggregate<TSource, TResult>(IAsyncObserver<TResult> observer, TResult seed, Func<TResult, TSource, TResult> func)
  117. {
  118. if (observer == null)
  119. throw new ArgumentNullException(nameof(observer));
  120. if (func == null)
  121. throw new ArgumentNullException(nameof(func));
  122. return Aggregate<TSource, TResult, TResult>(observer, seed, (a, x) => Task.FromResult(func(a, x)), (a) => Task.FromResult(a));
  123. }
  124. public static IAsyncObserver<TSource> Aggregate<TSource, TResult>(IAsyncObserver<TResult> observer, TResult seed, Func<TResult, TSource, Task<TResult>> func)
  125. {
  126. if (observer == null)
  127. throw new ArgumentNullException(nameof(observer));
  128. if (func == null)
  129. throw new ArgumentNullException(nameof(func));
  130. return Aggregate<TSource, TResult, TResult>(observer, seed, (a, x) => func(a, x), (a) => Task.FromResult(a));
  131. }
  132. public static IAsyncObserver<TSource> Aggregate<TSource, TAccumulate, TResult>(IAsyncObserver<TResult> observer, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate, TResult> resultSelector)
  133. {
  134. if (observer == null)
  135. throw new ArgumentNullException(nameof(observer));
  136. if (resultSelector == null)
  137. throw new ArgumentNullException(nameof(resultSelector));
  138. if (func == null)
  139. throw new ArgumentNullException(nameof(func));
  140. return Aggregate<TSource, TAccumulate, TResult>(observer, seed, (a, x) => Task.FromResult(func(a, x)), (a) => Task.FromResult(resultSelector(a)));
  141. }
  142. public static IAsyncObserver<TSource> Aggregate<TSource, TAccumulate, TResult>(IAsyncObserver<TResult> observer, TAccumulate seed, Func<TAccumulate, TSource, Task<TAccumulate>> func, Func<TAccumulate, Task<TResult>> resultSelector)
  143. {
  144. if (observer == null)
  145. throw new ArgumentNullException(nameof(observer));
  146. if (resultSelector == null)
  147. throw new ArgumentNullException(nameof(resultSelector));
  148. if (func == null)
  149. throw new ArgumentNullException(nameof(func));
  150. var value = seed;
  151. return Create<TSource>(
  152. async x =>
  153. {
  154. try
  155. {
  156. value = await func(value, x).ConfigureAwait(false);
  157. }
  158. catch (Exception ex)
  159. {
  160. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  161. return;
  162. }
  163. },
  164. observer.OnErrorAsync,
  165. async () =>
  166. {
  167. var res = default(TResult);
  168. try
  169. {
  170. res = await resultSelector(value).ConfigureAwait(false);
  171. }
  172. catch (Exception ex)
  173. {
  174. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  175. return;
  176. }
  177. await observer.OnNextAsync(res).ConfigureAwait(false);
  178. await observer.OnCompletedAsync().ConfigureAwait(false);
  179. }
  180. );
  181. }
  182. }
  183. }