Scan.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. public partial class AsyncObservable
  8. {
  9. public static IAsyncObservable<TSource> Scan<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 CreateAsyncObservable<TSource>.From(
  16. source,
  17. func,
  18. static (source, func, observer) => source.SubscribeSafeAsync(AsyncObserver.Scan(observer, func)));
  19. }
  20. public static IAsyncObservable<TSource> Scan<TSource>(this IAsyncObservable<TSource> source, Func<TSource, TSource, ValueTask<TSource>> func)
  21. {
  22. if (source == null)
  23. throw new ArgumentNullException(nameof(source));
  24. if (func == null)
  25. throw new ArgumentNullException(nameof(func));
  26. return CreateAsyncObservable<TSource>.From(
  27. source,
  28. func,
  29. static (source, func, observer) => source.SubscribeSafeAsync(AsyncObserver.Scan(observer, func)));
  30. }
  31. public static IAsyncObservable<TResult> Scan<TSource, TResult>(this IAsyncObservable<TSource> source, TResult seed, Func<TResult, TSource, TResult> func)
  32. {
  33. if (source == null)
  34. throw new ArgumentNullException(nameof(source));
  35. if (func == null)
  36. throw new ArgumentNullException(nameof(func));
  37. return CreateAsyncObservable<TResult>.From(
  38. source,
  39. (func, seed),
  40. static (source, state, observer) => source.SubscribeSafeAsync(AsyncObserver.Scan(observer, state.seed, state.func)));
  41. }
  42. public static IAsyncObservable<TResult> Scan<TSource, TResult>(this IAsyncObservable<TSource> source, TResult seed, Func<TResult, TSource, ValueTask<TResult>> func)
  43. {
  44. if (source == null)
  45. throw new ArgumentNullException(nameof(source));
  46. if (func == null)
  47. throw new ArgumentNullException(nameof(func));
  48. return CreateAsyncObservable<TResult>.From(
  49. source,
  50. (func, seed),
  51. static (source, state, observer) => source.SubscribeSafeAsync(AsyncObserver.Scan(observer, state.seed, state.func)));
  52. }
  53. }
  54. public partial class AsyncObserver
  55. {
  56. public static IAsyncObserver<TSource> Scan<TSource>(IAsyncObserver<TSource> observer, Func<TSource, TSource, TSource> func)
  57. {
  58. if (observer == null)
  59. throw new ArgumentNullException(nameof(observer));
  60. if (func == null)
  61. throw new ArgumentNullException(nameof(func));
  62. var hasValue = false;
  63. var value = default(TSource);
  64. return Create<TSource>(
  65. async x =>
  66. {
  67. if (hasValue)
  68. {
  69. try
  70. {
  71. value = func(value, x);
  72. }
  73. catch (Exception ex)
  74. {
  75. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  76. return;
  77. }
  78. }
  79. else
  80. {
  81. value = x;
  82. hasValue = true;
  83. }
  84. await observer.OnNextAsync(value).ConfigureAwait(false);
  85. },
  86. observer.OnErrorAsync,
  87. observer.OnCompletedAsync
  88. );
  89. }
  90. public static IAsyncObserver<TSource> Scan<TSource>(IAsyncObserver<TSource> observer, Func<TSource, TSource, ValueTask<TSource>> func)
  91. {
  92. if (observer == null)
  93. throw new ArgumentNullException(nameof(observer));
  94. if (func == null)
  95. throw new ArgumentNullException(nameof(func));
  96. var hasValue = false;
  97. var value = default(TSource);
  98. return Create<TSource>(
  99. async x =>
  100. {
  101. if (hasValue)
  102. {
  103. try
  104. {
  105. value = await func(value, x).ConfigureAwait(false);
  106. }
  107. catch (Exception ex)
  108. {
  109. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  110. return;
  111. }
  112. }
  113. else
  114. {
  115. value = x;
  116. hasValue = true;
  117. }
  118. await observer.OnNextAsync(value).ConfigureAwait(false);
  119. },
  120. observer.OnErrorAsync,
  121. observer.OnCompletedAsync
  122. );
  123. }
  124. public static IAsyncObserver<TSource> Scan<TSource, TResult>(IAsyncObserver<TResult> observer, TResult seed, Func<TResult, TSource, TResult> func)
  125. {
  126. if (observer == null)
  127. throw new ArgumentNullException(nameof(observer));
  128. if (func == null)
  129. throw new ArgumentNullException(nameof(func));
  130. var value = seed;
  131. return Create<TSource>(
  132. async x =>
  133. {
  134. try
  135. {
  136. value = func(value, x);
  137. }
  138. catch (Exception ex)
  139. {
  140. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  141. return;
  142. }
  143. await observer.OnNextAsync(value).ConfigureAwait(false);
  144. },
  145. observer.OnErrorAsync,
  146. observer.OnCompletedAsync
  147. );
  148. }
  149. public static IAsyncObserver<TSource> Scan<TSource, TResult>(IAsyncObserver<TResult> observer, TResult seed, Func<TResult, TSource, ValueTask<TResult>> func)
  150. {
  151. if (observer == null)
  152. throw new ArgumentNullException(nameof(observer));
  153. if (func == null)
  154. throw new ArgumentNullException(nameof(func));
  155. var value = seed;
  156. return Create<TSource>(
  157. async x =>
  158. {
  159. try
  160. {
  161. value = await func(value, x).ConfigureAwait(false);
  162. }
  163. catch (Exception ex)
  164. {
  165. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  166. return;
  167. }
  168. await observer.OnNextAsync(value).ConfigureAwait(false);
  169. },
  170. observer.OnErrorAsync,
  171. observer.OnCompletedAsync
  172. );
  173. }
  174. }
  175. }