Switch.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #if !NO_PERF
  5. using System;
  6. using System.Reactive.Disposables;
  7. namespace System.Reactive.Linq.ObservableImpl
  8. {
  9. class Switch<TSource> : Producer<TSource>
  10. {
  11. private readonly IObservable<IObservable<TSource>> _sources;
  12. public Switch(IObservable<IObservable<TSource>> sources)
  13. {
  14. _sources = sources;
  15. }
  16. protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
  17. {
  18. var sink = new _(this, observer, cancel);
  19. setSink(sink);
  20. return sink.Run();
  21. }
  22. class _ : Sink<TSource>, IObserver<IObservable<TSource>>
  23. {
  24. private readonly Switch<TSource> _parent;
  25. public _(Switch<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
  26. : base(observer, cancel)
  27. {
  28. _parent = parent;
  29. }
  30. private object _gate;
  31. private IDisposable _subscription;
  32. private SerialDisposable _innerSubscription;
  33. private bool _isStopped;
  34. private ulong _latest;
  35. private bool _hasLatest;
  36. public IDisposable Run()
  37. {
  38. _gate = new object();
  39. _innerSubscription = new SerialDisposable();
  40. _isStopped = false;
  41. _latest = 0UL;
  42. _hasLatest = false;
  43. var subscription = new SingleAssignmentDisposable();
  44. _subscription = subscription;
  45. subscription.Disposable = _parent._sources.SubscribeSafe(this);
  46. return StableCompositeDisposable.Create(_subscription, _innerSubscription);
  47. }
  48. public void OnNext(IObservable<TSource> value)
  49. {
  50. var id = default(ulong);
  51. lock (_gate)
  52. {
  53. id = unchecked(++_latest);
  54. _hasLatest = true;
  55. }
  56. var d = new SingleAssignmentDisposable();
  57. _innerSubscription.Disposable = d;
  58. d.Disposable = value.SubscribeSafe(new Iter(this, id, d));
  59. }
  60. public void OnError(Exception error)
  61. {
  62. lock (_gate)
  63. base._observer.OnError(error);
  64. base.Dispose();
  65. }
  66. public void OnCompleted()
  67. {
  68. lock (_gate)
  69. {
  70. _subscription.Dispose();
  71. _isStopped = true;
  72. if (!_hasLatest)
  73. {
  74. base._observer.OnCompleted();
  75. base.Dispose();
  76. }
  77. }
  78. }
  79. class Iter : IObserver<TSource>
  80. {
  81. private readonly _ _parent;
  82. private readonly ulong _id;
  83. private readonly IDisposable _self;
  84. public Iter(_ parent, ulong id, IDisposable self)
  85. {
  86. _parent = parent;
  87. _id = id;
  88. _self = self;
  89. }
  90. public void OnNext(TSource value)
  91. {
  92. lock (_parent._gate)
  93. {
  94. if (_parent._latest == _id)
  95. _parent._observer.OnNext(value);
  96. }
  97. }
  98. public void OnError(Exception error)
  99. {
  100. lock (_parent._gate)
  101. {
  102. _self.Dispose();
  103. if (_parent._latest == _id)
  104. {
  105. _parent._observer.OnError(error);
  106. _parent.Dispose();
  107. }
  108. }
  109. }
  110. public void OnCompleted()
  111. {
  112. lock (_parent._gate)
  113. {
  114. _self.Dispose();
  115. if (_parent._latest == _id)
  116. {
  117. _parent._hasLatest = false;
  118. if (_parent._isStopped)
  119. {
  120. _parent._observer.OnCompleted();
  121. _parent.Dispose();
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. #endif