Join.cs 11 KB

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