Take.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. {
  30. return this;
  31. }
  32. else
  33. {
  34. return new Count(_source, count);
  35. }
  36. }
  37. protected override _ CreateSink(IObserver<TSource> observer) => new _(_count, observer);
  38. protected override void Run(_ sink) => sink.Run(_source);
  39. internal sealed class _ : IdentitySink<TSource>
  40. {
  41. private int _remaining;
  42. public _(int count, IObserver<TSource> observer)
  43. : base(observer)
  44. {
  45. _remaining = count;
  46. }
  47. public override void OnNext(TSource value)
  48. {
  49. if (_remaining > 0)
  50. {
  51. --_remaining;
  52. ForwardOnNext(value);
  53. if (_remaining == 0)
  54. {
  55. ForwardOnCompleted();
  56. }
  57. }
  58. }
  59. }
  60. }
  61. internal sealed class Time : Producer<TSource, Time._>
  62. {
  63. private readonly IObservable<TSource> _source;
  64. private readonly TimeSpan _duration;
  65. internal readonly IScheduler _scheduler;
  66. public Time(IObservable<TSource> source, TimeSpan duration, IScheduler scheduler)
  67. {
  68. _source = source;
  69. _duration = duration;
  70. _scheduler = scheduler;
  71. }
  72. public IObservable<TSource> Combine(TimeSpan duration)
  73. {
  74. //
  75. // Minimum semantics:
  76. //
  77. // t 0--1--2--3--4--5--6--7-> t 0--1--2--3--4--5--6--7->
  78. //
  79. // xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--|
  80. // xs.Take(5s) --o--o--o--o--o| xs.Take(3s) --o--o--o|
  81. // xs.Take(5s).Take(3s) --o--o--o| xs.Take(3s).Take(5s) --o--o--o|
  82. //
  83. if (_duration <= duration)
  84. {
  85. return this;
  86. }
  87. else
  88. {
  89. return new Time(_source, duration, _scheduler);
  90. }
  91. }
  92. protected override _ CreateSink(IObserver<TSource> observer) => new _(observer);
  93. protected override void Run(_ sink) => sink.Run(this);
  94. internal sealed class _ : IdentitySink<TSource>
  95. {
  96. public _(IObserver<TSource> observer)
  97. : base(observer)
  98. {
  99. }
  100. private object _gate;
  101. private IDisposable _task;
  102. public void Run(Time parent)
  103. {
  104. _gate = new object();
  105. Disposable.SetSingle(ref _task, parent._scheduler.ScheduleAction(this, parent._duration, state => state.Tick()));
  106. Run(parent._source);
  107. }
  108. protected override void Dispose(bool disposing)
  109. {
  110. if (disposing)
  111. {
  112. Disposable.TryDispose(ref _task);
  113. }
  114. base.Dispose(disposing);
  115. }
  116. private void Tick()
  117. {
  118. lock (_gate)
  119. {
  120. ForwardOnCompleted();
  121. }
  122. }
  123. public override void OnNext(TSource value)
  124. {
  125. lock (_gate)
  126. {
  127. ForwardOnNext(value);
  128. }
  129. }
  130. public override void OnError(Exception error)
  131. {
  132. lock (_gate)
  133. {
  134. ForwardOnError(error);
  135. }
  136. }
  137. public override void OnCompleted()
  138. {
  139. lock (_gate)
  140. {
  141. ForwardOnCompleted();
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }