Launcher/LauncherAPI/src/main/java/pro/gravit/launcher/request/RequestService.java

42 lines
1.2 KiB
Java
Raw Normal View History

2021-11-16 14:32:52 +03:00
package pro.gravit.launcher.request;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public interface RequestService {
<T extends WebSocketEvent> CompletableFuture<T> request(Request<T> request) throws IOException;
2022-11-18 10:47:03 +03:00
2021-11-16 14:32:52 +03:00
void registerEventHandler(EventHandler handler);
2022-11-18 10:47:03 +03:00
2021-11-16 14:32:52 +03:00
void unregisterEventHandler(EventHandler handler);
2022-11-18 10:47:03 +03:00
default <T extends WebSocketEvent> T requestSync(Request<T> request) throws IOException {
2021-11-16 14:32:52 +03:00
try {
return request(request).get();
} catch (InterruptedException e) {
throw new RequestException("Request interrupted");
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof IOException)
throw (IOException) e.getCause();
else {
throw new RequestException(cause);
}
}
}
boolean isClosed();
@FunctionalInterface
public interface EventHandler {
/**
* @param event processing event
* @param <T> event type
* @return false - continue, true - stop
*/
<T extends WebSocketEvent> boolean eventHandle(T event);
}
}