Any.cs 4.9 KB

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