1
0

WithLatestFrom.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 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. lock (_parent._gate)
  71. {
  72. if (_parent._hasLatest)
  73. {
  74. var res = default(TResult);
  75. try
  76. {
  77. res = _parent._parent._resultSelector(value, _parent._latest);
  78. }
  79. catch (Exception ex)
  80. {
  81. _parent._observer.OnError(ex);
  82. _parent.Dispose();
  83. return;
  84. }
  85. _parent._observer.OnNext(res);
  86. }
  87. }
  88. }
  89. }
  90. class S : IObserver<TSecond>
  91. {
  92. private readonly _ _parent;
  93. private readonly IDisposable _self;
  94. public S(_ parent, IDisposable self)
  95. {
  96. _parent = parent;
  97. _self = self;
  98. }
  99. public void OnCompleted()
  100. {
  101. _self.Dispose();
  102. }
  103. public void OnError(Exception error)
  104. {
  105. lock (_parent._gate)
  106. {
  107. _parent._observer.OnError(error);
  108. _parent.Dispose();
  109. }
  110. }
  111. public void OnNext(TSecond value)
  112. {
  113. lock (_parent._gate)
  114. {
  115. _parent._latest = value;
  116. _parent._hasLatest = true;
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. #endif