Count.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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<int> Count<TSource>(this IAsyncObservable<TSource> source)
  10. {
  11. if (source == null)
  12. throw new ArgumentNullException(nameof(source));
  13. return Create<TSource, int>(source, static (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Count<TSource>(observer)));
  14. }
  15. public static IAsyncObservable<int> Count<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 CreateAsyncObservable<int>.From(
  22. source,
  23. predicate,
  24. static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Count(observer, predicate)));
  25. }
  26. public static IAsyncObservable<int> Count<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)
  27. {
  28. if (source == null)
  29. throw new ArgumentNullException(nameof(source));
  30. if (predicate == null)
  31. throw new ArgumentNullException(nameof(predicate));
  32. return CreateAsyncObservable<int>.From(
  33. source,
  34. predicate,
  35. static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Count(observer, predicate)));
  36. }
  37. }
  38. public partial class AsyncObserver
  39. {
  40. public static IAsyncObserver<TSource> Count<TSource>(IAsyncObserver<int> observer)
  41. {
  42. if (observer == null)
  43. throw new ArgumentNullException(nameof(observer));
  44. var count = 0;
  45. return Create<TSource>(
  46. async x =>
  47. {
  48. try
  49. {
  50. checked
  51. {
  52. count++;
  53. }
  54. }
  55. catch (Exception ex)
  56. {
  57. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  58. return;
  59. }
  60. },
  61. observer.OnErrorAsync,
  62. async () =>
  63. {
  64. await observer.OnNextAsync(count).ConfigureAwait(false);
  65. await observer.OnCompletedAsync().ConfigureAwait(false);
  66. }
  67. );
  68. }
  69. public static IAsyncObserver<TSource> Count<TSource>(IAsyncObserver<int> observer, Func<TSource, 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 Where(Count<TSource>(observer), predicate);
  76. }
  77. public static IAsyncObserver<TSource> Count<TSource>(IAsyncObserver<int> observer, Func<TSource, ValueTask<bool>> predicate)
  78. {
  79. if (observer == null)
  80. throw new ArgumentNullException(nameof(observer));
  81. if (predicate == null)
  82. throw new ArgumentNullException(nameof(predicate));
  83. return Where(Count<TSource>(observer), predicate);
  84. }
  85. }
  86. }