Skip.cs 5.1 KB

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