Any.cs 5.2 KB

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