Scan.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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> 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 Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.Scan(observer, func)));
  16. }
  17. public static IAsyncObservable<TSource> Scan<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.Scan(observer, func)));
  24. }
  25. public static IAsyncObservable<TResult> Scan<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.Scan(observer, seed, func)));
  32. }
  33. public static IAsyncObservable<TResult> Scan<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.Scan(observer, seed, func)));
  40. }
  41. }
  42. partial class AsyncObserver
  43. {
  44. public static IAsyncObserver<TSource> Scan<TSource>(IAsyncObserver<TSource> observer, Func<TSource, TSource, TSource> func)
  45. {
  46. if (observer == null)
  47. throw new ArgumentNullException(nameof(observer));
  48. if (func == null)
  49. throw new ArgumentNullException(nameof(func));
  50. var hasValue = false;
  51. var value = default(TSource);
  52. return Create<TSource>(
  53. async x =>
  54. {
  55. if (hasValue)
  56. {
  57. try
  58. {
  59. value = func(value, x);
  60. }
  61. catch (Exception ex)
  62. {
  63. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  64. return;
  65. }
  66. }
  67. else
  68. {
  69. value = x;
  70. hasValue = true;
  71. }
  72. await observer.OnNextAsync(value).ConfigureAwait(false);
  73. },
  74. observer.OnErrorAsync,
  75. observer.OnCompletedAsync
  76. );
  77. }
  78. public static IAsyncObserver<TSource> Scan<TSource>(IAsyncObserver<TSource> observer, Func<TSource, TSource, Task<TSource>> func)
  79. {
  80. if (observer == null)
  81. throw new ArgumentNullException(nameof(observer));
  82. if (func == null)
  83. throw new ArgumentNullException(nameof(func));
  84. var hasValue = false;
  85. var value = default(TSource);
  86. return Create<TSource>(
  87. async x =>
  88. {
  89. if (hasValue)
  90. {
  91. try
  92. {
  93. value = await func(value, x).ConfigureAwait(false);
  94. }
  95. catch (Exception ex)
  96. {
  97. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  98. return;
  99. }
  100. }
  101. else
  102. {
  103. value = x;
  104. hasValue = true;
  105. }
  106. await observer.OnNextAsync(value).ConfigureAwait(false);
  107. },
  108. observer.OnErrorAsync,
  109. observer.OnCompletedAsync
  110. );
  111. }
  112. public static IAsyncObserver<TSource> Scan<TSource, TResult>(IAsyncObserver<TResult> observer, TResult seed, Func<TResult, TSource, TResult> func)
  113. {
  114. if (observer == null)
  115. throw new ArgumentNullException(nameof(observer));
  116. if (func == null)
  117. throw new ArgumentNullException(nameof(func));
  118. var value = seed;
  119. return Create<TSource>(
  120. async x =>
  121. {
  122. try
  123. {
  124. value = func(value, x);
  125. }
  126. catch (Exception ex)
  127. {
  128. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  129. return;
  130. }
  131. await observer.OnNextAsync(value).ConfigureAwait(false);
  132. },
  133. observer.OnErrorAsync,
  134. observer.OnCompletedAsync
  135. );
  136. }
  137. public static IAsyncObserver<TSource> Scan<TSource, TResult>(IAsyncObserver<TResult> observer, TResult seed, Func<TResult, TSource, Task<TResult>> func)
  138. {
  139. if (observer == null)
  140. throw new ArgumentNullException(nameof(observer));
  141. if (func == null)
  142. throw new ArgumentNullException(nameof(func));
  143. var value = seed;
  144. return Create<TSource>(
  145. async x =>
  146. {
  147. try
  148. {
  149. value = await func(value, x).ConfigureAwait(false);
  150. }
  151. catch (Exception ex)
  152. {
  153. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  154. return;
  155. }
  156. await observer.OnNextAsync(value).ConfigureAwait(false);
  157. },
  158. observer.OnErrorAsync,
  159. observer.OnCompletedAsync
  160. );
  161. }
  162. }
  163. }