WithLatestFrom.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. using System.Threading;
  8. namespace System.Reactive.Linq.ObservableImpl
  9. {
  10. class WithLatestFrom<TFirst, TSecond, TResult> : Producer<TResult>
  11. {
  12. private readonly IObservable<TFirst> _first;
  13. private readonly IObservable<TSecond> _second;
  14. private readonly Func<TFirst, TSecond, TResult> _resultSelector;
  15. public WithLatestFrom(IObservable<TFirst> first, IObservable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector)
  16. {
  17. _first = first;
  18. _second = second;
  19. _resultSelector = resultSelector;
  20. }
  21. protected override IDisposable Run(IObserver<TResult> observer, IDisposable cancel, Action<IDisposable> setSink)
  22. {
  23. var sink = new _(this, observer, cancel);
  24. setSink(sink);
  25. return sink.Run();
  26. }
  27. class _ : Sink<TResult>
  28. {
  29. private readonly WithLatestFrom<TFirst, TSecond, TResult> _parent;
  30. public _(WithLatestFrom<TFirst, TSecond, TResult> parent, IObserver<TResult> observer, IDisposable cancel)
  31. : base(observer, cancel)
  32. {
  33. _parent = parent;
  34. }
  35. private object _gate;
  36. private volatile bool _hasLatest;
  37. private TSecond _latest;
  38. private object _latestGate;
  39. public IDisposable Run()
  40. {
  41. _gate = new object();
  42. _latestGate = new object();
  43. var sndSubscription = new SingleAssignmentDisposable();
  44. var fstO = new F(this);
  45. var sndO = new S(this, sndSubscription);
  46. var fstSubscription = _parent._first.SubscribeSafe(fstO);
  47. sndSubscription.Disposable = _parent._second.SubscribeSafe(sndO);
  48. return StableCompositeDisposable.Create(fstSubscription, sndSubscription);
  49. }
  50. class F : IObserver<TFirst>
  51. {
  52. private readonly _ _parent;
  53. public F(_ parent)
  54. {
  55. _parent = parent;
  56. }
  57. public void OnCompleted()
  58. {
  59. lock (_parent._gate)
  60. {
  61. _parent._observer.OnCompleted();
  62. _parent.Dispose();
  63. }
  64. }
  65. public void OnError(Exception error)
  66. {
  67. lock (_parent._gate)
  68. {
  69. _parent._observer.OnError(error);
  70. _parent.Dispose();
  71. }
  72. }
  73. public void OnNext(TFirst value)
  74. {
  75. if (_parent._hasLatest) // Volatile read
  76. {
  77. TSecond latest;
  78. lock (_parent._latestGate)
  79. {
  80. latest = _parent._latest;
  81. }
  82. var res = default(TResult);
  83. try
  84. {
  85. res = _parent._parent._resultSelector(value, latest);
  86. }
  87. catch (Exception ex)
  88. {
  89. lock (_parent._gate)
  90. {
  91. _parent._observer.OnError(ex);
  92. _parent.Dispose();
  93. }
  94. return;
  95. }
  96. lock (_parent._gate)
  97. {
  98. _parent._observer.OnNext(res);
  99. }
  100. }
  101. }
  102. }
  103. class S : IObserver<TSecond>
  104. {
  105. private readonly _ _parent;
  106. private readonly IDisposable _self;
  107. public S(_ parent, IDisposable self)
  108. {
  109. _parent = parent;
  110. _self = self;
  111. }
  112. public void OnCompleted()
  113. {
  114. _self.Dispose();
  115. }
  116. public void OnError(Exception error)
  117. {
  118. lock (_parent._gate)
  119. {
  120. _parent._observer.OnError(error);
  121. _parent.Dispose();
  122. }
  123. }
  124. public void OnNext(TSecond value)
  125. {
  126. lock (_parent._latestGate)
  127. {
  128. _parent._latest = value;
  129. }
  130. if (!_parent._hasLatest)
  131. {
  132. _parent._hasLatest = true;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. #endif