123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728 |
- using Cysharp.Threading.Tasks.Internal;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading;
- namespace Cysharp.Threading.Tasks.Linq
- {
- public static partial class UniTaskAsyncEnumerable
- {
- public static IUniTaskAsyncEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector)
- {
- Error.ThrowArgumentNullException(outer, nameof(outer));
- Error.ThrowArgumentNullException(inner, nameof(inner));
- Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
- Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
- Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
- return new Join<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default);
- }
- public static IUniTaskAsyncEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, IEqualityComparer<TKey> comparer)
- {
- Error.ThrowArgumentNullException(outer, nameof(outer));
- Error.ThrowArgumentNullException(inner, nameof(inner));
- Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
- Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
- Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
- Error.ThrowArgumentNullException(comparer, nameof(comparer));
- return new Join<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
- }
- public static IUniTaskAsyncEnumerable<TResult> JoinAwait<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, UniTask<TKey>> outerKeySelector, Func<TInner, UniTask<TKey>> innerKeySelector, Func<TOuter, TInner, UniTask<TResult>> resultSelector)
- {
- Error.ThrowArgumentNullException(outer, nameof(outer));
- Error.ThrowArgumentNullException(inner, nameof(inner));
- Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
- Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
- Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
- return new JoinAwait<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default);
- }
- public static IUniTaskAsyncEnumerable<TResult> JoinAwait<TOuter, TInner, TKey, TResult>(this IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, UniTask<TKey>> outerKeySelector, Func<TInner, UniTask<TKey>> innerKeySelector, Func<TOuter, TInner, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
- {
- Error.ThrowArgumentNullException(outer, nameof(outer));
- Error.ThrowArgumentNullException(inner, nameof(inner));
- Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
- Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
- Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
- Error.ThrowArgumentNullException(comparer, nameof(comparer));
- return new JoinAwait<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
- }
- public static IUniTaskAsyncEnumerable<TResult> JoinAwaitWithCancellation<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, TInner, CancellationToken, UniTask<TResult>> resultSelector)
- {
- Error.ThrowArgumentNullException(outer, nameof(outer));
- Error.ThrowArgumentNullException(inner, nameof(inner));
- Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
- Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
- Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
- return new JoinAwaitWithCancellation<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, EqualityComparer<TKey>.Default);
- }
- public static IUniTaskAsyncEnumerable<TResult> JoinAwaitWithCancellation<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, TInner, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
- {
- Error.ThrowArgumentNullException(outer, nameof(outer));
- Error.ThrowArgumentNullException(inner, nameof(inner));
- Error.ThrowArgumentNullException(outerKeySelector, nameof(outerKeySelector));
- Error.ThrowArgumentNullException(innerKeySelector, nameof(innerKeySelector));
- Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
- Error.ThrowArgumentNullException(comparer, nameof(comparer));
- return new JoinAwaitWithCancellation<TOuter, TInner, TKey, TResult>(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer);
- }
- }
- internal sealed class Join<TOuter, TInner, TKey, TResult> : IUniTaskAsyncEnumerable<TResult>
- {
- readonly IUniTaskAsyncEnumerable<TOuter> outer;
- readonly IUniTaskAsyncEnumerable<TInner> inner;
- readonly Func<TOuter, TKey> outerKeySelector;
- readonly Func<TInner, TKey> innerKeySelector;
- readonly Func<TOuter, TInner, TResult> resultSelector;
- readonly IEqualityComparer<TKey> comparer;
- public Join(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, IEqualityComparer<TKey> comparer)
- {
- this.outer = outer;
- this.inner = inner;
- this.outerKeySelector = outerKeySelector;
- this.innerKeySelector = innerKeySelector;
- this.resultSelector = resultSelector;
- this.comparer = comparer;
- }
- public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
- {
- return new _Join(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken);
- }
- sealed class _Join : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
- {
- static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
- readonly IUniTaskAsyncEnumerable<TOuter> outer;
- readonly IUniTaskAsyncEnumerable<TInner> inner;
- readonly Func<TOuter, TKey> outerKeySelector;
- readonly Func<TInner, TKey> innerKeySelector;
- readonly Func<TOuter, TInner, TResult> resultSelector;
- readonly IEqualityComparer<TKey> comparer;
- CancellationToken cancellationToken;
- ILookup<TKey, TInner> lookup;
- IUniTaskAsyncEnumerator<TOuter> enumerator;
- UniTask<bool>.Awaiter awaiter;
- TOuter currentOuterValue;
- IEnumerator<TInner> valueEnumerator;
- bool continueNext;
- public _Join(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, TKey> outerKeySelector, Func<TInner, TKey> innerKeySelector, Func<TOuter, TInner, TResult> resultSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
- {
- this.outer = outer;
- this.inner = inner;
- this.outerKeySelector = outerKeySelector;
- this.innerKeySelector = innerKeySelector;
- this.resultSelector = resultSelector;
- this.comparer = comparer;
- this.cancellationToken = cancellationToken;
- TaskTracker.TrackActiveTask(this, 3);
- }
- public TResult Current { get; private set; }
- public UniTask<bool> MoveNextAsync()
- {
- cancellationToken.ThrowIfCancellationRequested();
- completionSource.Reset();
- if (lookup == null)
- {
- CreateInnerHashSet().Forget();
- }
- else
- {
- SourceMoveNext();
- }
- return new UniTask<bool>(this, completionSource.Version);
- }
- async UniTaskVoid CreateInnerHashSet()
- {
- try
- {
- lookup = await inner.ToLookupAsync(innerKeySelector, comparer, cancellationToken);
- enumerator = outer.GetAsyncEnumerator(cancellationToken);
- }
- catch (Exception ex)
- {
- completionSource.TrySetException(ex);
- return;
- }
- SourceMoveNext();
- }
- void SourceMoveNext()
- {
- try
- {
- LOOP:
- if (valueEnumerator != null)
- {
- if (valueEnumerator.MoveNext())
- {
- Current = resultSelector(currentOuterValue, valueEnumerator.Current);
- goto TRY_SET_RESULT_TRUE;
- }
- else
- {
- valueEnumerator.Dispose();
- valueEnumerator = null;
- }
- }
- awaiter = enumerator.MoveNextAsync().GetAwaiter();
- if (awaiter.IsCompleted)
- {
- continueNext = true;
- MoveNextCore(this);
- if (continueNext)
- {
- continueNext = false;
- goto LOOP; // avoid recursive
- }
- }
- else
- {
- awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
- }
- }
- catch (Exception ex)
- {
- completionSource.TrySetException(ex);
- }
- return;
- TRY_SET_RESULT_TRUE:
- completionSource.TrySetResult(true);
- }
- static void MoveNextCore(object state)
- {
- var self = (_Join)state;
- if (self.TryGetResult(self.awaiter, out var result))
- {
- if (result)
- {
- self.currentOuterValue = self.enumerator.Current;
- var key = self.outerKeySelector(self.currentOuterValue);
- self.valueEnumerator = self.lookup[key].GetEnumerator();
- if (self.continueNext)
- {
- return;
- }
- else
- {
- self.SourceMoveNext();
- }
- }
- else
- {
- self.continueNext = false;
- self.completionSource.TrySetResult(false);
- }
- }
- else
- {
- self.continueNext = false;
- }
- }
- public UniTask DisposeAsync()
- {
- TaskTracker.RemoveTracking(this);
- if (valueEnumerator != null)
- {
- valueEnumerator.Dispose();
- }
- if (enumerator != null)
- {
- return enumerator.DisposeAsync();
- }
- return default;
- }
- }
- }
- internal sealed class JoinAwait<TOuter, TInner, TKey, TResult> : IUniTaskAsyncEnumerable<TResult>
- {
- readonly IUniTaskAsyncEnumerable<TOuter> outer;
- readonly IUniTaskAsyncEnumerable<TInner> inner;
- readonly Func<TOuter, UniTask<TKey>> outerKeySelector;
- readonly Func<TInner, UniTask<TKey>> innerKeySelector;
- readonly Func<TOuter, TInner, UniTask<TResult>> resultSelector;
- readonly IEqualityComparer<TKey> comparer;
- public JoinAwait(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, UniTask<TKey>> outerKeySelector, Func<TInner, UniTask<TKey>> innerKeySelector, Func<TOuter, TInner, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
- {
- this.outer = outer;
- this.inner = inner;
- this.outerKeySelector = outerKeySelector;
- this.innerKeySelector = innerKeySelector;
- this.resultSelector = resultSelector;
- this.comparer = comparer;
- }
- public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
- {
- return new _JoinAwait(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken);
- }
- sealed class _JoinAwait : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
- {
- static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
- static readonly Action<object> OuterSelectCoreDelegate = OuterSelectCore;
- static readonly Action<object> ResultSelectCoreDelegate = ResultSelectCore;
- readonly IUniTaskAsyncEnumerable<TOuter> outer;
- readonly IUniTaskAsyncEnumerable<TInner> inner;
- readonly Func<TOuter, UniTask<TKey>> outerKeySelector;
- readonly Func<TInner, UniTask<TKey>> innerKeySelector;
- readonly Func<TOuter, TInner, UniTask<TResult>> resultSelector;
- readonly IEqualityComparer<TKey> comparer;
- CancellationToken cancellationToken;
- ILookup<TKey, TInner> lookup;
- IUniTaskAsyncEnumerator<TOuter> enumerator;
- UniTask<bool>.Awaiter awaiter;
- TOuter currentOuterValue;
- IEnumerator<TInner> valueEnumerator;
- UniTask<TResult>.Awaiter resultAwaiter;
- UniTask<TKey>.Awaiter outerKeyAwaiter;
- bool continueNext;
- public _JoinAwait(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, UniTask<TKey>> outerKeySelector, Func<TInner, UniTask<TKey>> innerKeySelector, Func<TOuter, TInner, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
- {
- this.outer = outer;
- this.inner = inner;
- this.outerKeySelector = outerKeySelector;
- this.innerKeySelector = innerKeySelector;
- this.resultSelector = resultSelector;
- this.comparer = comparer;
- this.cancellationToken = cancellationToken;
- TaskTracker.TrackActiveTask(this, 3);
- }
- public TResult Current { get; private set; }
- public UniTask<bool> MoveNextAsync()
- {
- cancellationToken.ThrowIfCancellationRequested();
- completionSource.Reset();
- if (lookup == null)
- {
- CreateInnerHashSet().Forget();
- }
- else
- {
- SourceMoveNext();
- }
- return new UniTask<bool>(this, completionSource.Version);
- }
- async UniTaskVoid CreateInnerHashSet()
- {
- try
- {
- lookup = await inner.ToLookupAwaitAsync(innerKeySelector, comparer, cancellationToken);
- enumerator = outer.GetAsyncEnumerator(cancellationToken);
- }
- catch (Exception ex)
- {
- completionSource.TrySetException(ex);
- return;
- }
- SourceMoveNext();
- }
- void SourceMoveNext()
- {
- try
- {
- LOOP:
- if (valueEnumerator != null)
- {
- if (valueEnumerator.MoveNext())
- {
- resultAwaiter = resultSelector(currentOuterValue, valueEnumerator.Current).GetAwaiter();
- if (resultAwaiter.IsCompleted)
- {
- ResultSelectCore(this);
- }
- else
- {
- resultAwaiter.SourceOnCompleted(ResultSelectCoreDelegate, this);
- }
- return;
- }
- else
- {
- valueEnumerator.Dispose();
- valueEnumerator = null;
- }
- }
- awaiter = enumerator.MoveNextAsync().GetAwaiter();
- if (awaiter.IsCompleted)
- {
- continueNext = true;
- MoveNextCore(this);
- if (continueNext)
- {
- continueNext = false;
- goto LOOP; // avoid recursive
- }
- }
- else
- {
- awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
- }
- }
- catch (Exception ex)
- {
- completionSource.TrySetException(ex);
- }
- }
- static void MoveNextCore(object state)
- {
- var self = (_JoinAwait)state;
- if (self.TryGetResult(self.awaiter, out var result))
- {
- if (result)
- {
- self.currentOuterValue = self.enumerator.Current;
- self.outerKeyAwaiter = self.outerKeySelector(self.currentOuterValue).GetAwaiter();
- if (self.outerKeyAwaiter.IsCompleted)
- {
- OuterSelectCore(self);
- }
- else
- {
- self.continueNext = false;
- self.outerKeyAwaiter.SourceOnCompleted(OuterSelectCoreDelegate, self);
- }
- }
- else
- {
- self.continueNext = false;
- self.completionSource.TrySetResult(false);
- }
- }
- else
- {
- self.continueNext = false;
- }
- }
- static void OuterSelectCore(object state)
- {
- var self = (_JoinAwait)state;
- if (self.TryGetResult(self.outerKeyAwaiter, out var key))
- {
- self.valueEnumerator = self.lookup[key].GetEnumerator();
- if (self.continueNext)
- {
- return;
- }
- else
- {
- self.SourceMoveNext();
- }
- }
- else
- {
- self.continueNext = false;
- }
- }
- static void ResultSelectCore(object state)
- {
- var self = (_JoinAwait)state;
- if (self.TryGetResult(self.resultAwaiter, out var result))
- {
- self.Current = result;
- self.completionSource.TrySetResult(true);
- }
- }
- public UniTask DisposeAsync()
- {
- TaskTracker.RemoveTracking(this);
- if (valueEnumerator != null)
- {
- valueEnumerator.Dispose();
- }
- if (enumerator != null)
- {
- return enumerator.DisposeAsync();
- }
- return default;
- }
- }
- }
- internal sealed class JoinAwaitWithCancellation<TOuter, TInner, TKey, TResult> : IUniTaskAsyncEnumerable<TResult>
- {
- readonly IUniTaskAsyncEnumerable<TOuter> outer;
- readonly IUniTaskAsyncEnumerable<TInner> inner;
- readonly Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector;
- readonly Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector;
- readonly Func<TOuter, TInner, CancellationToken, UniTask<TResult>> resultSelector;
- readonly IEqualityComparer<TKey> comparer;
- public JoinAwaitWithCancellation(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector, Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector, Func<TOuter, TInner, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer)
- {
- this.outer = outer;
- this.inner = inner;
- this.outerKeySelector = outerKeySelector;
- this.innerKeySelector = innerKeySelector;
- this.resultSelector = resultSelector;
- this.comparer = comparer;
- }
- public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
- {
- return new _JoinAwaitWithCancellation(outer, inner, outerKeySelector, innerKeySelector, resultSelector, comparer, cancellationToken);
- }
- sealed class _JoinAwaitWithCancellation : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
- {
- static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
- static readonly Action<object> OuterSelectCoreDelegate = OuterSelectCore;
- static readonly Action<object> ResultSelectCoreDelegate = ResultSelectCore;
- readonly IUniTaskAsyncEnumerable<TOuter> outer;
- readonly IUniTaskAsyncEnumerable<TInner> inner;
- readonly Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector;
- readonly Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector;
- readonly Func<TOuter, TInner, CancellationToken, UniTask<TResult>> resultSelector;
- readonly IEqualityComparer<TKey> comparer;
- CancellationToken cancellationToken;
- ILookup<TKey, TInner> lookup;
- IUniTaskAsyncEnumerator<TOuter> enumerator;
- UniTask<bool>.Awaiter awaiter;
- TOuter currentOuterValue;
- IEnumerator<TInner> valueEnumerator;
- UniTask<TResult>.Awaiter resultAwaiter;
- UniTask<TKey>.Awaiter outerKeyAwaiter;
- bool continueNext;
- public _JoinAwaitWithCancellation(IUniTaskAsyncEnumerable<TOuter> outer, IUniTaskAsyncEnumerable<TInner> inner, Func<TOuter, CancellationToken, UniTask<TKey>> outerKeySelector, Func<TInner, CancellationToken, UniTask<TKey>> innerKeySelector, Func<TOuter, TInner, CancellationToken, UniTask<TResult>> resultSelector, IEqualityComparer<TKey> comparer, CancellationToken cancellationToken)
- {
- this.outer = outer;
- this.inner = inner;
- this.outerKeySelector = outerKeySelector;
- this.innerKeySelector = innerKeySelector;
- this.resultSelector = resultSelector;
- this.comparer = comparer;
- this.cancellationToken = cancellationToken;
- TaskTracker.TrackActiveTask(this, 3);
- }
- public TResult Current { get; private set; }
- public UniTask<bool> MoveNextAsync()
- {
- cancellationToken.ThrowIfCancellationRequested();
- completionSource.Reset();
- if (lookup == null)
- {
- CreateInnerHashSet().Forget();
- }
- else
- {
- SourceMoveNext();
- }
- return new UniTask<bool>(this, completionSource.Version);
- }
- async UniTaskVoid CreateInnerHashSet()
- {
- try
- {
- lookup = await inner.ToLookupAwaitWithCancellationAsync(innerKeySelector, comparer, cancellationToken: cancellationToken);
- enumerator = outer.GetAsyncEnumerator(cancellationToken);
- }
- catch (Exception ex)
- {
- completionSource.TrySetException(ex);
- return;
- }
- SourceMoveNext();
- }
- void SourceMoveNext()
- {
- try
- {
- LOOP:
- if (valueEnumerator != null)
- {
- if (valueEnumerator.MoveNext())
- {
- resultAwaiter = resultSelector(currentOuterValue, valueEnumerator.Current, cancellationToken).GetAwaiter();
- if (resultAwaiter.IsCompleted)
- {
- ResultSelectCore(this);
- }
- else
- {
- resultAwaiter.SourceOnCompleted(ResultSelectCoreDelegate, this);
- }
- return;
- }
- else
- {
- valueEnumerator.Dispose();
- valueEnumerator = null;
- }
- }
- awaiter = enumerator.MoveNextAsync().GetAwaiter();
- if (awaiter.IsCompleted)
- {
- continueNext = true;
- MoveNextCore(this);
- if (continueNext)
- {
- continueNext = false;
- goto LOOP; // avoid recursive
- }
- }
- else
- {
- awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
- }
- }
- catch (Exception ex)
- {
- completionSource.TrySetException(ex);
- }
- }
- static void MoveNextCore(object state)
- {
- var self = (_JoinAwaitWithCancellation)state;
- if (self.TryGetResult(self.awaiter, out var result))
- {
- if (result)
- {
- self.currentOuterValue = self.enumerator.Current;
- self.outerKeyAwaiter = self.outerKeySelector(self.currentOuterValue, self.cancellationToken).GetAwaiter();
- if (self.outerKeyAwaiter.IsCompleted)
- {
- OuterSelectCore(self);
- }
- else
- {
- self.continueNext = false;
- self.outerKeyAwaiter.SourceOnCompleted(OuterSelectCoreDelegate, self);
- }
- }
- else
- {
- self.continueNext = false;
- self.completionSource.TrySetResult(false);
- }
- }
- else
- {
- self.continueNext = false;
- }
- }
- static void OuterSelectCore(object state)
- {
- var self = (_JoinAwaitWithCancellation)state;
- if (self.TryGetResult(self.outerKeyAwaiter, out var key))
- {
- self.valueEnumerator = self.lookup[key].GetEnumerator();
- if (self.continueNext)
- {
- return;
- }
- else
- {
- self.SourceMoveNext();
- }
- }
- else
- {
- self.continueNext = false;
- }
- }
- static void ResultSelectCore(object state)
- {
- var self = (_JoinAwaitWithCancellation)state;
- if (self.TryGetResult(self.resultAwaiter, out var result))
- {
- self.Current = result;
- self.completionSource.TrySetResult(true);
- }
- }
- public UniTask DisposeAsync()
- {
- TaskTracker.RemoveTracking(this);
- if (valueEnumerator != null)
- {
- valueEnumerator.Dispose();
- }
- if (enumerator != null)
- {
- return enumerator.DisposeAsync();
- }
- return default;
- }
- }
- }
- }
|