WithLatestFrom.cs 4.5 KB

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