|
38 | 38 | import java.util.concurrent.BlockingQueue; |
39 | 39 | import java.util.concurrent.CompletableFuture; |
40 | 40 | import java.util.concurrent.CopyOnWriteArraySet; |
| 41 | +import java.util.concurrent.ExecutionException; |
| 42 | +import java.util.concurrent.TimeUnit; |
| 43 | +import java.util.concurrent.TimeoutException; |
41 | 44 | import java.util.concurrent.atomic.AtomicBoolean; |
42 | 45 | import java.util.function.Consumer; |
43 | 46 | import java.util.stream.Collectors; |
@@ -125,7 +128,7 @@ private void mainLoop() { |
125 | 128 |
|
126 | 129 | synchronized (requestLock) { |
127 | 130 | if (requestQueue.isEmpty()) { |
128 | | - Messages.log("PPS: Completed"); |
| 131 | + Messages.log("PPS: Done"); |
129 | 132 | preprocessingTask.complete(prevResult); |
130 | 133 | } |
131 | 134 | } |
@@ -176,17 +179,33 @@ public void notifyCodeFolderChanged() { |
176 | 179 | } |
177 | 180 |
|
178 | 181 |
|
179 | | - public void whenDone(Consumer<PreprocessedSketch> callback) { |
180 | | - if (!isEnabled) return; |
| 182 | + private CompletableFuture<?> registerCallback(Consumer<PreprocessedSketch> callback) { |
181 | 183 | synchronized (requestLock) { |
182 | 184 | lastCallback = preprocessingTask |
183 | 185 | // Run callback after both preprocessing task and previous callback |
184 | 186 | .thenAcceptBothAsync(lastCallback, (ps, a) -> callback.accept(ps)) |
185 | 187 | // Make sure exception in callback won't cancel whole callback chain |
186 | 188 | .handleAsync((res, e) -> { |
187 | | - if (e != null) Messages.loge("exception in preprocessing callback", e); |
| 189 | + if (e != null) Messages.loge("PPS: exception in callback", e); |
188 | 190 | return res; |
189 | 191 | }); |
| 192 | + return lastCallback; |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | + public void whenDone(Consumer<PreprocessedSketch> callback) { |
| 198 | + if (!isEnabled) return; |
| 199 | + registerCallback(callback); |
| 200 | + } |
| 201 | + |
| 202 | + |
| 203 | + public void whenDoneBlocking(Consumer<PreprocessedSketch> callback) { |
| 204 | + if (!isEnabled) return; |
| 205 | + try { |
| 206 | + registerCallback(callback).get(3000, TimeUnit.SECONDS); |
| 207 | + } catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 208 | + // Don't care |
190 | 209 | } |
191 | 210 | } |
192 | 211 |
|
|
0 commit comments