Join.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. using System.Collections.Generic;
  5. using System.Reactive.Disposables;
  6. namespace System.Reactive.Linq.ObservableImpl
  7. {
  8. internal sealed class Join<TLeft, TRight, TLeftDuration, TRightDuration, TResult> : Producer<TResult, Join<TLeft, TRight, TLeftDuration, TRightDuration, TResult>._>
  9. {
  10. private readonly IObservable<TLeft> _left;
  11. private readonly IObservable<TRight> _right;
  12. private readonly Func<TLeft, IObservable<TLeftDuration>> _leftDurationSelector;
  13. private readonly Func<TRight, IObservable<TRightDuration>> _rightDurationSelector;
  14. private readonly Func<TLeft, TRight, TResult> _resultSelector;
  15. public Join(IObservable<TLeft> left, IObservable<TRight> right, Func<TLeft, IObservable<TLeftDuration>> leftDurationSelector, Func<TRight, IObservable<TRightDuration>> rightDurationSelector, Func<TLeft, TRight, TResult> resultSelector)
  16. {
  17. _left = left;
  18. _right = right;
  19. _leftDurationSelector = leftDurationSelector;
  20. _rightDurationSelector = rightDurationSelector;
  21. _resultSelector = resultSelector;
  22. }
  23. protected override _ CreateSink(IObserver<TResult> observer) => new _(this, observer);
  24. protected override void Run(_ sink) => sink.Run(this);
  25. internal sealed class _ : IdentitySink<TResult>
  26. {
  27. private readonly object _gate = new object();
  28. private readonly CompositeDisposable _group = new CompositeDisposable();
  29. private readonly SortedDictionary<int, TLeft> _leftMap = new SortedDictionary<int, TLeft>();
  30. private readonly SortedDictionary<int, TRight> _rightMap = new SortedDictionary<int, TRight>();
  31. private readonly Func<TLeft, IObservable<TLeftDuration>> _leftDurationSelector;
  32. private readonly Func<TRight, IObservable<TRightDuration>> _rightDurationSelector;
  33. private readonly Func<TLeft, TRight, TResult> _resultSelector;
  34. public _(Join<TLeft, TRight, TLeftDuration, TRightDuration, TResult> parent, IObserver<TResult> observer)
  35. : base(observer)
  36. {
  37. _leftDurationSelector = parent._leftDurationSelector;
  38. _rightDurationSelector = parent._rightDurationSelector;
  39. _resultSelector = parent._resultSelector;
  40. }
  41. private bool _leftDone;
  42. private int _leftID;
  43. private bool _rightDone;
  44. private int _rightID;
  45. public void Run(Join<TLeft, TRight, TLeftDuration, TRightDuration, TResult> parent)
  46. {
  47. var leftObserver = new LeftObserver(this);
  48. var rightObserver = new RightObserver(this);
  49. _group.Add(leftObserver);
  50. _group.Add(rightObserver);
  51. leftObserver.SetResource(parent._left.SubscribeSafe(leftObserver));
  52. rightObserver.SetResource(parent._right.SubscribeSafe(rightObserver));
  53. SetUpstream(_group);
  54. }
  55. private sealed class LeftObserver : SafeObserver<TLeft>
  56. {
  57. private readonly _ _parent;
  58. public LeftObserver(_ parent)
  59. {
  60. _parent = parent;
  61. }
  62. private void Expire(int id, IDisposable resource)
  63. {
  64. lock (_parent._gate)
  65. {
  66. if (_parent._leftMap.Remove(id) && _parent._leftMap.Count == 0 && _parent._leftDone)
  67. {
  68. _parent.ForwardOnCompleted();
  69. }
  70. }
  71. _parent._group.Remove(resource);
  72. }
  73. public override void OnNext(TLeft value)
  74. {
  75. var id = 0;
  76. var rightID = 0;
  77. lock (_parent._gate)
  78. {
  79. id = _parent._leftID++;
  80. rightID = _parent._rightID;
  81. _parent._leftMap.Add(id, value);
  82. }
  83. var duration = default(IObservable<TLeftDuration>);
  84. try
  85. {
  86. duration = _parent._leftDurationSelector(value);
  87. }
  88. catch (Exception exception)
  89. {
  90. _parent.ForwardOnError(exception);
  91. return;
  92. }
  93. var durationObserver = new DurationObserver(this, id);
  94. _parent._group.Add(durationObserver);
  95. durationObserver.SetResource(duration.SubscribeSafe(durationObserver));
  96. lock (_parent._gate)
  97. {
  98. foreach (var rightValue in _parent._rightMap)
  99. {
  100. if (rightValue.Key < rightID)
  101. {
  102. TResult result;
  103. try
  104. {
  105. result = _parent._resultSelector(value, rightValue.Value);
  106. }
  107. catch (Exception exception)
  108. {
  109. _parent.ForwardOnError(exception);
  110. return;
  111. }
  112. _parent.ForwardOnNext(result);
  113. }
  114. }
  115. }
  116. }
  117. private sealed class DurationObserver : SafeObserver<TLeftDuration>
  118. {
  119. private readonly LeftObserver _parent;
  120. private readonly int _id;
  121. public DurationObserver(LeftObserver parent, int id)
  122. {
  123. _parent = parent;
  124. _id = id;
  125. }
  126. public override void OnNext(TLeftDuration value)
  127. {
  128. _parent.Expire(_id, this);
  129. }
  130. public override void OnError(Exception error)
  131. {
  132. _parent.OnError(error);
  133. }
  134. public override void OnCompleted()
  135. {
  136. _parent.Expire(_id, this);
  137. }
  138. }
  139. public override void OnError(Exception error)
  140. {
  141. lock (_parent._gate)
  142. {
  143. _parent.ForwardOnError(error);
  144. }
  145. }
  146. public override void OnCompleted()
  147. {
  148. lock (_parent._gate)
  149. {
  150. _parent._leftDone = true;
  151. if (_parent._rightDone || _parent._leftMap.Count == 0)
  152. {
  153. _parent.ForwardOnCompleted();
  154. }
  155. else
  156. {
  157. Dispose();
  158. }
  159. }
  160. }
  161. }
  162. private sealed class RightObserver : SafeObserver<TRight>
  163. {
  164. private readonly _ _parent;
  165. public RightObserver(_ parent)
  166. {
  167. _parent = parent;
  168. }
  169. private void Expire(int id, IDisposable resource)
  170. {
  171. lock (_parent._gate)
  172. {
  173. if (_parent._rightMap.Remove(id) && _parent._rightMap.Count == 0 && _parent._rightDone)
  174. {
  175. _parent.ForwardOnCompleted();
  176. }
  177. }
  178. _parent._group.Remove(resource);
  179. }
  180. public override void OnNext(TRight value)
  181. {
  182. var id = 0;
  183. var leftID = 0;
  184. lock (_parent._gate)
  185. {
  186. id = _parent._rightID++;
  187. leftID = _parent._leftID;
  188. _parent._rightMap.Add(id, value);
  189. }
  190. var duration = default(IObservable<TRightDuration>);
  191. try
  192. {
  193. duration = _parent._rightDurationSelector(value);
  194. }
  195. catch (Exception exception)
  196. {
  197. _parent.ForwardOnError(exception);
  198. return;
  199. }
  200. var durationObserver = new DurationObserver(this, id);
  201. _parent._group.Add(durationObserver);
  202. durationObserver.SetResource(duration.SubscribeSafe(durationObserver));
  203. lock (_parent._gate)
  204. {
  205. foreach (var leftValue in _parent._leftMap)
  206. {
  207. if (leftValue.Key < leftID)
  208. {
  209. TResult result;
  210. try
  211. {
  212. result = _parent._resultSelector(leftValue.Value, value);
  213. }
  214. catch (Exception exception)
  215. {
  216. _parent.ForwardOnError(exception);
  217. return;
  218. }
  219. _parent.ForwardOnNext(result);
  220. }
  221. }
  222. }
  223. }
  224. private sealed class DurationObserver : SafeObserver<TRightDuration>
  225. {
  226. private readonly RightObserver _parent;
  227. private readonly int _id;
  228. public DurationObserver(RightObserver parent, int id)
  229. {
  230. _parent = parent;
  231. _id = id;
  232. }
  233. public override void OnNext(TRightDuration value)
  234. {
  235. _parent.Expire(_id, this);
  236. }
  237. public override void OnError(Exception error)
  238. {
  239. _parent.OnError(error);
  240. }
  241. public override void OnCompleted()
  242. {
  243. _parent.Expire(_id, this);
  244. }
  245. }
  246. public override void OnError(Exception error)
  247. {
  248. lock (_parent._gate)
  249. {
  250. _parent.ForwardOnError(error);
  251. }
  252. }
  253. public override void OnCompleted()
  254. {
  255. lock (_parent._gate)
  256. {
  257. _parent._rightDone = true;
  258. if (_parent._leftDone || _parent._rightMap.Count == 0)
  259. {
  260. _parent.ForwardOnCompleted();
  261. }
  262. else
  263. {
  264. Dispose();
  265. }
  266. }
  267. }
  268. }
  269. }
  270. }
  271. }