master
neuecc 2020-05-22 02:33:55 +09:00
parent 2b87cadba3
commit 4504d84aa8
2 changed files with 1083 additions and 453 deletions

View File

@ -20,7 +20,7 @@ namespace Cysharp.Threading.Tasks.Linq
{ {
public static partial class UniTaskAsyncEnumerable public static partial class UniTaskAsyncEnumerable
{ {
<# for(var i = 1; i <= tMax; i++) { #> <# for(var i = 2; i <= tMax; i++) { #>
public static IUniTaskAsyncEnumerable<TResult> CombineLatest<<#= typeArgs(i) #>>(this <#= paramArgs(i) #>, Func<<#= typeArgs(i) #>> resultSelector) public static IUniTaskAsyncEnumerable<TResult> CombineLatest<<#= typeArgs(i) #>>(this <#= paramArgs(i) #>, Func<<#= typeArgs(i) #>> resultSelector)
{ {
<# for(var j = 1; j <= i; j++) { #> <# for(var j = 1; j <= i; j++) { #>
@ -34,7 +34,7 @@ namespace Cysharp.Threading.Tasks.Linq
<# } #> <# } #>
} }
<# for(var i = 1; i <= tMax; i++) { #> <# for(var i = 2; i <= tMax; i++) { #>
internal class CombineLatest<<#= typeArgs(i) #>> : IUniTaskAsyncEnumerable<TResult> internal class CombineLatest<<#= typeArgs(i) #>> : IUniTaskAsyncEnumerable<TResult>
{ {
<# for(var j = 1; j <= i; j++) { #> <# for(var j = 1; j <= i; j++) { #>
@ -76,7 +76,6 @@ namespace Cysharp.Threading.Tasks.Linq
T<#= j #> current<#= j #>; T<#= j #> current<#= j #>;
<# } #> <# } #>
int completedCount; int completedCount;
bool syncRunning; bool syncRunning;
TResult result; TResult result;
@ -113,7 +112,14 @@ namespace Cysharp.Threading.Tasks.Linq
{ {
running<#= j #> = true; running<#= j #> = true;
awaiter<#= j #> = enumerator<#= j #>.MoveNextAsync().GetAwaiter(); awaiter<#= j #> = enumerator<#= j #>.MoveNextAsync().GetAwaiter();
awaiter<#= j #>.SourceOnCompleted(Completed<#= j #>Delegate, this); if (awaiter<#= j #>.IsCompleted)
{
Completed<#= j #>(this);
}
else
{
awaiter<#= j #>.SourceOnCompleted(Completed<#= j #>Delegate, this);
}
} }
<# } #> <# } #>
@ -142,11 +148,11 @@ namespace Cysharp.Threading.Tasks.Linq
} }
else else
{ {
self.running<#= j #> = true; // as complete, no more call MoveNextAsync.
if (Interlocked.Increment(ref self.completedCount) == CompleteCount) if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
{ {
goto COMPLETE; goto COMPLETE;
} }
self.running<#= j #> = true; // as complete.
return; return;
} }
} }
@ -181,7 +187,6 @@ namespace Cysharp.Threading.Tasks.Linq
} }
<# } #> <# } #>
bool TrySetResult() bool TrySetResult()
{ {
if (<#= string.Join(" && ", Enumerable.Range(1, i).Select(x => $"hasCurrent{x}")) #>) if (<#= string.Join(" && ", Enumerable.Range(1, i).Select(x => $"hasCurrent{x}")) #>)