SkipUntil.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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 sealed class SkipUntil<TSource, TOther> : Producer<TSource, SkipUntil<TSource, TOther>._>
  9. {
  10. private readonly IObservable<TSource> _source;
  11. private readonly IObservable<TOther> _other;
  12. public SkipUntil(IObservable<TSource> source, IObservable<TOther> other)
  13. {
  14. _source = source;
  15. _other = other;
  16. }
  17. protected override _ CreateSink(IObserver<TSource> observer) => new _(observer);
  18. protected override void Run(_ sink) => sink.Run(this);
  19. internal sealed class _ : IdentitySink<TSource>
  20. {
  21. private IDisposable? _otherDisposable;
  22. private bool _forward;
  23. private int _halfSerializer;
  24. private Exception? _error;
  25. public _(IObserver<TSource> observer)
  26. : base(observer)
  27. {
  28. }
  29. public void Run(SkipUntil<TSource, TOther> parent)
  30. {
  31. Disposable.TrySetSingle(ref _otherDisposable, parent._other.Subscribe(new OtherObserver(this)));
  32. Run(parent._source);
  33. }
  34. protected override void Dispose(bool disposing)
  35. {
  36. if (disposing)
  37. {
  38. if (!Disposable.GetIsDisposed(ref _otherDisposable))
  39. {
  40. Disposable.Dispose(ref _otherDisposable);
  41. }
  42. }
  43. base.Dispose(disposing);
  44. }
  45. public override void OnNext(TSource value)
  46. {
  47. if (_forward)
  48. {
  49. HalfSerializer.ForwardOnNext(this, value, ref _halfSerializer, ref _error);
  50. }
  51. }
  52. public override void OnError(Exception ex)
  53. {
  54. HalfSerializer.ForwardOnError(this, ex, ref _halfSerializer, ref _error);
  55. }
  56. public override void OnCompleted()
  57. {
  58. if (_forward)
  59. {
  60. HalfSerializer.ForwardOnCompleted(this, ref _halfSerializer, ref _error);
  61. }
  62. else
  63. {
  64. DisposeUpstream();
  65. }
  66. }
  67. private void OtherComplete()
  68. {
  69. _forward = true;
  70. }
  71. private sealed class OtherObserver : IObserver<TOther>, IDisposable
  72. {
  73. private readonly _ _parent;
  74. public OtherObserver(_ parent)
  75. {
  76. _parent = parent;
  77. }
  78. public void Dispose()
  79. {
  80. if (!Disposable.GetIsDisposed(ref _parent._otherDisposable))
  81. {
  82. Disposable.Dispose(ref _parent._otherDisposable);
  83. }
  84. }
  85. public void OnCompleted()
  86. {
  87. Dispose();
  88. }
  89. public void OnError(Exception error)
  90. {
  91. HalfSerializer.ForwardOnError(_parent, error, ref _parent._halfSerializer, ref _parent._error);
  92. }
  93. public void OnNext(TOther value)
  94. {
  95. _parent.OtherComplete();
  96. Dispose();
  97. }
  98. }
  99. }
  100. }
  101. internal sealed class SkipUntil<TSource> : Producer<TSource, SkipUntil<TSource>._>
  102. {
  103. private readonly IObservable<TSource> _source;
  104. private readonly DateTimeOffset _startTime;
  105. internal readonly IScheduler _scheduler;
  106. public SkipUntil(IObservable<TSource> source, DateTimeOffset startTime, IScheduler scheduler)
  107. {
  108. _source = source;
  109. _startTime = startTime;
  110. _scheduler = scheduler;
  111. }
  112. public IObservable<TSource> Combine(DateTimeOffset startTime)
  113. {
  114. //
  115. // Maximum semantics:
  116. //
  117. // t 0--1--2--3--4--5--6--7-> t 0--1--2--3--4--5--6--7->
  118. //
  119. // xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--|
  120. // xs.SU(5AM) xxxxxxxxxxxxxxxx-o--| xs.SU(3AM) xxxxxxxxxx-o--o--o--|
  121. // xs.SU(5AM).SU(3AM) xxxxxxxxx--------o--| xs.SU(3AM).SU(5AM) xxxxxxxxxxxxxxxx-o--|
  122. //
  123. if (startTime <= _startTime)
  124. {
  125. return this;
  126. }
  127. return new SkipUntil<TSource>(_source, startTime, _scheduler);
  128. }
  129. protected override _ CreateSink(IObserver<TSource> observer) => new _(observer);
  130. protected override void Run(_ sink) => sink.Run(this);
  131. internal sealed class _ : IdentitySink<TSource>
  132. {
  133. private bool _open;
  134. public _(IObserver<TSource> observer)
  135. : base(observer)
  136. {
  137. }
  138. private IDisposable? _task;
  139. public void Run(SkipUntil<TSource> parent)
  140. {
  141. Disposable.SetSingle(ref _task, parent._scheduler.ScheduleAction(this, parent._startTime, static state => state.Tick()));
  142. Run(parent._source);
  143. }
  144. protected override void Dispose(bool disposing)
  145. {
  146. if (disposing)
  147. {
  148. Disposable.Dispose(ref _task);
  149. }
  150. base.Dispose(disposing);
  151. }
  152. private void Tick()
  153. {
  154. _open = true;
  155. }
  156. public override void OnNext(TSource value)
  157. {
  158. if (_open)
  159. {
  160. ForwardOnNext(value);
  161. }
  162. }
  163. }
  164. }
  165. }