Skip.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 Skip<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. // Sum semantics:
  23. //
  24. // xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--|
  25. // xs.Skip(2) --x--x--o--o--o--o--| xs.Skip(3) --x--x--x--o--o--o--|
  26. // xs.Skip(2).Skip(3) --------x--x--x--o--| xs.Skip(3).Skip(2) -----------x--x--o--|
  27. //
  28. return new Count(_source, _count + count);
  29. }
  30. protected override _ CreateSink(IObserver<TSource> observer) => new _(_count, observer);
  31. protected override void Run(_ sink) => sink.Run(_source);
  32. internal sealed class _ : IdentitySink<TSource>
  33. {
  34. private int _remaining;
  35. public _(int count, IObserver<TSource> observer)
  36. : base(observer)
  37. {
  38. _remaining = count;
  39. }
  40. public override void OnNext(TSource value)
  41. {
  42. if (_remaining <= 0)
  43. {
  44. ForwardOnNext(value);
  45. }
  46. else
  47. {
  48. _remaining--;
  49. }
  50. }
  51. }
  52. }
  53. internal sealed class Time : Producer<TSource, Time._>
  54. {
  55. private readonly IObservable<TSource> _source;
  56. private readonly TimeSpan _duration;
  57. internal readonly IScheduler _scheduler;
  58. public Time(IObservable<TSource> source, TimeSpan duration, IScheduler scheduler)
  59. {
  60. _source = source;
  61. _duration = duration;
  62. _scheduler = scheduler;
  63. }
  64. public IObservable<TSource> Combine(TimeSpan duration)
  65. {
  66. //
  67. // Maximum semantics:
  68. //
  69. // t 0--1--2--3--4--5--6--7-> t 0--1--2--3--4--5--6--7->
  70. //
  71. // xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--|
  72. // xs.Skip(2s) xxxxxxx-o--o--o--o--| xs.Skip(3s) xxxxxxxxxx-o--o--o--|
  73. // xs.Skip(2s).Skip(3s) xxxxxxxxxx-o--o--o--| xs.Skip(3s).Skip(2s) xxxxxxx----o--o--o--|
  74. //
  75. if (duration <= _duration)
  76. {
  77. return this;
  78. }
  79. return new Time(_source, duration, _scheduler);
  80. }
  81. protected override _ CreateSink(IObserver<TSource> observer) => new _(observer);
  82. protected override void Run(_ sink) => sink.Run(this);
  83. internal sealed class _ : IdentitySink<TSource>
  84. {
  85. private volatile bool _open;
  86. public _(IObserver<TSource> observer)
  87. : base(observer)
  88. {
  89. }
  90. private IDisposable _sourceDisposable;
  91. public void Run(Time parent)
  92. {
  93. SetUpstream(parent._scheduler.ScheduleAction(this, parent._duration, state => state.Tick()));
  94. Disposable.SetSingle(ref _sourceDisposable, parent._source.SubscribeSafe(this));
  95. }
  96. protected override void Dispose(bool disposing)
  97. {
  98. if (disposing)
  99. {
  100. Disposable.TryDispose(ref _sourceDisposable);
  101. }
  102. base.Dispose(disposing);
  103. }
  104. private void Tick()
  105. {
  106. _open = true;
  107. }
  108. public override void OnNext(TSource value)
  109. {
  110. if (_open)
  111. {
  112. ForwardOnNext(value);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }