GroupJoin.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. using Cysharp.Threading.Tasks.Internal;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading;
  6. namespace Cysharp.Threading.Tasks.Linq
  7. {
  8. public static partial class UniTaskAsyncEnumerable
  9. {
  10. public static IUniTaskAsyncEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector)
  11. {
  12. Error.ThrowArgumentNullException(outer, nameof(outer));
  13. Error.ThrowArgumentNullException(inner, nameof(inner));
  14. Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
  15. Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
  16. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  17. return new GroupJoin<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default);
  18. }
  19. public static IUniTaskAsyncEnumerable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector, IEqualityComparer<TKey> comparer)
  20. {
  21. Error.ThrowArgumentNullException(outer, nameof(outer));
  22. Error.ThrowArgumentNullException(inner, nameof(inner));
  23. Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
  24. Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
  25. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  26. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  27. return new GroupJoin<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
  28. }
  29. public static IUniTaskAsyncEnumerable<TResult> GroupJoinAwait<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, UniTask<TKey>> outerKeySelector, Func<TInner, UniTask<TKey>> innerKeySelector, Func<TOuter, IEnumerable<TInner>, UniTask<TResult>> resultSelector)
  30. {
  31. Error.ThrowArgumentNullException(outer, nameof(outer));
  32. Error.ThrowArgumentNullException(inner, nameof(inner));
  33. Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
  34. Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
  35. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  36. return new GroupJoinAwait<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default);
  37. }
  38. public static IUniTaskAsyncEnumerable<TResult> GroupJoinAwait<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, UniTask<TKey>> outerKeySelector, Func<TInner, UniTask<TKey>> innerKeySelector, Func<TOuter, IEnumerable<TInner>, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
  39. {
  40. Error.ThrowArgumentNullException(outer, nameof(outer));
  41. Error.ThrowArgumentNullException(inner, nameof(inner));
  42. Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
  43. Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
  44. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  45. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  46. return new GroupJoinAwait<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
  47. }
  48. public static IUniTaskAsyncEnumerable<TResult> GroupJoinAwaitWithCancellation<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector, Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector, Func<TOuter, IEnumerable<TInner>, CancellationToken, UniTask<TResult>> resultSelector)
  49. {
  50. Error.ThrowArgumentNullException(outer, nameof(outer));
  51. Error.ThrowArgumentNullException(inner, nameof(inner));
  52. Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
  53. Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
  54. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  55. return new GroupJoinAwaitWithCancellation<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default);
  56. }
  57. public static IUniTaskAsyncEnumerable<TResult> GroupJoinAwaitWithCancellation<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector, Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector, Func<TOuter, IEnumerable<TInner>, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
  58. {
  59. Error.ThrowArgumentNullException(outer, nameof(outer));
  60. Error.ThrowArgumentNullException(inner, nameof(inner));
  61. Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
  62. Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
  63. Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
  64. Error.ThrowArgumentNullException(comparer, nameof(comparer));
  65. return new GroupJoinAwaitWithCancellation<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
  66. }
  67. }
  68. internal sealed class GroupJoin<TOuter, TInner, TKey, TResult> : IUniTaskAsyncEnumerable<TResult>
  69. {
  70. readonly IUniTaskAsyncEnumerable<TOuter> outer;
  71. readonly IUniTaskAsyncEnumerable<TInner> inner;
  72. readonly Func<TOuter, TKey> outerKeySelector;
  73. readonly Func<TInner, TKey> innerKeySelector;
  74. readonly Func<TOuter, IEnumerable<TInner>, TResult> resultSelector;
  75. readonly IEqualityComparer<TKey> comparer;
  76. public GroupJoin(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector, IEqualityComparer<TKey> comparer)
  77. {
  78. this.outer = outer;
  79. this.inner = inner;
  80. this.outerKeySelector = outerKeySelector;
  81. this.innerKeySelector = innerKeySelector;
  82. this.resultSelector = resultSelector;
  83. this.comparer = comparer;
  84. }
  85. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  86. {
  87. return new _GroupJoin(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken);
  88. }
  89. sealed class _GroupJoin : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  90. {
  91. static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
  92. readonly IUniTaskAsyncEnumerable<TOuter> outer;
  93. readonly IUniTaskAsyncEnumerable<TInner> inner;
  94. readonly Func<TOuter, TKey> outerKeySelector;
  95. readonly Func<TInner, TKey> innerKeySelector;
  96. readonly Func<TOuter, IEnumerable<TInner>, TResult> resultSelector;
  97. readonly IEqualityComparer<TKey> comparer;
  98. CancellationToken cancellationToken;
  99. ILookup<TKey, TInner> lookup;
  100. IUniTaskAsyncEnumerator<TOuter> enumerator;
  101. UniTask<bool>.Awaiter awaiter;
  102. public _GroupJoin(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, IEnumerable<TInner>, TResult> resultSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  103. {
  104. this.outer = outer;
  105. this.inner = inner;
  106. this.outerKeySelector = outerKeySelector;
  107. this.innerKeySelector = innerKeySelector;
  108. this.resultSelector = resultSelector;
  109. this.comparer = comparer;
  110. this.cancellationToken = cancellationToken;
  111. TaskTracker.TrackActiveTask(this, 3);
  112. }
  113. public TResult Current { get; private set; }
  114. public UniTask<bool> MoveNextAsync()
  115. {
  116. cancellationToken.ThrowIfCancellationRequested();
  117. completionSource.Reset();
  118. if (lookup == null)
  119. {
  120. CreateLookup().Forget();
  121. }
  122. else
  123. {
  124. SourceMoveNext();
  125. }
  126. return new UniTask<bool>(this, completionSource.Version);
  127. }
  128. async UniTaskVoid CreateLookup()
  129. {
  130. try
  131. {
  132. lookup = await inner.ToLookupAsync(innerKeySelector, comparer, cancellationToken);
  133. enumerator = outer.GetAsyncEnumerator(cancellationToken);
  134. }
  135. catch (Exception ex)
  136. {
  137. completionSource.TrySetException(ex);
  138. return;
  139. }
  140. SourceMoveNext();
  141. }
  142. void SourceMoveNext()
  143. {
  144. try
  145. {
  146. awaiter = enumerator.MoveNextAsync().GetAwaiter();
  147. if (awaiter.IsCompleted)
  148. {
  149. MoveNextCore(this);
  150. }
  151. else
  152. {
  153. awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
  154. }
  155. }
  156. catch (Exception ex)
  157. {
  158. completionSource.TrySetException(ex);
  159. }
  160. }
  161. static void MoveNextCore(object state)
  162. {
  163. var self = (_GroupJoin)state;
  164. if (self.TryGetResult(self.awaiter, out var result))
  165. {
  166. if (result)
  167. {
  168. var outer = self.enumerator.Current;
  169. var key = self.outerKeySelector(outer);
  170. var values = self.lookup[key];
  171. self.Current = self.resultSelector(outer, values);
  172. self.completionSource.TrySetResult(true);
  173. }
  174. else
  175. {
  176. self.completionSource.TrySetResult(false);
  177. }
  178. }
  179. }
  180. public UniTask DisposeAsync()
  181. {
  182. TaskTracker.RemoveTracking(this);
  183. if (enumerator != null)
  184. {
  185. return enumerator.DisposeAsync();
  186. }
  187. return default;
  188. }
  189. }
  190. }
  191. internal sealed class GroupJoinAwait<TOuter, TInner, TKey, TResult> : IUniTaskAsyncEnumerable<TResult>
  192. {
  193. readonly IUniTaskAsyncEnumerable<TOuter> outer;
  194. readonly IUniTaskAsyncEnumerable<TInner> inner;
  195. readonly Func<TOuter, UniTask<TKey>> outerKeySelector;
  196. readonly Func<TInner, UniTask<TKey>> innerKeySelector;
  197. readonly Func<TOuter, IEnumerable<TInner>, UniTask<TResult>> resultSelector;
  198. readonly IEqualityComparer<TKey> comparer;
  199. public GroupJoinAwait(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, UniTask<TKey>> outerKeySelector, Func<TInner, UniTask<TKey>> innerKeySelector, Func<TOuter, IEnumerable<TInner>, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
  200. {
  201. this.outer = outer;
  202. this.inner = inner;
  203. this.outerKeySelector = outerKeySelector;
  204. this.innerKeySelector = innerKeySelector;
  205. this.resultSelector = resultSelector;
  206. this.comparer = comparer;
  207. }
  208. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  209. {
  210. return new _GroupJoinAwait(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken);
  211. }
  212. sealed class _GroupJoinAwait : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  213. {
  214. static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
  215. readonly static Action<object> ResultSelectCoreDelegate = ResultSelectCore;
  216. readonly static Action<object> OuterKeySelectCoreDelegate = OuterKeySelectCore;
  217. readonly IUniTaskAsyncEnumerable<TOuter> outer;
  218. readonly IUniTaskAsyncEnumerable<TInner> inner;
  219. readonly Func<TOuter, UniTask<TKey>> outerKeySelector;
  220. readonly Func<TInner, UniTask<TKey>> innerKeySelector;
  221. readonly Func<TOuter, IEnumerable<TInner>, UniTask<TResult>> resultSelector;
  222. readonly IEqualityComparer<TKey> comparer;
  223. CancellationToken cancellationToken;
  224. ILookup<TKey, TInner> lookup;
  225. IUniTaskAsyncEnumerator<TOuter> enumerator;
  226. TOuter outerValue;
  227. UniTask<bool>.Awaiter awaiter;
  228. UniTask<TKey>.Awaiter outerKeyAwaiter;
  229. UniTask<TResult>.Awaiter resultAwaiter;
  230. public _GroupJoinAwait(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, UniTask<TKey>> outerKeySelector, Func<TInner, UniTask<TKey>> innerKeySelector, Func<TOuter, IEnumerable<TInner>, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  231. {
  232. this.outer = outer;
  233. this.inner = inner;
  234. this.outerKeySelector = outerKeySelector;
  235. this.innerKeySelector = innerKeySelector;
  236. this.resultSelector = resultSelector;
  237. this.comparer = comparer;
  238. this.cancellationToken = cancellationToken;
  239. TaskTracker.TrackActiveTask(this, 3);
  240. }
  241. public TResult Current { get; private set; }
  242. public UniTask<bool> MoveNextAsync()
  243. {
  244. cancellationToken.ThrowIfCancellationRequested();
  245. completionSource.Reset();
  246. if (lookup == null)
  247. {
  248. CreateLookup().Forget();
  249. }
  250. else
  251. {
  252. SourceMoveNext();
  253. }
  254. return new UniTask<bool>(this, completionSource.Version);
  255. }
  256. async UniTaskVoid CreateLookup()
  257. {
  258. try
  259. {
  260. lookup = await inner.ToLookupAwaitAsync(innerKeySelector, comparer, cancellationToken);
  261. enumerator = outer.GetAsyncEnumerator(cancellationToken);
  262. }
  263. catch (Exception ex)
  264. {
  265. completionSource.TrySetException(ex);
  266. return;
  267. }
  268. SourceMoveNext();
  269. }
  270. void SourceMoveNext()
  271. {
  272. try
  273. {
  274. awaiter = enumerator.MoveNextAsync().GetAwaiter();
  275. if (awaiter.IsCompleted)
  276. {
  277. MoveNextCore(this);
  278. }
  279. else
  280. {
  281. awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
  282. }
  283. }
  284. catch (Exception ex)
  285. {
  286. completionSource.TrySetException(ex);
  287. }
  288. }
  289. static void MoveNextCore(object state)
  290. {
  291. var self = (_GroupJoinAwait)state;
  292. if (self.TryGetResult(self.awaiter, out var result))
  293. {
  294. if (result)
  295. {
  296. try
  297. {
  298. self.outerValue = self.enumerator.Current;
  299. self.outerKeyAwaiter = self.outerKeySelector(self.outerValue).GetAwaiter();
  300. if (self.outerKeyAwaiter.IsCompleted)
  301. {
  302. OuterKeySelectCore(self);
  303. }
  304. else
  305. {
  306. self.outerKeyAwaiter.SourceOnCompleted(OuterKeySelectCoreDelegate, self);
  307. }
  308. }
  309. catch (Exception ex)
  310. {
  311. self.completionSource.TrySetException(ex);
  312. }
  313. }
  314. else
  315. {
  316. self.completionSource.TrySetResult(false);
  317. }
  318. }
  319. }
  320. static void OuterKeySelectCore(object state)
  321. {
  322. var self = (_GroupJoinAwait)state;
  323. if (self.TryGetResult(self.outerKeyAwaiter, out var result))
  324. {
  325. try
  326. {
  327. var values = self.lookup[result];
  328. self.resultAwaiter = self.resultSelector(self.outerValue, values).GetAwaiter();
  329. if (self.resultAwaiter.IsCompleted)
  330. {
  331. ResultSelectCore(self);
  332. }
  333. else
  334. {
  335. self.resultAwaiter.SourceOnCompleted(ResultSelectCoreDelegate, self);
  336. }
  337. }
  338. catch (Exception ex)
  339. {
  340. self.completionSource.TrySetException(ex);
  341. }
  342. }
  343. }
  344. static void ResultSelectCore(object state)
  345. {
  346. var self = (_GroupJoinAwait)state;
  347. if (self.TryGetResult(self.resultAwaiter, out var result))
  348. {
  349. self.Current = result;
  350. self.completionSource.TrySetResult(true);
  351. }
  352. }
  353. public UniTask DisposeAsync()
  354. {
  355. TaskTracker.RemoveTracking(this);
  356. if (enumerator != null)
  357. {
  358. return enumerator.DisposeAsync();
  359. }
  360. return default;
  361. }
  362. }
  363. }
  364. internal sealed class GroupJoinAwaitWithCancellation<TOuter, TInner, TKey, TResult> : IUniTaskAsyncEnumerable<TResult>
  365. {
  366. readonly IUniTaskAsyncEnumerable<TOuter> outer;
  367. readonly IUniTaskAsyncEnumerable<TInner> inner;
  368. readonly Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector;
  369. readonly Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector;
  370. readonly Func<TOuter, IEnumerable<TInner>, CancellationToken, UniTask<TResult>> resultSelector;
  371. readonly IEqualityComparer<TKey> comparer;
  372. public GroupJoinAwaitWithCancellation(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector, Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector, Func<TOuter, IEnumerable<TInner>, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
  373. {
  374. this.outer = outer;
  375. this.inner = inner;
  376. this.outerKeySelector = outerKeySelector;
  377. this.innerKeySelector = innerKeySelector;
  378. this.resultSelector = resultSelector;
  379. this.comparer = comparer;
  380. }
  381. public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
  382. {
  383. return new _GroupJoinAwaitWithCancellation(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken);
  384. }
  385. sealed class _GroupJoinAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
  386. {
  387. static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
  388. readonly static Action<object> ResultSelectCoreDelegate = ResultSelectCore;
  389. readonly static Action<object> OuterKeySelectCoreDelegate = OuterKeySelectCore;
  390. readonly IUniTaskAsyncEnumerable<TOuter> outer;
  391. readonly IUniTaskAsyncEnumerable<TInner> inner;
  392. readonly Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector;
  393. readonly Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector;
  394. readonly Func<TOuter, IEnumerable<TInner>, CancellationToken, UniTask<TResult>> resultSelector;
  395. readonly IEqualityComparer<TKey> comparer;
  396. CancellationToken cancellationToken;
  397. ILookup<TKey, TInner> lookup;
  398. IUniTaskAsyncEnumerator<TOuter> enumerator;
  399. TOuter outerValue;
  400. UniTask<bool>.Awaiter awaiter;
  401. UniTask<TKey>.Awaiter outerKeyAwaiter;
  402. UniTask<TResult>.Awaiter resultAwaiter;
  403. public _GroupJoinAwaitWithCancellation(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector, Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector, Func<TOuter, IEnumerable<TInner>, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
  404. {
  405. this.outer = outer;
  406. this.inner = inner;
  407. this.outerKeySelector = outerKeySelector;
  408. this.innerKeySelector = innerKeySelector;
  409. this.resultSelector = resultSelector;
  410. this.comparer = comparer;
  411. this.cancellationToken = cancellationToken;
  412. TaskTracker.TrackActiveTask(this, 3);
  413. }
  414. public TResult Current { get; private set; }
  415. public UniTask<bool> MoveNextAsync()
  416. {
  417. cancellationToken.ThrowIfCancellationRequested();
  418. completionSource.Reset();
  419. if (lookup == null)
  420. {
  421. CreateLookup().Forget();
  422. }
  423. else
  424. {
  425. SourceMoveNext();
  426. }
  427. return new UniTask<bool>(this, completionSource.Version);
  428. }
  429. async UniTaskVoid CreateLookup()
  430. {
  431. try
  432. {
  433. lookup = await inner.ToLookupAwaitWithCancellationAsync(innerKeySelector, comparer, cancellationToken);
  434. enumerator = outer.GetAsyncEnumerator(cancellationToken);
  435. }
  436. catch (Exception ex)
  437. {
  438. completionSource.TrySetException(ex);
  439. return;
  440. }
  441. SourceMoveNext();
  442. }
  443. void SourceMoveNext()
  444. {
  445. try
  446. {
  447. awaiter = enumerator.MoveNextAsync().GetAwaiter();
  448. if (awaiter.IsCompleted)
  449. {
  450. MoveNextCore(this);
  451. }
  452. else
  453. {
  454. awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
  455. }
  456. }
  457. catch (Exception ex)
  458. {
  459. completionSource.TrySetException(ex);
  460. }
  461. }
  462. static void MoveNextCore(object state)
  463. {
  464. var self = (_GroupJoinAwaitWithCancellation)state;
  465. if (self.TryGetResult(self.awaiter, out var result))
  466. {
  467. if (result)
  468. {
  469. try
  470. {
  471. self.outerValue = self.enumerator.Current;
  472. self.outerKeyAwaiter = self.outerKeySelector(self.outerValue, self.cancellationToken).GetAwaiter();
  473. if (self.outerKeyAwaiter.IsCompleted)
  474. {
  475. OuterKeySelectCore(self);
  476. }
  477. else
  478. {
  479. self.outerKeyAwaiter.SourceOnCompleted(OuterKeySelectCoreDelegate, self);
  480. }
  481. }
  482. catch (Exception ex)
  483. {
  484. self.completionSource.TrySetException(ex);
  485. }
  486. }
  487. else
  488. {
  489. self.completionSource.TrySetResult(false);
  490. }
  491. }
  492. }
  493. static void OuterKeySelectCore(object state)
  494. {
  495. var self = (_GroupJoinAwaitWithCancellation)state;
  496. if (self.TryGetResult(self.outerKeyAwaiter, out var result))
  497. {
  498. try
  499. {
  500. var values = self.lookup[result];
  501. self.resultAwaiter = self.resultSelector(self.outerValue, values, self.cancellationToken).GetAwaiter();
  502. if (self.resultAwaiter.IsCompleted)
  503. {
  504. ResultSelectCore(self);
  505. }
  506. else
  507. {
  508. self.resultAwaiter.SourceOnCompleted(ResultSelectCoreDelegate, self);
  509. }
  510. }
  511. catch (Exception ex)
  512. {
  513. self.completionSource.TrySetException(ex);
  514. }
  515. }
  516. }
  517. static void ResultSelectCore(object state)
  518. {
  519. var self = (_GroupJoinAwaitWithCancellation)state;
  520. if (self.TryGetResult(self.resultAwaiter, out var result))
  521. {
  522. self.Current = result;
  523. self.completionSource.TrySetResult(true);
  524. }
  525. }
  526. public UniTask DisposeAsync()
  527. {
  528. TaskTracker.RemoveTracking(this);
  529. if (enumerator != null)
  530. {
  531. return enumerator.DisposeAsync();
  532. }
  533. return default;
  534. }
  535. }
  536. }
  537. }