Skip.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.Reactive.Concurrency;
  7. using System.Reactive.Disposables;
  8. namespace System.Reactive.Linq.ObservableImpl
  9. {
  10. class Skip<TSource> : Producer<TSource>
  11. {
  12. private readonly IObservable<TSource> _source;
  13. private readonly int _count;
  14. private readonly TimeSpan _duration;
  15. internal readonly IScheduler _scheduler;
  16. public Skip(IObservable<TSource> source, int count)
  17. {
  18. _source = source;
  19. _count = count;
  20. }
  21. public Skip(IObservable<TSource> source, TimeSpan duration, IScheduler scheduler)
  22. {
  23. _source = source;
  24. _duration = duration;
  25. _scheduler = scheduler;
  26. }
  27. public IObservable<TSource> Omega(int count)
  28. {
  29. //
  30. // Sum semantics:
  31. //
  32. // xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--|
  33. // xs.Skip(2) --x--x--o--o--o--o--| xs.Skip(3) --x--x--x--o--o--o--|
  34. // xs.Skip(2).Skip(3) --------x--x--x--o--| xs.Skip(3).Skip(2) -----------x--x--o--|
  35. //
  36. return new Skip<TSource>(_source, _count + count);
  37. }
  38. public IObservable<TSource> Omega(TimeSpan duration)
  39. {
  40. //
  41. // Maximum semantics:
  42. //
  43. // t 0--1--2--3--4--5--6--7-> t 0--1--2--3--4--5--6--7->
  44. //
  45. // xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--|
  46. // xs.Skip(2s) xxxxxxx-o--o--o--o--| xs.Skip(3s) xxxxxxxxxx-o--o--o--|
  47. // xs.Skip(2s).Skip(3s) xxxxxxxxxx-o--o--o--| xs.Skip(3s).Skip(2s) xxxxxxx----o--o--o--|
  48. //
  49. if (duration <= _duration)
  50. return this;
  51. else
  52. return new Skip<TSource>(_source, duration, _scheduler);
  53. }
  54. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  55. {
  56. if (_scheduler == null)
  57. {
  58. var sink = new _(this, observer, cancel);
  59. setSink(sink);
  60. return _source.SubscribeSafe(sink);
  61. }
  62. else
  63. {
  64. var sink = new SkipImpl(this, observer, cancel);
  65. setSink(sink);
  66. return sink.Run();
  67. }
  68. }
  69. class _ : Sink<TSource>, IObserver<TSource>
  70. {
  71. private readonly Skip<TSource> _parent;
  72. private int _remaining;
  73. public _(Skip<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  74. : base(observer, cancel)
  75. {
  76. _parent = parent;
  77. _remaining = _parent._count;
  78. }
  79. public void OnNext(TSource value)
  80. {
  81. if (_remaining <= 0)
  82. base._observer.OnNext(value);
  83. else
  84. _remaining--;
  85. }
  86. public void OnError(Exception error)
  87. {
  88. base._observer.OnError(error);
  89. base.Dispose();
  90. }
  91. public void OnCompleted()
  92. {
  93. base._observer.OnCompleted();
  94. base.Dispose();
  95. }
  96. }
  97. class SkipImpl : Sink<TSource>, IObserver<TSource>
  98. {
  99. private readonly Skip<TSource> _parent;
  100. private volatile bool _open;
  101. public SkipImpl(Skip<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  102. : base(observer, cancel)
  103. {
  104. _parent = parent;
  105. }
  106. public IDisposable Run()
  107. {
  108. var t = _parent._scheduler.Schedule(_parent._duration, Tick);
  109. var d = _parent._source.SubscribeSafe(this);
  110. return StableCompositeDisposable.Create(t, d);
  111. }
  112. private void Tick()
  113. {
  114. _open = true;
  115. }
  116. public void OnNext(TSource value)
  117. {
  118. if (_open)
  119. base._observer.OnNext(value);
  120. }
  121. public void OnError(Exception error)
  122. {
  123. base._observer.OnError(error);
  124. base.Dispose();
  125. }
  126. public void OnCompleted()
  127. {
  128. base._observer.OnCompleted();
  129. base.Dispose();
  130. }
  131. }
  132. }
  133. }
  134. #endif