All.cs 4.1 KB

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