WithLatestFrom.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace UniRx.Operators
  6. {
  7. internal class WithLatestFromObservable<TLeft, TRight, TResult> : OperatorObservableBase<TResult>
  8. {
  9. readonly IObservable<TLeft> left;
  10. readonly IObservable<TRight> right;
  11. readonly Func<TLeft, TRight, TResult> selector;
  12. public WithLatestFromObservable(IObservable<TLeft> left, IObservable<TRight> right, Func<TLeft, TRight, TResult> selector)
  13. : base(left.IsRequiredSubscribeOnCurrentThread() || right.IsRequiredSubscribeOnCurrentThread())
  14. {
  15. this.left = left;
  16. this.right = right;
  17. this.selector = selector;
  18. }
  19. protected override IDisposable SubscribeCore(IObserver<TResult> observer, IDisposable cancel)
  20. {
  21. return new WithLatestFrom(this, observer, cancel).Run();
  22. }
  23. class WithLatestFrom : OperatorObserverBase<TResult, TResult>
  24. {
  25. readonly WithLatestFromObservable<TLeft, TRight, TResult> parent;
  26. readonly object gate = new object();
  27. volatile bool hasLatest;
  28. TRight latestValue = default(TRight);
  29. public WithLatestFrom(WithLatestFromObservable<TLeft, TRight, TResult> parent, IObserver<TResult> observer, IDisposable cancel) : base(observer, cancel)
  30. {
  31. this.parent = parent;
  32. }
  33. public IDisposable Run()
  34. {
  35. var l = parent.left.Subscribe(new LeftObserver(this));
  36. var rSubscription = new SingleAssignmentDisposable();
  37. rSubscription.Disposable = parent.right.Subscribe(new RightObserver(this, rSubscription));
  38. return StableCompositeDisposable.Create(l, rSubscription);
  39. }
  40. public override void OnNext(TResult value)
  41. {
  42. base.observer.OnNext(value);
  43. }
  44. public override void OnError(Exception error)
  45. {
  46. try { observer.OnError(error); }
  47. finally { Dispose(); }
  48. }
  49. public override void OnCompleted()
  50. {
  51. try { observer.OnCompleted(); }
  52. finally { Dispose(); }
  53. }
  54. class LeftObserver : IObserver<TLeft>
  55. {
  56. readonly WithLatestFrom parent;
  57. public LeftObserver(WithLatestFrom parent)
  58. {
  59. this.parent = parent;
  60. }
  61. public void OnNext(TLeft value)
  62. {
  63. if (parent.hasLatest)
  64. {
  65. var result = default(TResult);
  66. try
  67. {
  68. result = parent.parent.selector(value, parent.latestValue);
  69. }
  70. catch (Exception ex)
  71. {
  72. lock (parent.gate)
  73. {
  74. parent.OnError(ex);
  75. }
  76. return;
  77. }
  78. lock (parent.gate)
  79. {
  80. parent.OnNext(result);
  81. }
  82. }
  83. }
  84. public void OnError(Exception error)
  85. {
  86. lock (parent.gate)
  87. {
  88. parent.OnError(error);
  89. }
  90. }
  91. public void OnCompleted()
  92. {
  93. lock (parent.gate)
  94. {
  95. parent.OnCompleted();
  96. }
  97. }
  98. }
  99. class RightObserver : IObserver<TRight>
  100. {
  101. readonly WithLatestFrom parent;
  102. readonly IDisposable selfSubscription;
  103. public RightObserver(WithLatestFrom parent, IDisposable subscription)
  104. {
  105. this.parent = parent;
  106. this.selfSubscription = subscription;
  107. }
  108. public void OnNext(TRight value)
  109. {
  110. parent.latestValue = value;
  111. parent.hasLatest = true;
  112. }
  113. public void OnError(Exception error)
  114. {
  115. lock (parent.gate)
  116. {
  117. parent.OnError(error);
  118. }
  119. }
  120. public void OnCompleted()
  121. {
  122. selfSubscription.Dispose();
  123. }
  124. }
  125. }
  126. }
  127. }