meh
This commit is contained in:
31
Maintenance/Extensions/CompletableFutureExtensions.cs
Normal file
31
Maintenance/Extensions/CompletableFutureExtensions.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user