Any.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 Any<TSource>(observer, x => Task.FromResult(predicate(x)));
  59. }
  60. public static IAsyncObserver<TSource> Any<TSource>(IAsyncObserver<bool> observer, Func<TSource, Task<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 = await predicate(x).ConfigureAwait(false);
  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. }
  94. }