SkipLast.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #if !NO_PERF
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Reactive.Concurrency;
  8. namespace System.Reactive.Linq.ObservableImpl
  9. {
  10. class SkipLast<TSource> : Producer<TSource>
  11. {
  12. private readonly IObservable<TSource> _source;
  13. private readonly int _count;
  14. private readonly TimeSpan _duration;
  15. private readonly IScheduler _scheduler;
  16. public SkipLast(IObservable<TSource> source, int count)
  17. {
  18. _source = source;
  19. _count = count;
  20. }
  21. public SkipLast(IObservable<TSource> source, TimeSpan duration, IScheduler scheduler)
  22. {
  23. _source = source;
  24. _duration = duration;
  25. _scheduler = scheduler;
  26. }
  27. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  28. {
  29. if (_scheduler == null)
  30. {
  31. var sink = new _(this, observer, cancel);
  32. setSink(sink);
  33. return _source.SubscribeSafe(sink);
  34. }
  35. else
  36. {
  37. var sink = new SkipLastImpl(this, observer, cancel);
  38. setSink(sink);
  39. return sink.Run();
  40. }
  41. }
  42. class _ : Sink<TSource>, IObserver<TSource>
  43. {
  44. private readonly SkipLast<TSource> _parent;
  45. private Queue<TSource> _queue;
  46. public _(SkipLast<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  47. : base(observer, cancel)
  48. {
  49. _parent = parent;
  50. _queue = new Queue<TSource>();
  51. }
  52. public void OnNext(TSource value)
  53. {
  54. _queue.Enqueue(value);
  55. if (_queue.Count > _parent._count)
  56. base._observer.OnNext(_queue.Dequeue());
  57. }
  58. public void OnError(Exception error)
  59. {
  60. base._observer.OnError(error);
  61. base.Dispose();
  62. }
  63. public void OnCompleted()
  64. {
  65. base._observer.OnCompleted();
  66. base.Dispose();
  67. }
  68. }
  69. class SkipLastImpl : Sink<TSource>, IObserver<TSource>
  70. {
  71. private readonly SkipLast<TSource> _parent;
  72. private Queue<System.Reactive.TimeInterval<TSource>> _queue;
  73. public SkipLastImpl(SkipLast<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  74. : base(observer, cancel)
  75. {
  76. _parent = parent;
  77. _queue = new Queue<System.Reactive.TimeInterval<TSource>>();
  78. }
  79. private IStopwatch _watch;
  80. public IDisposable Run()
  81. {
  82. _watch = _parent._scheduler.StartStopwatch();
  83. return _parent._source.SubscribeSafe(this);
  84. }
  85. public void OnNext(TSource value)
  86. {
  87. var now = _watch.Elapsed;
  88. _queue.Enqueue(new System.Reactive.TimeInterval<TSource>(value, now));
  89. while (_queue.Count > 0 && now - _queue.Peek().Interval >= _parent._duration)
  90. base._observer.OnNext(_queue.Dequeue().Value);
  91. }
  92. public void OnError(Exception error)
  93. {
  94. base._observer.OnError(error);
  95. base.Dispose();
  96. }
  97. public void OnCompleted()
  98. {
  99. var now = _watch.Elapsed;
  100. while (_queue.Count > 0 && now - _queue.Peek().Interval >= _parent._duration)
  101. base._observer.OnNext(_queue.Dequeue().Value);
  102. base._observer.OnCompleted();
  103. base.Dispose();
  104. }
  105. }
  106. }
  107. }
  108. #endif