Join.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Reactive.Disposables;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace System.Reactive.Linq
  9. {
  10. public partial class AsyncObservable
  11. {
  12. public static IAsyncObservable<TResult> Join<TLeft, TRight, TLeftDuration, TRightDuration, TResult>(this IAsyncObservable<TLeft> left, IAsyncObservable<TRight> right, Func<TLeft, IAsyncObservable<TLeftDuration>> leftDurationSelector, Func<TRight, IAsyncObservable<TRightDuration>> rightDurationSelector, Func<TLeft, TRight, TResult> resultSelector)
  13. {
  14. if (left == null)
  15. throw new ArgumentNullException(nameof(left));
  16. if (right == null)
  17. throw new ArgumentNullException(nameof(right));
  18. if (leftDurationSelector == null)
  19. throw new ArgumentNullException(nameof(leftDurationSelector));
  20. if (rightDurationSelector == null)
  21. throw new ArgumentNullException(nameof(rightDurationSelector));
  22. if (resultSelector == null)
  23. throw new ArgumentNullException(nameof(resultSelector));
  24. return Create(
  25. left,
  26. (right, leftDurationSelector, rightDurationSelector, resultSelector),
  27. default(TResult),
  28. async (left, state, observer) =>
  29. {
  30. var subscriptions = new CompositeAsyncDisposable();
  31. var (leftObserver, rightObserver, disposable) = AsyncObserver.Join(observer, subscriptions, state.leftDurationSelector, state.rightDurationSelector, state.resultSelector);
  32. var leftSubscription = await left.SubscribeSafeAsync(leftObserver).ConfigureAwait(false);
  33. await subscriptions.AddAsync(leftSubscription).ConfigureAwait(false);
  34. var rightSubscription = await state.right.SubscribeSafeAsync(rightObserver).ConfigureAwait(false);
  35. await subscriptions.AddAsync(rightSubscription).ConfigureAwait(false);
  36. return disposable;
  37. });
  38. }
  39. }
  40. public partial class AsyncObserver
  41. {
  42. public static (IAsyncObserver<TLeft>, IAsyncObserver<TRight>, IAsyncDisposable) Join<TLeft, TRight, TLeftDuration, TRightDuration, TResult>(IAsyncObserver<TResult> observer, IAsyncDisposable subscriptions, Func<TLeft, IAsyncObservable<TLeftDuration>> leftDurationSelector, Func<TRight, IAsyncObservable<TRightDuration>> rightDurationSelector, Func<TLeft, TRight, TResult> resultSelector)
  43. {
  44. if (observer == null)
  45. throw new ArgumentNullException(nameof(observer));
  46. if (subscriptions == null)
  47. throw new ArgumentNullException(nameof(subscriptions));
  48. if (leftDurationSelector == null)
  49. throw new ArgumentNullException(nameof(leftDurationSelector));
  50. if (rightDurationSelector == null)
  51. throw new ArgumentNullException(nameof(rightDurationSelector));
  52. if (resultSelector == null)
  53. throw new ArgumentNullException(nameof(resultSelector));
  54. var gate = new AsyncLock();
  55. var group = new CompositeAsyncDisposable(subscriptions);
  56. var leftMap = new SortedDictionary<int, TLeft>();
  57. var rightMap = new SortedDictionary<int, TRight>();
  58. var leftDone = false;
  59. var rightDone = false;
  60. var leftId = default(int);
  61. var rightId = default(int);
  62. async ValueTask OnErrorAsync(Exception ex)
  63. {
  64. using (await gate.LockAsync().ConfigureAwait(false))
  65. {
  66. await observer.OnErrorAsync(ex).ConfigureAwait(false);
  67. }
  68. }
  69. var leftObserver =
  70. Create<TLeft>(
  71. async x =>
  72. {
  73. var theLeftId = default(int);
  74. var theRightId = default(int);
  75. using (await gate.LockAsync().ConfigureAwait(false))
  76. {
  77. theLeftId = leftId++;
  78. theRightId = rightId;
  79. leftMap.Add(theLeftId, x);
  80. }
  81. var duration = default(IAsyncObservable<TLeftDuration>);
  82. try
  83. {
  84. duration = leftDurationSelector(x);
  85. }
  86. catch (Exception ex)
  87. {
  88. await OnErrorAsync(ex).ConfigureAwait(false);
  89. return;
  90. }
  91. var sad = new SingleAssignmentAsyncDisposable();
  92. await group.AddAsync(sad).ConfigureAwait(false);
  93. var durationObserver =
  94. Create<TLeftDuration>(
  95. d => default,
  96. OnErrorAsync,
  97. async () =>
  98. {
  99. using (await gate.LockAsync().ConfigureAwait(false))
  100. {
  101. if (leftMap.Remove(theLeftId) && leftMap.Count == 0 && leftDone)
  102. {
  103. await observer.OnCompletedAsync().ConfigureAwait(false);
  104. }
  105. }
  106. await group.RemoveAsync(sad).ConfigureAwait(false);
  107. }
  108. );
  109. var durationSubscription = await duration.FirstOrDefault().SubscribeSafeAsync(durationObserver).ConfigureAwait(false);
  110. await sad.AssignAsync(durationSubscription).ConfigureAwait(false);
  111. using (await gate.LockAsync().ConfigureAwait(false))
  112. {
  113. foreach (var rightValue in rightMap)
  114. {
  115. if (rightValue.Key < theRightId)
  116. {
  117. var result = default(TResult);
  118. try
  119. {
  120. result = resultSelector(x, rightValue.Value);
  121. }
  122. catch (Exception ex)
  123. {
  124. await OnErrorAsync(ex).ConfigureAwait(false);
  125. return;
  126. }
  127. await observer.OnNextAsync(result).ConfigureAwait(false);
  128. }
  129. }
  130. }
  131. },
  132. OnErrorAsync,
  133. async () =>
  134. {
  135. using (await gate.LockAsync().ConfigureAwait(false))
  136. {
  137. leftDone = true;
  138. if (rightDone || leftMap.Count == 0)
  139. {
  140. await observer.OnCompletedAsync().ConfigureAwait(false);
  141. }
  142. }
  143. }
  144. );
  145. var rightObserver =
  146. Create<TRight>(
  147. async x =>
  148. {
  149. var theLeftId = 0;
  150. var theRightId = 0;
  151. using (await gate.LockAsync().ConfigureAwait(false))
  152. {
  153. theRightId = rightId++;
  154. theLeftId = leftId;
  155. rightMap.Add(theRightId, x);
  156. }
  157. var duration = default(IAsyncObservable<TRightDuration>);
  158. try
  159. {
  160. duration = rightDurationSelector(x);
  161. }
  162. catch (Exception ex)
  163. {
  164. await OnErrorAsync(ex).ConfigureAwait(false);
  165. return;
  166. }
  167. var sad = new SingleAssignmentAsyncDisposable();
  168. await group.AddAsync(sad).ConfigureAwait(false);
  169. var durationObserver =
  170. Create<TRightDuration>(
  171. d => default,
  172. OnErrorAsync,
  173. async () =>
  174. {
  175. using (await gate.LockAsync().ConfigureAwait(false))
  176. {
  177. if (rightMap.Remove(theRightId) && rightMap.Count == 0 && rightDone)
  178. {
  179. await observer.OnCompletedAsync().ConfigureAwait(false);
  180. }
  181. }
  182. await group.RemoveAsync(sad).ConfigureAwait(false);
  183. }
  184. );
  185. var durationSubscription = await duration.FirstOrDefault().SubscribeSafeAsync(durationObserver).ConfigureAwait(false);
  186. await sad.AssignAsync(durationSubscription).ConfigureAwait(false);
  187. using (await gate.LockAsync().ConfigureAwait(false))
  188. {
  189. foreach (var leftValue in leftMap)
  190. {
  191. if (leftValue.Key < theLeftId)
  192. {
  193. var result = default(TResult);
  194. try
  195. {
  196. result = resultSelector(leftValue.Value, x);
  197. }
  198. catch (Exception ex)
  199. {
  200. await OnErrorAsync(ex).ConfigureAwait(false);
  201. return;
  202. }
  203. await observer.OnNextAsync(result).ConfigureAwait(false);
  204. }
  205. }
  206. }
  207. },
  208. OnErrorAsync,
  209. async () =>
  210. {
  211. using (await gate.LockAsync().ConfigureAwait(false))
  212. {
  213. rightDone = true;
  214. if (leftDone || rightMap.Count == 0)
  215. {
  216. await observer.OnCompletedAsync().ConfigureAwait(false);
  217. }
  218. }
  219. }
  220. );
  221. return (leftObserver, rightObserver, group);
  222. }
  223. }
  224. }