Where.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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> 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 Create<TSource>(observer => source.SubscribeSafeAsync(AsyncObserver.Where(observer, predicate)));
  16. }
  17. public static IAsyncObservable<TSource> Where<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.Where(observer, predicate)));
  24. }
  25. public static IAsyncObservable<TSource> Where<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.Where(observer, predicate)));
  32. }
  33. public static IAsyncObservable<TSource> Where<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.Where(observer, predicate)));
  40. }
  41. }
  42. partial class AsyncObserver
  43. {
  44. public static IAsyncObserver<TSource> Where<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. },
  68. observer.OnErrorAsync,
  69. observer.OnCompletedAsync
  70. );
  71. }
  72. public static IAsyncObserver<TSource> Where<TSource>(IAsyncObserver<TSource> observer, Func<TSource, Task<bool>> predicate)
  73. {
  74. if (observer == null)
  75. throw new ArgumentNullException(nameof(observer));
  76. if (predicate == null)
  77. throw new ArgumentNullException(nameof(predicate));
  78. return Create<TSource>(
  79. async x =>
  80. {
  81. bool b;
  82. try
  83. {
  84. b = await predicate(x).ConfigureAwait(false);
  85. }
  86. catch (Exception ex)
  87. {
  88. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  89. return;
  90. }
  91. if (b)
  92. {
  93. await observer.OnNextAsync(x).ConfigureAwait(false);
  94. }
  95. },
  96. observer.OnErrorAsync,
  97. observer.OnCompletedAsync
  98. );
  99. }
  100. public static IAsyncObserver<TSource> Where<TSource>(IAsyncObserver<TSource> observer, Func<TSource, int, bool> predicate)
  101. {
  102. if (observer == null)
  103. throw new ArgumentNullException(nameof(observer));
  104. if (predicate == null)
  105. throw new ArgumentNullException(nameof(predicate));
  106. int i = 0;
  107. return Create<TSource>(
  108. async x =>
  109. {
  110. bool b;
  111. try
  112. {
  113. b = predicate(x, checked(i++));
  114. }
  115. catch (Exception ex)
  116. {
  117. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  118. return;
  119. }
  120. if (b)
  121. {
  122. await observer.OnNextAsync(x).ConfigureAwait(false);
  123. }
  124. },
  125. observer.OnErrorAsync,
  126. observer.OnCompletedAsync
  127. );
  128. }
  129. public static IAsyncObserver<TSource> Where<TSource>(IAsyncObserver<TSource> observer, Func<TSource, int, Task<bool>> predicate)
  130. {
  131. if (observer == null)
  132. throw new ArgumentNullException(nameof(observer));
  133. if (predicate == null)
  134. throw new ArgumentNullException(nameof(predicate));
  135. int i = 0;
  136. return Create<TSource>(
  137. async x =>
  138. {
  139. bool b;
  140. try
  141. {
  142. b = await predicate(x, checked(i++)).ConfigureAwait(false);
  143. }
  144. catch (Exception ex)
  145. {
  146. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  147. return;
  148. }
  149. if (b)
  150. {
  151. await observer.OnNextAsync(x).ConfigureAwait(false);
  152. }
  153. },
  154. observer.OnErrorAsync,
  155. observer.OnCompletedAsync
  156. );
  157. }
  158. }
  159. }