LongCount.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. namespace System.Reactive.Linq.ObservableImpl
  5. {
  6. internal static class LongCount<TSource>
  7. {
  8. internal sealed class All : Producer<long>
  9. {
  10. private readonly IObservable<TSource> _source;
  11. public All(IObservable<TSource> source)
  12. {
  13. _source = source;
  14. }
  15. protected override IDisposable CreateSink(IObserver<long> observer, IDisposable cancel) => new _(observer, cancel);
  16. protected override IDisposable Run(IObserver<long> observer, IDisposable cancel, Action<IDisposable> setSink)
  17. {
  18. var sink = new _(observer, cancel);
  19. setSink(sink);
  20. return _source.SubscribeSafe(sink);
  21. }
  22. private sealed class _ : Sink<long>, IObserver<TSource>
  23. {
  24. private long _count;
  25. public _(IObserver<long> observer, IDisposable cancel)
  26. : base(observer, cancel)
  27. {
  28. _count = 0L;
  29. }
  30. public void OnNext(TSource value)
  31. {
  32. try
  33. {
  34. checked
  35. {
  36. _count++;
  37. }
  38. }
  39. catch (Exception ex)
  40. {
  41. base._observer.OnError(ex);
  42. base.Dispose();
  43. }
  44. }
  45. public void OnError(Exception error)
  46. {
  47. base._observer.OnError(error);
  48. base.Dispose();
  49. }
  50. public void OnCompleted()
  51. {
  52. base._observer.OnNext(_count);
  53. base._observer.OnCompleted();
  54. base.Dispose();
  55. }
  56. }
  57. }
  58. internal sealed class Predicate : Producer<long>
  59. {
  60. private readonly IObservable<TSource> _source;
  61. private readonly Func<TSource, bool> _predicate;
  62. public Predicate(IObservable<TSource> source, Func<TSource, bool> predicate)
  63. {
  64. _source = source;
  65. _predicate = predicate;
  66. }
  67. protected override IDisposable CreateSink(IObserver<long> observer, IDisposable cancel) => new _(_predicate, observer, cancel);
  68. protected override IDisposable Run(IObserver<long> observer, IDisposable cancel, Action<IDisposable> setSink)
  69. {
  70. var sink = new _(_predicate, observer, cancel);
  71. setSink(sink);
  72. return _source.SubscribeSafe(sink);
  73. }
  74. private sealed class _ : Sink<long>, IObserver<TSource>
  75. {
  76. private readonly Func<TSource, bool> _predicate;
  77. private long _count;
  78. public _(Func<TSource, bool> predicate, IObserver<long> observer, IDisposable cancel)
  79. : base(observer, cancel)
  80. {
  81. _predicate = predicate;
  82. _count = 0L;
  83. }
  84. public void OnNext(TSource value)
  85. {
  86. try
  87. {
  88. checked
  89. {
  90. if (_predicate(value))
  91. {
  92. _count++;
  93. }
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. base._observer.OnError(ex);
  99. base.Dispose();
  100. }
  101. }
  102. public void OnError(Exception error)
  103. {
  104. base._observer.OnError(error);
  105. base.Dispose();
  106. }
  107. public void OnCompleted()
  108. {
  109. base._observer.OnNext(_count);
  110. base._observer.OnCompleted();
  111. base.Dispose();
  112. }
  113. }
  114. }
  115. }
  116. }