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