Join.cs 12 KB

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