TakeUntil.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. using System.Reactive.Concurrency;
  5. using System.Reactive.Disposables;
  6. namespace System.Reactive.Linq.ObservableImpl
  7. {
  8. internal sealed class TakeUntil<TSource, TOther> : Producer<TSource, TakeUntil<TSource, TOther>._>
  9. {
  10. private readonly IObservable<TSource> _source;
  11. private readonly IObservable<TOther> _other;
  12. public TakeUntil(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 int _halfSerializer;
  23. private Exception _error;
  24. public _(IObserver<TSource> observer)
  25. : base(observer)
  26. {
  27. }
  28. public void Run(TakeUntil<TSource, TOther> parent)
  29. {
  30. Disposable.SetSingle(ref _otherDisposable, parent._other.Subscribe(new OtherObserver(this)));
  31. Run(parent._source);
  32. }
  33. protected override void Dispose(bool disposing)
  34. {
  35. if (disposing)
  36. {
  37. if (!Disposable.GetIsDisposed(ref _otherDisposable))
  38. {
  39. Disposable.TryDispose(ref _otherDisposable);
  40. }
  41. }
  42. base.Dispose(disposing);
  43. }
  44. public override void OnNext(TSource value)
  45. {
  46. HalfSerializer.ForwardOnNext(this, value, ref _halfSerializer, ref _error);
  47. }
  48. public override void OnError(Exception ex)
  49. {
  50. HalfSerializer.ForwardOnError(this, ex, ref _halfSerializer, ref _error);
  51. }
  52. public override void OnCompleted()
  53. {
  54. HalfSerializer.ForwardOnCompleted(this, ref _halfSerializer, ref _error);
  55. }
  56. private sealed class OtherObserver : IObserver<TOther>
  57. {
  58. private readonly _ _parent;
  59. public OtherObserver(_ parent)
  60. {
  61. _parent = parent;
  62. }
  63. public void OnCompleted()
  64. {
  65. // Completion doesn't mean termination in Rx.NET for this operator
  66. Disposable.TryDispose(ref _parent._otherDisposable);
  67. }
  68. public void OnError(Exception error)
  69. {
  70. HalfSerializer.ForwardOnError(_parent, error, ref _parent._halfSerializer, ref _parent._error);
  71. }
  72. public void OnNext(TOther value)
  73. {
  74. HalfSerializer.ForwardOnCompleted(_parent, ref _parent._halfSerializer, ref _parent._error);
  75. }
  76. }
  77. }
  78. }
  79. internal sealed class TakeUntil<TSource> : Producer<TSource, TakeUntil<TSource>._>
  80. {
  81. private readonly IObservable<TSource> _source;
  82. private readonly DateTimeOffset _endTime;
  83. internal readonly IScheduler _scheduler;
  84. public TakeUntil(IObservable<TSource> source, DateTimeOffset endTime, IScheduler scheduler)
  85. {
  86. _source = source;
  87. _endTime = endTime;
  88. _scheduler = scheduler;
  89. }
  90. public IObservable<TSource> Combine(DateTimeOffset endTime)
  91. {
  92. //
  93. // Minimum semantics:
  94. //
  95. // t 0--1--2--3--4--5--6--7-> t 0--1--2--3--4--5--6--7->
  96. //
  97. // xs --o--o--o--o--o--o--| xs --o--o--o--o--o--o--|
  98. // xs.TU(5AM) --o--o--o--o--o| xs.TU(3AM) --o--o--o|
  99. // xs.TU(5AM).TU(3AM) --o--o--o| xs.TU(3AM).TU(5AM) --o--o--o|
  100. //
  101. if (_endTime <= endTime)
  102. {
  103. return this;
  104. }
  105. return new TakeUntil<TSource>(_source, endTime, _scheduler);
  106. }
  107. protected override _ CreateSink(IObserver<TSource> observer) => new _(observer);
  108. protected override void Run(_ sink) => sink.Run(this);
  109. internal sealed class _ : IdentitySink<TSource>
  110. {
  111. private IDisposable _timerDisposable;
  112. private int _wip;
  113. private Exception _error;
  114. public _(IObserver<TSource> observer)
  115. : base(observer)
  116. {
  117. }
  118. public void Run(TakeUntil<TSource> parent)
  119. {
  120. Disposable.SetSingle(ref _timerDisposable, parent._scheduler.ScheduleAction(this, parent._endTime, state => state.Tick()));
  121. Run(parent._source);
  122. }
  123. protected override void Dispose(bool disposing)
  124. {
  125. if (disposing)
  126. {
  127. Disposable.TryDispose(ref _timerDisposable);
  128. }
  129. base.Dispose(disposing);
  130. }
  131. private void Tick()
  132. {
  133. OnCompleted();
  134. }
  135. public override void OnNext(TSource value)
  136. {
  137. HalfSerializer.ForwardOnNext(this, value, ref _wip, ref _error);
  138. }
  139. public override void OnError(Exception error)
  140. {
  141. HalfSerializer.ForwardOnError(this, error, ref _wip, ref _error);
  142. }
  143. public override void OnCompleted()
  144. {
  145. HalfSerializer.ForwardOnCompleted(this, ref _wip, ref _error);
  146. }
  147. }
  148. }
  149. }