WithLatestFrom.cs 4.6 KB

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