Take.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 Take<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 Take(IObservable<TSource> source, int count)
  17. {
  18. _source = source;
  19. _count = count;
  20. }
  21. public Take(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. // Minimum semantics:
  31. //
  32. // xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--|
  33. // xs.Take(5) --o--o--o--o--o| xs.Take(3) --o--o--o|
  34. // xs.Take(5).Take(3) --o--o--o| xs.Take(3).Take(5) --o--o--o|
  35. //
  36. if (_count <= count)
  37. return this;
  38. else
  39. return new Take<TSource>(_source, count);
  40. }
  41. public IObservable<TSource> Omega(TimeSpan duration)
  42. {
  43. //
  44. // Minimum semantics:
  45. //
  46. // t 0--1--2--3--4--5--6--7-> t 0--1--2--3--4--5--6--7->
  47. //
  48. // xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--|
  49. // xs.Take(5s) --o--o--o--o--o| xs.Take(3s) --o--o--o|
  50. // xs.Take(5s).Take(3s) --o--o--o| xs.Take(3s).Take(5s) --o--o--o|
  51. //
  52. if (_duration <= duration)
  53. return this;
  54. else
  55. return new Take<TSource>(_source, duration, _scheduler);
  56. }
  57. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  58. {
  59. if (_scheduler == null)
  60. {
  61. var sink = new _(this, observer, cancel);
  62. setSink(sink);
  63. return _source.SubscribeSafe(sink);
  64. }
  65. else
  66. {
  67. var sink = new TakeImpl(this, observer, cancel);
  68. setSink(sink);
  69. return sink.Run();
  70. }
  71. }
  72. class _ : Sink<TSource>, IObserver<TSource>
  73. {
  74. private readonly Take<TSource> _parent;
  75. private int _remaining;
  76. public _(Take<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  77. : base(observer, cancel)
  78. {
  79. _parent = parent;
  80. _remaining = _parent._count;
  81. }
  82. public void OnNext(TSource value)
  83. {
  84. if (_remaining > 0)
  85. {
  86. --_remaining;
  87. base._observer.OnNext(value);
  88. if (_remaining == 0)
  89. {
  90. base._observer.OnCompleted();
  91. base.Dispose();
  92. }
  93. }
  94. }
  95. public void OnError(Exception error)
  96. {
  97. base._observer.OnError(error);
  98. base.Dispose();
  99. }
  100. public void OnCompleted()
  101. {
  102. base._observer.OnCompleted();
  103. base.Dispose();
  104. }
  105. }
  106. class TakeImpl : Sink<TSource>, IObserver<TSource>
  107. {
  108. private readonly Take<TSource> _parent;
  109. public TakeImpl(Take<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  110. : base(observer, cancel)
  111. {
  112. _parent = parent;
  113. }
  114. private object _gate;
  115. public IDisposable Run()
  116. {
  117. _gate = new object();
  118. var t = _parent._scheduler.Schedule(_parent._duration, Tick);
  119. var d = _parent._source.SubscribeSafe(this);
  120. return StableCompositeDisposable.Create(t, d);
  121. }
  122. private void Tick()
  123. {
  124. lock (_gate)
  125. {
  126. base._observer.OnCompleted();
  127. base.Dispose();
  128. }
  129. }
  130. public void OnNext(TSource value)
  131. {
  132. lock (_gate)
  133. {
  134. base._observer.OnNext(value);
  135. }
  136. }
  137. public void OnError(Exception error)
  138. {
  139. lock (_gate)
  140. {
  141. base._observer.OnError(error);
  142. base.Dispose();
  143. }
  144. }
  145. public void OnCompleted()
  146. {
  147. lock (_gate)
  148. {
  149. base._observer.OnCompleted();
  150. base.Dispose();
  151. }
  152. }
  153. }
  154. }
  155. }
  156. #endif