Files
TorchPlugins/Maintenance/Extensions/CompletableFutureExtensions.cs
zznty 5e4bef5b01 meh
2023-11-20 13:57:12 +07:00

31 lines
916 B
C#

using java.util.concurrent;
using java.util.function;
using Torch.Utils;
namespace Maintenance.Extensions;
public static class CompletableFutureExtensions
{
[ReflectedStaticMethod(Type = typeof(Consumer), Name = "<default>andThen")]
private static readonly Func<Consumer, Consumer, Consumer> AndThenDefault = null!;
public static Task<T> ToTask<T>(this CompletableFuture completableFuture)
{
var taskCompletionSource = new TaskCompletionSource<T>();
completableFuture.thenAccept(new TaskConsumer<T>(taskCompletionSource));
return taskCompletionSource.Task;
}
private sealed class TaskConsumer<T>(TaskCompletionSource<T> completionSource) : Consumer
{
public void accept(object t)
{
completionSource.SetResult((T)t);
}
public Consumer andThen(Consumer after) => AndThenDefault(this, after);
}
}