(@Nullable SessionID sessionId, Command<X> command)
| 143 | } |
| 144 | |
| 145 | public <X> CompletableFuture<X> send(@Nullable SessionID sessionId, Command<X> command) { |
| 146 | long id = NEXT_ID.getAndIncrement(); |
| 147 | |
| 148 | CompletableFuture<X> result = new CompletableFuture<>(); |
| 149 | if (command.getSendsResponse()) { |
| 150 | methodCallbacks.put( |
| 151 | id, |
| 152 | NamedConsumer.of( |
| 153 | command.getMethod(), |
| 154 | inputOrException -> { |
| 155 | if (inputOrException.isRight()) { |
| 156 | try { |
| 157 | X value = command.getMapper().apply(inputOrException.right()); |
| 158 | result.complete(value); |
| 159 | } catch (Throwable e) { |
| 160 | LOG.log( |
| 161 | Level.WARNING, |
| 162 | String.format("Unable to map result for %s", command.getMethod()), |
| 163 | e); |
| 164 | result.completeExceptionally(e); |
| 165 | } |
| 166 | } else { |
| 167 | result.completeExceptionally(inputOrException.left()); |
| 168 | } |
| 169 | })); |
| 170 | } |
| 171 | |
| 172 | Map<String, Object> serialized = new LinkedHashMap<>(); |
| 173 | serialized.put("id", id); |
| 174 | serialized.put("method", command.getMethod()); |
| 175 | serialized.put("params", command.getParams()); |
| 176 | if (sessionId != null) { |
| 177 | serialized.put("sessionId", sessionId); |
| 178 | } |
| 179 | |
| 180 | StringBuilder json = new StringBuilder(); |
| 181 | try (JsonOutput out = JSON.newOutput(json).writeClassName(false)) { |
| 182 | out.write(Map.copyOf(serialized)); |
| 183 | } |
| 184 | LOG.log(getDebugLogLevel(), "-> {0}", json); |
| 185 | socket.sendText(json); |
| 186 | |
| 187 | if (!command.getSendsResponse()) { |
| 188 | // As far as I see, it never happens. |
| 189 | // All DevTools commands return something - at least empty map. |
| 190 | //noinspection DataFlowIssue |
| 191 | result.complete(null); |
| 192 | } |
| 193 | |
| 194 | return result; |
| 195 | } |
| 196 | |
| 197 | public <X> X sendAndWait(@Nullable SessionID sessionId, Command<X> command, Duration timeout) { |
| 198 | try { |
no test coverage detected