SkipUntil.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. using System.Threading;
  7. namespace System.Reactive.Linq.Observαble
  8. {
  9. class SkipUntil<TSource, TOther> : Producer<TSource>
  10. {
  11. private readonly IObservable<TSource> _source;
  12. private readonly IObservable<TOther> _other;
  13. public SkipUntil(IObservable<TSource> source, IObservable<TOther> other)
  14. {
  15. _source = source;
  16. _other = other;
  17. }
  18. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  19. {
  20. var sink = new _(this, observer, cancel);
  21. setSink(sink);
  22. return sink.Run();
  23. }
  24. class _ : Sink<TSource>
  25. {
  26. private readonly SkipUntil<TSource, TOther> _parent;
  27. public _(SkipUntil<TSource, TOther> parent, IObserver<TSource> observer, IDisposable cancel)
  28. : base(observer, cancel)
  29. {
  30. _parent = parent;
  31. }
  32. public IDisposable Run()
  33. {
  34. var sourceObserver = new T(this);
  35. var otherObserver = new O(this, sourceObserver);
  36. var sourceSubscription = _parent._source.SubscribeSafe(sourceObserver);
  37. var otherSubscription = _parent._other.SubscribeSafe(otherObserver);
  38. sourceObserver.Disposable = sourceSubscription;
  39. otherObserver.Disposable = otherSubscription;
  40. return new CompositeDisposable(
  41. sourceSubscription,
  42. otherSubscription
  43. );
  44. }
  45. class T : IObserver<TSource>
  46. {
  47. private readonly _ _parent;
  48. public volatile IObserver<TSource> _observer;
  49. private readonly SingleAssignmentDisposable _subscription;
  50. public T(_ parent)
  51. {
  52. _parent = parent;
  53. _observer = NopObserver<TSource>.Instance;
  54. _subscription = new SingleAssignmentDisposable();
  55. }
  56. public IDisposable Disposable
  57. {
  58. set { _subscription.Disposable = value; }
  59. }
  60. public void OnNext(TSource value)
  61. {
  62. _observer.OnNext(value);
  63. }
  64. public void OnError(Exception error)
  65. {
  66. _parent._observer.OnError(error);
  67. _parent.Dispose();
  68. }
  69. public void OnCompleted()
  70. {
  71. _observer.OnCompleted();
  72. _subscription.Dispose(); // We can't cancel the other stream yet, it may be on its way to dispatch an OnError message and we don't want to have a race.
  73. }
  74. }
  75. class O : IObserver<TOther>
  76. {
  77. private readonly _ _parent;
  78. private readonly T _sourceObserver;
  79. private readonly SingleAssignmentDisposable _subscription;
  80. public O(_ parent, T sourceObserver)
  81. {
  82. _parent = parent;
  83. _sourceObserver = sourceObserver;
  84. _subscription = new SingleAssignmentDisposable();
  85. }
  86. public IDisposable Disposable
  87. {
  88. set { _subscription.Disposable = value; }
  89. }
  90. public void OnNext(TOther value)
  91. {
  92. _sourceObserver._observer = _parent._observer;
  93. _subscription.Dispose();
  94. }
  95. public void OnError(Exception error)
  96. {
  97. _parent._observer.OnError(error);
  98. _parent.Dispose();
  99. }
  100. public void OnCompleted()
  101. {
  102. _subscription.Dispose();
  103. }
  104. }
  105. }
  106. }
  107. class SkipUntil<TSource> : Producer<TSource>
  108. {
  109. private readonly IObservable<TSource> _source;
  110. private readonly DateTimeOffset _startTime;
  111. internal readonly IScheduler _scheduler;
  112. public SkipUntil(IObservable<TSource> source, DateTimeOffset startTime, IScheduler scheduler)
  113. {
  114. _source = source;
  115. _startTime = startTime;
  116. _scheduler = scheduler;
  117. }
  118. public IObservable<TSource> Ω(DateTimeOffset startTime)
  119. {
  120. //
  121. // Maximum semantics:
  122. //
  123. // t 0--1--2--3--4--5--6--7-> t 0--1--2--3--4--5--6--7->
  124. //
  125. // xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--|
  126. // xs.SU(5AM) xxxxxxxxxxxxxxxx-o--| xs.SU(3AM) xxxxxxxxxx-o--o--o--|
  127. // xs.SU(5AM).SU(3AM) xxxxxxxxx--------o--| xs.SU(3AM).SU(5AM) xxxxxxxxxxxxxxxx-o--|
  128. //
  129. if (startTime <= _startTime)
  130. return this;
  131. else
  132. return new SkipUntil<TSource>(_source, startTime, _scheduler);
  133. }
  134. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  135. {
  136. var sink = new _(this, observer, cancel);
  137. setSink(sink);
  138. return sink.Run();
  139. }
  140. class _ : Sink<TSource>, IObserver<TSource>
  141. {
  142. private readonly SkipUntil<TSource> _parent;
  143. private volatile bool _open;
  144. public _(SkipUntil<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  145. : base(observer, cancel)
  146. {
  147. _parent = parent;
  148. }
  149. public IDisposable Run()
  150. {
  151. var t = _parent._scheduler.Schedule(_parent._startTime, Tick);
  152. var d = _parent._source.SubscribeSafe(this);
  153. return new CompositeDisposable(t, d);
  154. }
  155. private void Tick()
  156. {
  157. _open = true;
  158. }
  159. public void OnNext(TSource value)
  160. {
  161. if (_open)
  162. base._observer.OnNext(value);
  163. }
  164. public void OnError(Exception error)
  165. {
  166. base._observer.OnError(error);
  167. base.Dispose();
  168. }
  169. public void OnCompleted()
  170. {
  171. base._observer.OnCompleted();
  172. base.Dispose();
  173. }
  174. }
  175. }
  176. }
  177. #endif