use of org.reactivestreams.Publisher in project RxDownload by ssseasonnn.
the class DownloadType method startDownload.
public Observable<DownloadStatus> startDownload() {
return Flowable.just(1).doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription subscription) throws Exception {
log(startLog());
record.start();
}
}).flatMap(new Function<Integer, Publisher<DownloadStatus>>() {
@Override
public Publisher<DownloadStatus> apply(Integer integer) throws Exception {
return download();
}
}).doOnNext(new Consumer<DownloadStatus>() {
@Override
public void accept(DownloadStatus status) throws Exception {
record.update(status);
}
}).doOnError(new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
log(errorLog());
record.error();
}
}).doOnComplete(new Action() {
@Override
public void run() throws Exception {
log(completeLog());
record.complete();
}
}).doOnCancel(new Action() {
@Override
public void run() throws Exception {
log(cancelLog());
record.cancel();
}
}).doFinally(new Action() {
@Override
public void run() throws Exception {
log(finishLog());
record.finish();
}
}).toObservable();
}
use of org.reactivestreams.Publisher in project RxJava by ReactiveX.
the class TransformerTest method flowableTransformerThrows.
@Test
public void flowableTransformerThrows() {
try {
Flowable.just(1).compose(new FlowableTransformer<Integer, Integer>() {
@Override
public Publisher<Integer> apply(Flowable<Integer> v) {
throw new TestException("Forced failure");
}
});
fail("Should have thrown!");
} catch (TestException ex) {
assertEquals("Forced failure", ex.getMessage());
}
}
use of org.reactivestreams.Publisher in project ratpack by ratpack.
the class ServerSentEvents method render.
/**
* {@inheritDoc}
*/
@Override
public void render(Context context) throws Exception {
ByteBufAllocator bufferAllocator = context.get(ByteBufAllocator.class);
Response response = context.getResponse();
response.getHeaders().add(HttpHeaderConstants.CONTENT_TYPE, HttpHeaderConstants.TEXT_EVENT_STREAM_CHARSET_UTF_8);
response.getHeaders().add(HttpHeaderConstants.TRANSFER_ENCODING, HttpHeaderConstants.CHUNKED);
response.getHeaders().add(HttpHeaderConstants.CACHE_CONTROL, HttpHeaderConstants.NO_CACHE_FULL);
response.getHeaders().add(HttpHeaderConstants.PRAGMA, HttpHeaderConstants.NO_CACHE);
response.sendStream(Streams.map(publisher, i -> ServerSentEventEncoder.INSTANCE.encode(i, bufferAllocator)));
}
use of org.reactivestreams.Publisher in project ratpack by ratpack.
the class WebSockets method websocketBroadcast.
/**
* Sets up a websocket that sends the published Strings to a client.
* <p>
* This takes the place of a {@link Streams#bindExec(Publisher)} call.
*
* @param context the request handling context
* @param broadcaster a {@link Publisher} of Strings to send to the websocket client
*/
public static void websocketBroadcast(final Context context, final Publisher<String> broadcaster) {
ByteBufAllocator bufferAllocator = context.get(ByteBufAllocator.class);
websocketByteBufBroadcast(context, Streams.map(broadcaster, s -> ByteBufUtil.encodeString(bufferAllocator, CharBuffer.wrap(s), CharsetUtil.UTF_8)));
}
use of org.reactivestreams.Publisher in project ratpack by ratpack.
the class ConcatPublisher method subscribe.
@Override
public void subscribe(Subscriber<? super T> s) {
s.onSubscribe(new ManagedSubscription<T>(s, disposer) {
Iterator<? extends Publisher<? extends T>> iterator = publishers.iterator();
Subscription current;
@Override
protected void onRequest(long n) {
if (current == null) {
if (iterator.hasNext()) {
Publisher<? extends T> publisher = iterator.next();
publisher.subscribe(new Subscriber<T>() {
@Override
public void onSubscribe(Subscription s) {
current = s;
s.request(n);
}
@Override
public void onNext(T t) {
emitNext(t);
}
@Override
public void onError(Throwable t) {
emitError(t);
}
@Override
public void onComplete() {
current = null;
long demand = getDemand();
if (demand > 0) {
onRequest(demand);
}
}
});
} else {
emitComplete();
}
} else {
current.request(n);
}
}
@Override
protected void onCancel() {
if (current != null) {
current.cancel();
}
}
});
}
Aggregations