1
0

RepeatWhen.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.Disposables;
  5. using System.Reactive.Subjects;
  6. using System.Threading;
  7. namespace System.Reactive.Linq.ObservableImpl
  8. {
  9. internal sealed class RepeatWhen<T, U> : IObservable<T>
  10. {
  11. private readonly IObservable<T> _source;
  12. private readonly Func<IObservable<object>, IObservable<U>> _handler;
  13. internal RepeatWhen(IObservable<T> source, Func<IObservable<object>, IObservable<U>> handler)
  14. {
  15. _source = source;
  16. _handler = handler;
  17. }
  18. public IDisposable Subscribe(IObserver<T> observer)
  19. {
  20. if (observer == null)
  21. {
  22. throw new ArgumentNullException(nameof(observer));
  23. }
  24. var completeSignals = new Subject<object>();
  25. var redo = default(IObservable<U>);
  26. try
  27. {
  28. redo = _handler(completeSignals);
  29. if (redo == null)
  30. {
  31. throw new NullReferenceException("The handler returned a null IObservable");
  32. }
  33. }
  34. catch (Exception ex)
  35. {
  36. observer.OnError(ex);
  37. return Disposable.Empty;
  38. }
  39. var parent = new MainObserver(observer, _source, new RedoSerializedObserver<object>(completeSignals));
  40. var d = redo.SubscribeSafe(parent.HandlerConsumer);
  41. Disposable.SetSingle(ref parent.HandlerUpstream, d);
  42. parent.HandlerNext();
  43. return parent;
  44. }
  45. private sealed class MainObserver : Sink<T>, IObserver<T>
  46. {
  47. private readonly IObserver<Exception> _errorSignal;
  48. internal readonly HandlerObserver HandlerConsumer;
  49. private readonly IObservable<T> _source;
  50. private IDisposable _upstream;
  51. internal IDisposable HandlerUpstream;
  52. private int _trampoline;
  53. private int _halfSerializer;
  54. private Exception _error;
  55. internal MainObserver(IObserver<T> downstream, IObservable<T> source, IObserver<Exception> errorSignal) : base(downstream)
  56. {
  57. _source = source;
  58. _errorSignal = errorSignal;
  59. HandlerConsumer = new HandlerObserver(this);
  60. }
  61. protected override void Dispose(bool disposing)
  62. {
  63. if (disposing)
  64. {
  65. Disposable.TryDispose(ref _upstream);
  66. Disposable.TryDispose(ref HandlerUpstream);
  67. }
  68. base.Dispose(disposing);
  69. }
  70. public void OnCompleted()
  71. {
  72. if (Disposable.TrySetSerial(ref _upstream, null))
  73. {
  74. _errorSignal.OnNext(null);
  75. }
  76. }
  77. public void OnError(Exception error)
  78. {
  79. HalfSerializer.ForwardOnError(this, error, ref _halfSerializer, ref _error);
  80. }
  81. public void OnNext(T value)
  82. {
  83. HalfSerializer.ForwardOnNext(this, value, ref _halfSerializer, ref _error);
  84. }
  85. private void HandlerError(Exception error)
  86. {
  87. HalfSerializer.ForwardOnError(this, error, ref _halfSerializer, ref _error);
  88. }
  89. private void HandlerComplete()
  90. {
  91. HalfSerializer.ForwardOnCompleted(this, ref _halfSerializer, ref _error);
  92. }
  93. internal void HandlerNext()
  94. {
  95. if (Interlocked.Increment(ref _trampoline) == 1)
  96. {
  97. do
  98. {
  99. var sad = new SingleAssignmentDisposable();
  100. if (Interlocked.CompareExchange(ref _upstream, sad, null) != null)
  101. {
  102. return;
  103. }
  104. sad.Disposable = _source.SubscribeSafe(this);
  105. }
  106. while (Interlocked.Decrement(ref _trampoline) != 0);
  107. }
  108. }
  109. internal sealed class HandlerObserver : IObserver<U>
  110. {
  111. private readonly MainObserver _main;
  112. internal HandlerObserver(MainObserver main)
  113. {
  114. _main = main;
  115. }
  116. public void OnCompleted()
  117. {
  118. _main.HandlerComplete();
  119. }
  120. public void OnError(Exception error)
  121. {
  122. _main.HandlerError(error);
  123. }
  124. public void OnNext(U value)
  125. {
  126. _main.HandlerNext();
  127. }
  128. }
  129. }
  130. }
  131. }