Skip to content

Commit dcca7e0

Browse files
authored
Add utility method for request cancellation (#915)
1 parent 5d0dc40 commit dcca7e0

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/CompletableFutures.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,32 @@
1919
public final class CompletableFutures {
2020
private CompletableFutures() {}
2121

22+
/**
23+
* A utility method to cancel the JSON-RPC request associated with the given future.
24+
* <p>
25+
* If the given future does not pertain to a JSON-RPC request or is <code>null</code>,
26+
* this method has no effect.
27+
*
28+
* @param future a {@link CompletableFuture}, or <code>null</code>
29+
* @param cancelRootFuture if <code>false</code>, this method will only ensure that a cancel
30+
* notification has been sent for the pending request to the remote endpoint, without attempting
31+
* to cancel any future pertaining to the request; if <code>true</code>, this method will
32+
* also attempt to cancel the root future for the request
33+
* @return <code>false</code> if the given future does not pertain to a JSON-RPC request
34+
* or is <code>null</code>; <code>true</code> otherwise
35+
*/
36+
public static boolean cancelRequest(CompletableFuture<?> future, boolean cancelRootFuture) {
37+
if (future instanceof JsonRpcRequestFuture) {
38+
if (cancelRootFuture) {
39+
((JsonRpcRequestFuture<?>) future).getRoot().cancel(true);
40+
} else {
41+
((JsonRpcRequestFuture<?>) future).cancelRequest();
42+
}
43+
return true;
44+
}
45+
return false;
46+
}
47+
2248
/**
2349
* A utility method to create a {@link CompletableFuture} with cancellation support.
2450
*

0 commit comments

Comments
 (0)