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