Skip.cs 4.8 KB

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