Skip to content

Instantly share code, notes, and snippets.

@jbrisbin
Last active August 29, 2015 14:18
Show Gist options
  • Save jbrisbin/48ad8f8a254d7d7705fa to your computer and use it in GitHub Desktop.
Save jbrisbin/48ad8f8a254d7d7705fa to your computer and use it in GitHub Desktop.
Using Buffer and Promise rather than raw Publisher contracts
interface Buffer<B> extends Publisher<B> {
<NEWB> Buffer<NEWB> intercept(Interceptor<B, NEWB> fn);
Buffer<B> append(Buffer<B> buff);
Buffer<B> append(B obj);
}
interface Promise<T> extends Subscriber<T> {
boolean isComplete();
boolean isError();
T getValue();
Throwable getCause();
Promise<T> onSuccess(Handler<T> callback);
Promise<T> onError(Handler<Throwable> callback);
}
class Promises {
static <T> Publisher<T> toPublisher(Promise<T> promise);
}
interface Connection {
Buffer<Object> receive();
Promise<Void> send(Buffer<Object> buff);
}
server
.intercept(c -> new ConnectionWrapperImpl(c))
.handler(conn -> {
conn.receive()
.intercept(converter::convertToPojo)
.subscribe(new Subscriber<Pojo>() {});
Buffer<Pojo> b = pool.alloc(Pojo.class);
b.append(new Pojo());
toPublisher(conn.send(b.intercept(convert::convertToBuffer)))
.subscribe(new Subscriber<Void>() {});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment