TakeLast.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.Collections.Generic;
  5. using System.Reactive.Concurrency;
  6. using System.Reactive.Disposables;
  7. namespace System.Reactive.Linq.ObservableImpl
  8. {
  9. internal static class TakeLast<TSource>
  10. {
  11. internal sealed class Count : Producer<TSource, Count._>
  12. {
  13. private readonly IObservable<TSource> _source;
  14. private readonly int _count;
  15. private readonly IScheduler _loopScheduler;
  16. public Count(IObservable<TSource> source, int count, IScheduler loopScheduler)
  17. {
  18. _source = source;
  19. _count = count;
  20. _loopScheduler = loopScheduler;
  21. }
  22. protected override _ CreateSink(IObserver<TSource> observer) => new _(this, observer);
  23. protected override void Run(_ sink) => sink.Run(_source);
  24. internal sealed class _ : IdentitySink<TSource>
  25. {
  26. private readonly int _count;
  27. private readonly IScheduler _loopScheduler;
  28. private readonly Queue<TSource> _queue;
  29. public _(Count parent, IObserver<TSource> observer)
  30. : base(observer)
  31. {
  32. _count = parent._count;
  33. _loopScheduler = parent._loopScheduler;
  34. _queue = new Queue<TSource>();
  35. }
  36. private IDisposable _loopDisposable;
  37. protected override void Dispose(bool disposing)
  38. {
  39. if (disposing)
  40. {
  41. Disposable.TryDispose(ref _loopDisposable);
  42. }
  43. base.Dispose(disposing);
  44. }
  45. public override void OnNext(TSource value)
  46. {
  47. _queue.Enqueue(value);
  48. if (_queue.Count > _count)
  49. {
  50. _queue.Dequeue();
  51. }
  52. }
  53. public override void OnCompleted()
  54. {
  55. DisposeUpstream();
  56. var longRunning = _loopScheduler.AsLongRunning();
  57. if (longRunning != null)
  58. {
  59. Disposable.SetSingle(ref _loopDisposable, longRunning.ScheduleLongRunning(this, (@this, c) => @this.Loop(c)));
  60. }
  61. else
  62. {
  63. var first = _loopScheduler.Schedule(this, (innerScheduler, @this) => @this.LoopRec(innerScheduler));
  64. Disposable.TrySetSingle(ref _loopDisposable, first);
  65. }
  66. }
  67. private IDisposable LoopRec(IScheduler scheduler)
  68. {
  69. if (_queue.Count > 0)
  70. {
  71. ForwardOnNext(_queue.Dequeue());
  72. var next = scheduler.Schedule(this, (innerScheduler, @this) => @this.LoopRec(innerScheduler));
  73. Disposable.TrySetMultiple(ref _loopDisposable, next);
  74. }
  75. else
  76. {
  77. ForwardOnCompleted();
  78. }
  79. return Disposable.Empty;
  80. }
  81. private void Loop(ICancelable cancel)
  82. {
  83. var n = _queue.Count;
  84. while (!cancel.IsDisposed)
  85. {
  86. if (n == 0)
  87. {
  88. ForwardOnCompleted();
  89. break;
  90. }
  91. ForwardOnNext(_queue.Dequeue());
  92. n--;
  93. }
  94. Dispose();
  95. }
  96. }
  97. }
  98. internal sealed class Time : Producer<TSource, Time._>
  99. {
  100. private readonly IObservable<TSource> _source;
  101. private readonly TimeSpan _duration;
  102. private readonly IScheduler _scheduler;
  103. private readonly IScheduler _loopScheduler;
  104. public Time(IObservable<TSource> source, TimeSpan duration, IScheduler scheduler, IScheduler loopScheduler)
  105. {
  106. _source = source;
  107. _duration = duration;
  108. _scheduler = scheduler;
  109. _loopScheduler = loopScheduler;
  110. }
  111. protected override _ CreateSink(IObserver<TSource> observer) => new _(this, observer);
  112. protected override void Run(_ sink) => sink.Run(_source, _scheduler);
  113. internal sealed class _ : IdentitySink<TSource>
  114. {
  115. private readonly TimeSpan _duration;
  116. private readonly IScheduler _loopScheduler;
  117. private readonly Queue<Reactive.TimeInterval<TSource>> _queue;
  118. public _(Time parent, IObserver<TSource> observer)
  119. : base(observer)
  120. {
  121. _duration = parent._duration;
  122. _loopScheduler = parent._loopScheduler;
  123. _queue = new Queue<Reactive.TimeInterval<TSource>>();
  124. }
  125. private IDisposable _loopDisposable;
  126. private IStopwatch _watch;
  127. public void Run(IObservable<TSource> source, IScheduler scheduler)
  128. {
  129. _watch = scheduler.StartStopwatch();
  130. Run(source);
  131. }
  132. protected override void Dispose(bool disposing)
  133. {
  134. if (disposing)
  135. {
  136. Disposable.TryDispose(ref _loopDisposable);
  137. }
  138. base.Dispose(disposing);
  139. }
  140. public override void OnNext(TSource value)
  141. {
  142. var now = _watch.Elapsed;
  143. _queue.Enqueue(new Reactive.TimeInterval<TSource>(value, now));
  144. Trim(now);
  145. }
  146. public override void OnCompleted()
  147. {
  148. DisposeUpstream();
  149. var now = _watch.Elapsed;
  150. Trim(now);
  151. var longRunning = _loopScheduler.AsLongRunning();
  152. if (longRunning != null)
  153. {
  154. Disposable.SetSingle(ref _loopDisposable, longRunning.ScheduleLongRunning(this, (@this, c) => @this.Loop(c)));
  155. }
  156. else
  157. {
  158. var first = _loopScheduler.Schedule(this, (innerScheduler, @this) => @this.LoopRec(innerScheduler));
  159. Disposable.TrySetSingle(ref _loopDisposable, first);
  160. }
  161. }
  162. private IDisposable LoopRec(IScheduler scheduler)
  163. {
  164. if (_queue.Count > 0)
  165. {
  166. ForwardOnNext(_queue.Dequeue().Value);
  167. var next = scheduler.Schedule(this, (innerScheduler, @this) => @this.LoopRec(innerScheduler));
  168. Disposable.TrySetMultiple(ref _loopDisposable, next);
  169. }
  170. else
  171. {
  172. ForwardOnCompleted();
  173. }
  174. return Disposable.Empty;
  175. }
  176. private void Loop(ICancelable cancel)
  177. {
  178. var n = _queue.Count;
  179. while (!cancel.IsDisposed)
  180. {
  181. if (n == 0)
  182. {
  183. ForwardOnCompleted();
  184. break;
  185. }
  186. ForwardOnNext(_queue.Dequeue().Value);
  187. n--;
  188. }
  189. Dispose();
  190. }
  191. private void Trim(TimeSpan now)
  192. {
  193. while (_queue.Count > 0 && now - _queue.Peek().Interval >= _duration)
  194. {
  195. _queue.Dequeue();
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }