Switch.cs 4.6 KB

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