Awaitable.GetAwaiter().OnCompleted seems undocumented? GetAwaiter() is specifically decorated with [ExcludeFromDocs]. Is this something I should not be touching in my gameplay code?
This is in context of trying to get a viable WhenAll alternative up and running:
using System;
using System.Threading;
using UnityEngine;
namespace Redacted.Core.Utilities
{
public static class AwaitableUtilities
{
public static Awaitable WhenAll(params Awaitable[] awaitables)
{
if (awaitables == null) throw new ArgumentNullException(nameof(awaitables));
if (awaitables.Length == 0)
{
return CompletedAwaitable();
}
var completionSource = new AwaitableCompletionSource();
int remaining = awaitables.Length;
foreach (Awaitable awaitable in awaitables)
{
awaitable.GetAwaiter().OnCompleted(() =>
{
try
{
awaitable.GetAwaiter().GetResult();
}
catch
{
completionSource.TrySetCanceled();
}
finally
{
remaining--;
if (remaining == 0)
{
completionSource.SetResult();
}
}
});
}
return completionSource.Awaitable;
}
public static async Awaitable WaitUntil(Func<bool> condition, CancellationToken cancellationToken)
{
while (!condition())
{
if (cancellationToken.IsCancellationRequested)
{
break;
}
await Awaitable.NextFrameAsync(cancellationToken);
}
}
public static Awaitable CompletedAwaitable() => CompletedAwaitableCache.Get();
private static class CompletedAwaitableCache
{
private static readonly AwaitableCompletionSource s_completionSource = new();
public static Awaitable Get()
{
s_completionSource.SetResult();
Awaitable awaitable = s_completionSource.Awaitable;
s_completionSource.Reset();
return awaitable;
}
}
}
}