Where.cs 6.9 KB

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