PDEX + PPS: add blocking callbacks

This commit is contained in:
Jakub Valtar
2016-05-06 19:34:17 +02:00
parent d16c0a5072
commit b1eb7bec15
2 changed files with 26 additions and 7 deletions

View File

@@ -102,21 +102,21 @@ public class PDEX {
public void handleShowUsage(int tabIndex, int startTabOffset, int stopTabOffset) {
Messages.log("* handleShowUsage");
if (!enabled) return; // show usage disabled if java tabs
pps.whenDone(ps -> showUsage.findUsageAndUpdateTree(ps, tabIndex, startTabOffset, stopTabOffset));
pps.whenDoneBlocking(ps -> showUsage.findUsageAndUpdateTree(ps, tabIndex, startTabOffset, stopTabOffset));
}
public void handleRename(int tabIndex, int startTabOffset, int stopTabOffset) {
Messages.log("* handleRename");
if (!enabled) return; // refactoring disabled w/ java tabs
pps.whenDone(ps -> rename.handleRename(ps, tabIndex, startTabOffset, stopTabOffset));
pps.whenDoneBlocking(ps -> rename.handleRename(ps, tabIndex, startTabOffset, stopTabOffset));
}
public void handleCtrlClick(int tabIndex, int offset) {
Messages.log("* handleCtrlClick");
if (!enabled) return; // disabled w/ java tabs
pps.whenDone(ps -> handleCtrlClick(ps, tabIndex, offset));
pps.whenDoneBlocking(ps -> handleCtrlClick(ps, tabIndex, offset));
}

View File

@@ -38,6 +38,9 @@ import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@@ -125,7 +128,7 @@ public class PreprocessingService {
synchronized (requestLock) {
if (requestQueue.isEmpty()) {
Messages.log("PPS: Completed");
Messages.log("PPS: Done");
preprocessingTask.complete(prevResult);
}
}
@@ -176,17 +179,33 @@ public class PreprocessingService {
}
public void whenDone(Consumer<PreprocessedSketch> callback) {
if (!isEnabled) return;
private CompletableFuture<?> registerCallback(Consumer<PreprocessedSketch> callback) {
synchronized (requestLock) {
lastCallback = preprocessingTask
// Run callback after both preprocessing task and previous callback
.thenAcceptBothAsync(lastCallback, (ps, a) -> callback.accept(ps))
// Make sure exception in callback won't cancel whole callback chain
.handleAsync((res, e) -> {
if (e != null) Messages.loge("exception in preprocessing callback", e);
if (e != null) Messages.loge("PPS: exception in callback", e);
return res;
});
return lastCallback;
}
}
public void whenDone(Consumer<PreprocessedSketch> callback) {
if (!isEnabled) return;
registerCallback(callback);
}
public void whenDoneBlocking(Consumer<PreprocessedSketch> callback) {
if (!isEnabled) return;
try {
registerCallback(callback).get(3000, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
// Don't care
}
}