Take.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.Reactive.Concurrency;
  5. using System.Reactive.Disposables;
  6. namespace System.Reactive.Linq.ObservableImpl
  7. {
  8. internal static class Take<TSource>
  9. {
  10. internal sealed class Count : Producer<TSource, Count._>
  11. {
  12. private readonly IObservable<TSource> _source;
  13. private readonly int _count;
  14. public Count(IObservable<TSource> source, int count)
  15. {
  16. _source = source;
  17. _count = count;
  18. }
  19. public IObservable<TSource> Combine(int count)
  20. {
  21. //
  22. // Minimum semantics:
  23. //
  24. // xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--|
  25. // xs.Take(5) --o--o--o--o--o| xs.Take(3) --o--o--o|
  26. // xs.Take(5).Take(3) --o--o--o| xs.Take(3).Take(5) --o--o--o|
  27. //
  28. if (_count <= count)
  29. return this;
  30. else
  31. return new Count(_source, count);
  32. }
  33. protected override _ CreateSink(IObserver<TSource> observer, IDisposable cancel) => new _(_count, observer, cancel);
  34. protected override IDisposable Run(_ sink) => _source.SubscribeSafe(sink);
  35. internal sealed class _ : IdentitySink<TSource>
  36. {
  37. private int _remaining;
  38. public _(int count, IObserver<TSource> observer, IDisposable cancel)
  39. : base(observer, cancel)
  40. {
  41. _remaining = count;
  42. }
  43. public override void OnNext(TSource value)
  44. {
  45. if (_remaining > 0)
  46. {
  47. --_remaining;
  48. ForwardOnNext(value);
  49. if (_remaining == 0)
  50. {
  51. ForwardOnCompleted();
  52. }
  53. }
  54. }
  55. }
  56. }
  57. internal sealed class Time : Producer<TSource, Time._>
  58. {
  59. private readonly IObservable<TSource> _source;
  60. private readonly TimeSpan _duration;
  61. internal readonly IScheduler _scheduler;
  62. public Time(IObservable<TSource> source, TimeSpan duration, IScheduler scheduler)
  63. {
  64. _source = source;
  65. _duration = duration;
  66. _scheduler = scheduler;
  67. }
  68. public IObservable<TSource> Combine(TimeSpan duration)
  69. {
  70. //
  71. // Minimum semantics:
  72. //
  73. // t 0--1--2--3--4--5--6--7-> t 0--1--2--3--4--5--6--7->
  74. //
  75. // xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--|
  76. // xs.Take(5s) --o--o--o--o--o| xs.Take(3s) --o--o--o|
  77. // xs.Take(5s).Take(3s) --o--o--o| xs.Take(3s).Take(5s) --o--o--o|
  78. //
  79. if (_duration <= duration)
  80. return this;
  81. else
  82. return new Time(_source, duration, _scheduler);
  83. }
  84. protected override _ CreateSink(IObserver<TSource> observer, IDisposable cancel) => new _(observer, cancel);
  85. protected override IDisposable Run(_ sink) => sink.Run(this);
  86. internal sealed class _ : IdentitySink<TSource>
  87. {
  88. public _(IObserver<TSource> observer, IDisposable cancel)
  89. : base(observer, cancel)
  90. {
  91. }
  92. private object _gate;
  93. public IDisposable Run(Time parent)
  94. {
  95. _gate = new object();
  96. var t = parent._scheduler.Schedule(parent._duration, Tick);
  97. var d = parent._source.SubscribeSafe(this);
  98. return StableCompositeDisposable.Create(t, d);
  99. }
  100. private void Tick()
  101. {
  102. lock (_gate)
  103. {
  104. ForwardOnCompleted();
  105. }
  106. }
  107. public override void OnNext(TSource value)
  108. {
  109. lock (_gate)
  110. {
  111. ForwardOnNext(value);
  112. }
  113. }
  114. public override void OnError(Exception error)
  115. {
  116. lock (_gate)
  117. {
  118. ForwardOnError(error);
  119. }
  120. }
  121. public override void OnCompleted()
  122. {
  123. lock (_gate)
  124. {
  125. ForwardOnCompleted();
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }