/* * Copyright © 2018 Samuel Holland * Copyright © 2018 Jason A. Donenfeld . All Rights Reserved. * SPDX-License-Identifier: GPL-2.0-or-later */ package com.wireguard.android.util; import android.os.Handler; import java.util.concurrent.Executor; import java9.util.concurrent.CompletableFuture; import java9.util.concurrent.CompletionStage; /** * Helper class for running asynchronous tasks and ensuring they are completed on the main thread. */ public class AsyncWorker { private final Executor executor; private final Handler handler; public AsyncWorker(final Executor executor, final Handler handler) { this.executor = executor; this.handler = handler; } public CompletionStage runAsync(final AsyncRunnable runnable) { final CompletableFuture future = new CompletableFuture<>(); executor.execute(() -> { try { runnable.run(); handler.post(() -> future.complete(null)); } catch (final Throwable t) { handler.post(() -> future.completeExceptionally(t)); } }); return future; } public CompletionStage supplyAsync(final AsyncSupplier supplier) { final CompletableFuture future = new CompletableFuture<>(); executor.execute(() -> { try { final T result = supplier.get(); handler.post(() -> future.complete(result)); } catch (final Throwable t) { handler.post(() -> future.completeExceptionally(t)); } }); return future; } @FunctionalInterface public interface AsyncRunnable { void run() throws E; } @FunctionalInterface public interface AsyncSupplier { T get() throws E; } }