Any.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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<bool> Any<TSource>(this IAsyncObservable<TSource> source)
  10. {
  11. if (source == null)
  12. throw new ArgumentNullException(nameof(source));
  13. return Create<TSource, bool>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Any<TSource>(observer)));
  14. }
  15. public static IAsyncObservable<bool> Any<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate)
  16. {
  17. if (source == null)
  18. throw new ArgumentNullException(nameof(source));
  19. if (predicate == null)
  20. throw new ArgumentNullException(nameof(predicate));
  21. return Create(
  22. source,
  23. predicate,
  24. default(bool),
  25. (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Any(observer, predicate)));
  26. }
  27. public static IAsyncObservable<bool> Any<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)
  28. {
  29. if (source == null)
  30. throw new ArgumentNullException(nameof(source));
  31. if (predicate == null)
  32. throw new ArgumentNullException(nameof(predicate));
  33. return Create(
  34. source,
  35. predicate,
  36. default(bool),
  37. (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Any(observer, predicate)));
  38. }
  39. }
  40. public partial class AsyncObserver
  41. {
  42. public static IAsyncObserver<TSource> Any<TSource>(IAsyncObserver<bool> observer)
  43. {
  44. if (observer == null)
  45. throw new ArgumentNullException(nameof(observer));
  46. return Create<TSource>(
  47. async x =>
  48. {
  49. await observer.OnNextAsync(true).ConfigureAwait(false);
  50. await observer.OnCompletedAsync().ConfigureAwait(false);
  51. },
  52. observer.OnErrorAsync,
  53. async () =>
  54. {
  55. await observer.OnNextAsync(false).ConfigureAwait(false);
  56. await observer.OnCompletedAsync().ConfigureAwait(false);
  57. }
  58. );
  59. }
  60. public static IAsyncObserver<TSource> Any<TSource>(IAsyncObserver<bool> observer, Func<TSource, bool> predicate)
  61. {
  62. if (observer == null)
  63. throw new ArgumentNullException(nameof(observer));
  64. if (predicate == null)
  65. throw new ArgumentNullException(nameof(predicate));
  66. return Create<TSource>(
  67. async x =>
  68. {
  69. var b = default(bool);
  70. try
  71. {
  72. b = predicate(x);
  73. }
  74. catch (Exception ex)
  75. {
  76. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  77. return;
  78. }
  79. if (b)
  80. {
  81. await observer.OnNextAsync(true).ConfigureAwait(false);
  82. await observer.OnCompletedAsync().ConfigureAwait(false);
  83. }
  84. },
  85. observer.OnErrorAsync,
  86. async () =>
  87. {
  88. await observer.OnNextAsync(false).ConfigureAwait(false);
  89. await observer.OnCompletedAsync().ConfigureAwait(false);
  90. }
  91. );
  92. }
  93. public static IAsyncObserver<TSource> Any<TSource>(IAsyncObserver<bool> observer, Func<TSource, ValueTask<bool>> predicate)
  94. {
  95. if (observer == null)
  96. throw new ArgumentNullException(nameof(observer));
  97. if (predicate == null)
  98. throw new ArgumentNullException(nameof(predicate));
  99. return Create<TSource>(
  100. async x =>
  101. {
  102. var b = default(bool);
  103. try
  104. {
  105. b = await predicate(x).ConfigureAwait(false);
  106. }
  107. catch (Exception ex)
  108. {
  109. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  110. return;
  111. }
  112. if (b)
  113. {
  114. await observer.OnNextAsync(true).ConfigureAwait(false);
  115. await observer.OnCompletedAsync().ConfigureAwait(false);
  116. }
  117. },
  118. observer.OnErrorAsync,
  119. async () =>
  120. {
  121. await observer.OnNextAsync(false).ConfigureAwait(false);
  122. await observer.OnCompletedAsync().ConfigureAwait(false);
  123. }
  124. );
  125. }
  126. }
  127. }