LongCount.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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<long> LongCount<TSource>(this IAsyncObservable<TSource> source)
  10. {
  11. if (source == null)
  12. throw new ArgumentNullException(nameof(source));
  13. return Create<long>(observer => source.SubscribeSafeAsync(AsyncObserver.LongCount<TSource>(observer)));
  14. }
  15. public static IAsyncObservable<long> LongCount<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<long>(observer => source.SubscribeSafeAsync(AsyncObserver.LongCount(observer, predicate)));
  22. }
  23. public static IAsyncObservable<long> LongCount<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<long>(observer => source.SubscribeSafeAsync(AsyncObserver.LongCount(observer, predicate)));
  30. }
  31. }
  32. partial class AsyncObserver
  33. {
  34. public static IAsyncObserver<TSource> LongCount<TSource>(IAsyncObserver<long> observer)
  35. {
  36. if (observer == null)
  37. throw new ArgumentNullException(nameof(observer));
  38. var count = 0L;
  39. return Create<TSource>(
  40. async x =>
  41. {
  42. try
  43. {
  44. checked
  45. {
  46. count++;
  47. }
  48. }
  49. catch (Exception ex)
  50. {
  51. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  52. return;
  53. }
  54. },
  55. observer.OnErrorAsync,
  56. async () =>
  57. {
  58. await observer.OnNextAsync(count).ConfigureAwait(false);
  59. await observer.OnCompletedAsync().ConfigureAwait(false);
  60. }
  61. );
  62. }
  63. public static IAsyncObserver<TSource> LongCount<TSource>(IAsyncObserver<long> observer, Func<TSource, bool> predicate)
  64. {
  65. if (observer == null)
  66. throw new ArgumentNullException(nameof(observer));
  67. if (predicate == null)
  68. throw new ArgumentNullException(nameof(predicate));
  69. return Where(LongCount<TSource>(observer), predicate);
  70. }
  71. public static IAsyncObserver<TSource> LongCount<TSource>(IAsyncObserver<long> observer, Func<TSource, Task<bool>> predicate)
  72. {
  73. if (observer == null)
  74. throw new ArgumentNullException(nameof(observer));
  75. if (predicate == null)
  76. throw new ArgumentNullException(nameof(predicate));
  77. return Where(LongCount<TSource>(observer), predicate);
  78. }
  79. }
  80. }