TakeWhile.cs 7.0 KB

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