All.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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> All<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 CreateAsyncObservable<bool>.From(
  16. source,
  17. predicate,
  18. static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.All(observer, predicate)));
  19. }
  20. public static IAsyncObservable<bool> All<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)
  21. {
  22. if (source == null)
  23. throw new ArgumentNullException(nameof(source));
  24. if (predicate == null)
  25. throw new ArgumentNullException(nameof(predicate));
  26. return CreateAsyncObservable<bool>.From(
  27. source,
  28. predicate,
  29. static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.All(observer, predicate)));
  30. }
  31. }
  32. public partial class AsyncObserver
  33. {
  34. public static IAsyncObserver<TSource> All<TSource>(IAsyncObserver<bool> observer, Func<TSource, bool> predicate)
  35. {
  36. if (observer == null)
  37. throw new ArgumentNullException(nameof(observer));
  38. if (predicate == null)
  39. throw new ArgumentNullException(nameof(predicate));
  40. return Create<TSource>(
  41. async x =>
  42. {
  43. var b = default(bool);
  44. try
  45. {
  46. b = predicate(x);
  47. }
  48. catch (Exception ex)
  49. {
  50. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  51. return;
  52. }
  53. if (!b)
  54. {
  55. await observer.OnNextAsync(false).ConfigureAwait(false);
  56. await observer.OnCompletedAsync().ConfigureAwait(false);
  57. }
  58. },
  59. observer.OnErrorAsync,
  60. async () =>
  61. {
  62. await observer.OnNextAsync(true).ConfigureAwait(false);
  63. await observer.OnCompletedAsync().ConfigureAwait(false);
  64. }
  65. );
  66. }
  67. public static IAsyncObserver<TSource> All<TSource>(IAsyncObserver<bool> observer, Func<TSource, ValueTask<bool>> predicate)
  68. {
  69. if (observer == null)
  70. throw new ArgumentNullException(nameof(observer));
  71. if (predicate == null)
  72. throw new ArgumentNullException(nameof(predicate));
  73. return Create<TSource>(
  74. async x =>
  75. {
  76. var b = default(bool);
  77. try
  78. {
  79. b = await predicate(x).ConfigureAwait(false);
  80. }
  81. catch (Exception ex)
  82. {
  83. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  84. return;
  85. }
  86. if (!b)
  87. {
  88. await observer.OnNextAsync(false).ConfigureAwait(false);
  89. await observer.OnCompletedAsync().ConfigureAwait(false);
  90. }
  91. },
  92. observer.OnErrorAsync,
  93. async () =>
  94. {
  95. await observer.OnNextAsync(true).ConfigureAwait(false);
  96. await observer.OnCompletedAsync().ConfigureAwait(false);
  97. }
  98. );
  99. }
  100. }
  101. }