Switch.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #nullable disable
  5. using System.Reactive.Disposables;
  6. namespace System.Reactive.Linq.ObservableImpl
  7. {
  8. internal sealed class Switch<TSource> : Producer<TSource, Switch<TSource>._>
  9. {
  10. private readonly IObservable<IObservable<TSource>> _sources;
  11. public Switch(IObservable<IObservable<TSource>> sources)
  12. {
  13. _sources = sources;
  14. }
  15. protected override _ CreateSink(IObserver<TSource> observer) => new _(observer);
  16. protected override void Run(_ sink) => sink.Run(_sources);
  17. internal sealed class _ : Sink<IObservable<TSource>, TSource>
  18. {
  19. private readonly object _gate = new object();
  20. public _(IObserver<TSource> observer)
  21. : base(observer)
  22. {
  23. }
  24. private IDisposable _innerSerialDisposable;
  25. private bool _isStopped;
  26. private ulong _latest;
  27. private bool _hasLatest;
  28. protected override void Dispose(bool disposing)
  29. {
  30. if (disposing)
  31. {
  32. Disposable.Dispose(ref _innerSerialDisposable);
  33. }
  34. base.Dispose(disposing);
  35. }
  36. public override void OnNext(IObservable<TSource> value)
  37. {
  38. ulong id;
  39. lock (_gate)
  40. {
  41. id = unchecked(++_latest);
  42. _hasLatest = true;
  43. }
  44. var innerObserver = new InnerObserver(this, id);
  45. Disposable.TrySetSerial(ref _innerSerialDisposable, innerObserver);
  46. innerObserver.SetResource(value.SubscribeSafe(innerObserver));
  47. }
  48. public override void OnError(Exception error)
  49. {
  50. lock (_gate)
  51. {
  52. ForwardOnError(error);
  53. }
  54. }
  55. public override void OnCompleted()
  56. {
  57. lock (_gate)
  58. {
  59. DisposeUpstream();
  60. _isStopped = true;
  61. if (!_hasLatest)
  62. {
  63. ForwardOnCompleted();
  64. }
  65. }
  66. }
  67. private sealed class InnerObserver : SafeObserver<TSource>
  68. {
  69. private readonly _ _parent;
  70. private readonly ulong _id;
  71. public InnerObserver(_ parent, ulong id)
  72. {
  73. _parent = parent;
  74. _id = id;
  75. }
  76. public override void OnNext(TSource value)
  77. {
  78. lock (_parent._gate)
  79. {
  80. if (_parent._latest == _id)
  81. {
  82. _parent.ForwardOnNext(value);
  83. }
  84. }
  85. }
  86. public override void OnError(Exception error)
  87. {
  88. lock (_parent._gate)
  89. {
  90. Dispose();
  91. if (_parent._latest == _id)
  92. {
  93. _parent.ForwardOnError(error);
  94. }
  95. }
  96. }
  97. public override void OnCompleted()
  98. {
  99. lock (_parent._gate)
  100. {
  101. Dispose();
  102. if (_parent._latest == _id)
  103. {
  104. _parent._hasLatest = false;
  105. if (_parent._isStopped)
  106. {
  107. _parent.ForwardOnCompleted();
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }