Skip to content

Commit c8a3c5b

Browse files
committed
[java][bidi] Add constructor parameter for commands without a response
1 parent 8124086 commit c8a3c5b

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

java/src/org/openqa/selenium/bidi/Command.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class Command<X> {
3131
private final String method;
3232
private final Map<String, Object> params;
3333
private final Function<JsonInput, X> mapper;
34+
private final boolean sendsResponse;
3435

3536
public Command(String method, Map<String, Object> params) {
3637
this(method, params, Object.class);
@@ -41,9 +42,14 @@ public Command(String method, Map<String, Object> params, Type typeOfX) {
4142
}
4243

4344
public Command(String method, Map<String, Object> params, Function<JsonInput, X> mapper) {
45+
this(method, params, mapper, true);
46+
}
47+
48+
public Command(String method, Map<String, Object> params, Function<JsonInput, X> mapper, boolean sendsResponse) {
4449
this.method = Require.nonNull("Method name", method);
4550
this.params = ImmutableMap.copyOf(Require.nonNull("Command parameters", params));
4651
this.mapper = Require.nonNull("Mapper for result", mapper);
52+
this.sendsResponse = sendsResponse;
4753
}
4854

4955
public String getMethod() {
@@ -54,6 +60,10 @@ public Map<String, Object> getParams() {
5460
return params;
5561
}
5662

63+
public boolean getSendsResponse() {
64+
return sendsResponse;
65+
}
66+
5767
Function<JsonInput, X> getMapper() {
5868
return mapper;
5969
}

java/src/org/openqa/selenium/bidi/Connection.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,23 @@ public <X> CompletableFuture<X> send(Command<X> command) {
108108
long id = NEXT_ID.getAndIncrement();
109109

110110
CompletableFuture<X> result = new CompletableFuture<>();
111-
methodCallbacks.put(id, NamedConsumer.of(command.getMethod(), inputOrException -> {
112-
if (inputOrException.isRight()) {
113-
try {
114-
X value = command.getMapper().apply(inputOrException.right());
115-
result.complete(value);
116-
} catch (Exception e) {
117-
LOG.log(Level.WARNING, String.format("Unable to map result for %s", command.getMethod()),
118-
e);
119-
result.completeExceptionally(e);
111+
if (command.getSendsResponse()) {
112+
methodCallbacks.put(id, NamedConsumer.of(command.getMethod(), inputOrException -> {
113+
if (inputOrException.isRight()) {
114+
try {
115+
X value = command.getMapper().apply(inputOrException.right());
116+
result.complete(value);
117+
} catch (Exception e) {
118+
LOG.log(Level.WARNING,
119+
String.format("Unable to map result for %s", command.getMethod()),
120+
e);
121+
result.completeExceptionally(e);
122+
}
123+
} else {
124+
result.completeExceptionally(inputOrException.left());
120125
}
121-
} else {
122-
result.completeExceptionally(inputOrException.left());
123-
}
124-
}));
126+
}));
127+
}
125128

126129
ImmutableMap.Builder<String, Object> serialized = ImmutableMap.builder();
127130
serialized.put("id", id);
@@ -135,6 +138,10 @@ public <X> CompletableFuture<X> send(Command<X> command) {
135138
LOG.log(getDebugLogLevel(), () -> String.format("-> %s", json));
136139
socket.sendText(json);
137140

141+
if (!command.getSendsResponse()) {
142+
result.complete(null);
143+
}
144+
138145
return result;
139146
}
140147

0 commit comments

Comments
 (0)