SkipWhile.cs 6.9 KB

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