package moe.tristan.easyfxml.util;

import io.vavr.Tuple2;

import javafx.application.Platform;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * Convenience utility methods for cleaner asynchronous cross-thread calls.
 * <p>
 * Basically a glorified replacement of {@link Platform#runLater(Runnable)} that offers task-style methods and allows
 * for computational calls.
 */
public final class FxAsync {

    private FxAsync() {
    }

    /**
     * Asynchronously executes a consuming operation on the JavaFX thread.
     *
     * @param element The element to consume
     * @param action  How to consume it
     * @param <T>     The type of the element consumed
     *
     * @return A {@link CompletionStage} to have monitoring over the state of the asynchronous operation.
     */
    public static <T> CompletionStage<T> doOnFxThread(final T element, final Consumer<T> action) {
        CompletableFuture<T> asyncOp = new CompletableFuture<>();
        Platform.runLater(() -> {
            action.accept(element);
            asyncOp.complete(element);
        });
        return asyncOp;
    }

    /**
     * Asynchronously executes a computing operation on the JavaFX thread.
     *
     * @param element The element(s) required for the computation (use {@link Tuple2} for pair-based for example)
     * @param compute The computation to perform
     * @param <T>     The type of the (aggregated if necessary) inputs
     * @param <U>     The output type
     *
     * @return A {@link CompletionStage} to have monitoring over the state of the asynchronous computation.
     */
    public static <T, U> CompletionStage<U> computeOnFxThread(final T element, final Function<T, U> compute) {
        CompletableFuture<U> asyncComputeOp = new CompletableFuture<>();
        Platform.runLater(() -> asyncComputeOp.complete(compute.apply(element)));
        return asyncComputeOp;
    }
}

@@ -33,69 +33,8 @@
 l;%0A%0A
-import io.vavr.Tuple2;%0A%0Aimport javafx.application.Platform;%0A%0A
 impo
@@ -194,16 +194,77 @@
 ction;%0A%0A
+import javafx.application.Platform;%0A%0Aimport io.vavr.Tuple2;%0A%0A
 /**%0A * C
@@ -1015,91 +1015,44 @@
     
-CompletableFuture%3CT%3E asyncOp = new CompletableFuture%3C%3E();%0A        Platform.runLater
+return CompletableFuture.supplyAsync
 (() 
@@ -1108,33 +1108,22 @@
     
-asyncOp.complete(
+return 
 element
-)
 ;%0A  
@@ -1133,33 +1133,29 @@
    %7D
-);%0A        return asyncOp
+, Platform::runLater)
 ;%0A  
@@ -1767,190 +1767,100 @@
     
-CompletableFuture%3CU%3E asyncComputeOp = new CompletableFuture%3C%3E();%0A        Platform.runLater(() -%3E asyncComputeOp.complete(compute.apply(element)));%0A        return asyncComputeOp
+return CompletableFuture.supplyAsync(() -%3E compute.apply(element), Platform::runLater)
 ;%0A    %7D%0A
 %7D%0A
@@ -1857,10 +1857,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

Fix misused CompletableFuture implementation