All.cs 3.8 KB

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