Make Response typed
This commit is contained in:
parent
59977b53fe
commit
2ffdecbe41
5 changed files with 26 additions and 12 deletions
|
@ -306,7 +306,7 @@ public final class Capability {
|
|||
|
||||
final CompletableFuture<?> cancelAllowed;
|
||||
MessageBuilder request;
|
||||
Response response;
|
||||
Response<AnyPointer.Reader> response;
|
||||
AnyPointer.Builder responseBuilder;
|
||||
ClientHook clientRef;
|
||||
|
||||
|
@ -333,7 +333,10 @@ public final class Capability {
|
|||
if (this.response == null) {
|
||||
var localResponse = new LocalResponse();
|
||||
this.responseBuilder = localResponse.message.getRoot(AnyPointer.factory);
|
||||
this.response = new Response(this.responseBuilder.asReader(), localResponse);
|
||||
this.response = new Response<>(
|
||||
AnyPointer.factory,
|
||||
this.responseBuilder.asReader(),
|
||||
localResponse);
|
||||
}
|
||||
return this.responseBuilder;
|
||||
}
|
||||
|
|
|
@ -4,15 +4,16 @@ import java.util.concurrent.CompletableFuture;
|
|||
|
||||
class RemotePromise<Results> {
|
||||
|
||||
final CompletableFuture<Response> response;
|
||||
final CompletableFuture<Response<Results>> response;
|
||||
final PipelineHook pipeline;
|
||||
|
||||
RemotePromise(CompletableFuture<Response> response, PipelineHook pipeline) {
|
||||
RemotePromise(CompletableFuture<Response<Results>> response,
|
||||
PipelineHook pipeline) {
|
||||
this.response = response;
|
||||
this.pipeline = pipeline;
|
||||
}
|
||||
|
||||
public CompletableFuture<Response> getResponse() {
|
||||
public CompletableFuture<Response<Results>> getResponse() {
|
||||
return response;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,11 +22,17 @@ public class Request<Params, Results> {
|
|||
return params.getAs(paramsBuilder);
|
||||
}
|
||||
|
||||
CompletableFuture<Results> send() {
|
||||
RemotePromise<Results> send() {
|
||||
var typelessPromise = hook.send();
|
||||
hook = null; // prevent reuse
|
||||
return typelessPromise.getResponse().thenApply(
|
||||
response -> response.getAs(resultsReader));
|
||||
var typedPromise = typelessPromise.getResponse().thenApply(response -> {
|
||||
return new Response<Results>(
|
||||
resultsReader,
|
||||
response.get(),
|
||||
response.hook);
|
||||
});
|
||||
|
||||
return new RemotePromise<Results>(typedPromise, typelessPromise.pipeline);
|
||||
}
|
||||
|
||||
static <T, U> Request<T, U> newBrokenRequest(Throwable exc) {
|
||||
|
|
|
@ -2,7 +2,7 @@ package org.capnproto;
|
|||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
interface RequestHook {
|
||||
public interface RequestHook {
|
||||
RemotePromise<AnyPointer.Reader> send();
|
||||
CompletableFuture<?> sendStreaming();
|
||||
Object getBrand();
|
||||
|
|
|
@ -1,16 +1,20 @@
|
|||
package org.capnproto;
|
||||
|
||||
class Response {
|
||||
class Response<Results> {
|
||||
|
||||
final FromPointerReader<Results> factory;
|
||||
final ResponseHook hook;
|
||||
final AnyPointer.Reader results;
|
||||
|
||||
public Response(AnyPointer.Reader reader, ResponseHook hook) {
|
||||
public Response(FromPointerReader<Results> factory,
|
||||
AnyPointer.Reader reader,
|
||||
ResponseHook hook) {
|
||||
this.factory = factory;
|
||||
this.hook = hook;
|
||||
this.results = reader;
|
||||
}
|
||||
|
||||
public final <T> T getAs(FromPointerReader<T> factory) {
|
||||
public final Results get() {
|
||||
return this.results.getAs(factory);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue