use of rx.Emitter in project apollo-android by apollographql.
the class RxApollo method from.
/**
* Converts an {@link ApolloQueryWatcher} into an Observable.
*
* @param watcher the ApolloQueryWatcher to convert
* @param backpressureMode the back pressure strategy to apply to the observable source.
* @param <T> the value type
* @return the converted Observable
*/
@Nonnull
public static <T> Observable<Response<T>> from(@Nonnull final ApolloQueryWatcher<T> watcher, @Nonnull Emitter.BackpressureMode backpressureMode) {
checkNotNull(backpressureMode, "backpressureMode == null");
checkNotNull(watcher, "watcher == null");
return Observable.create(new Action1<Emitter<Response<T>>>() {
@Override
public void call(final Emitter<Response<T>> emitter) {
final AtomicBoolean canceled = new AtomicBoolean();
emitter.setCancellation(new Cancellable() {
@Override
public void cancel() throws Exception {
canceled.set(true);
watcher.cancel();
}
});
watcher.enqueueAndWatch(new ApolloCall.Callback<T>() {
@Override
public void onResponse(@Nonnull Response<T> response) {
if (!canceled.get()) {
emitter.onNext(response);
}
}
@Override
public void onFailure(@Nonnull ApolloException e) {
Exceptions.throwIfFatal(e);
if (!canceled.get()) {
emitter.onError(e);
}
}
});
}
}, backpressureMode);
}
use of rx.Emitter in project apollo-android by apollographql.
the class RxApollo method from.
/**
* Converts an {@link ApolloCall} to a Observable. The number of emissions this Observable will have is based on the
* {@link ResponseFetcher} used with the call.
*
* @param call the ApolloCall to convert
* @param <T> the value type
* @param backpressureMode The {@link rx.Emitter.BackpressureMode} to use.
* @return the converted Observable
*/
@Nonnull
public static <T> Observable<Response<T>> from(@Nonnull final ApolloCall<T> call, Emitter.BackpressureMode backpressureMode) {
checkNotNull(call, "call == null");
return Observable.create(new Action1<Emitter<Response<T>>>() {
@Override
public void call(final Emitter<Response<T>> emitter) {
final AtomicBoolean canceled = new AtomicBoolean();
emitter.setCancellation(new Cancellable() {
@Override
public void cancel() throws Exception {
canceled.set(true);
call.cancel();
}
});
call.enqueue(new ApolloCall.Callback<T>() {
@Override
public void onResponse(@Nonnull Response<T> response) {
if (!canceled.get()) {
emitter.onNext(response);
}
}
@Override
public void onFailure(@Nonnull ApolloException e) {
Exceptions.throwIfFatal(e);
if (!canceled.get()) {
emitter.onError(e);
}
}
@Override
public void onStatusEvent(@Nonnull ApolloCall.StatusEvent event) {
if (!canceled.get()) {
if (event == ApolloCall.StatusEvent.COMPLETED) {
emitter.onCompleted();
}
}
}
});
}
}, backpressureMode);
}
use of rx.Emitter in project apollo-android by apollographql.
the class RxApollo method from.
@Nonnull
public static <T> Observable<Response<T>> from(@Nonnull final ApolloSubscriptionCall<T> call, Emitter.BackpressureMode backpressureMode) {
checkNotNull(call, "call == null");
return Observable.create(new Action1<Emitter<Response<T>>>() {
@Override
public void call(final Emitter<Response<T>> emitter) {
final AtomicBoolean canceled = new AtomicBoolean();
emitter.setCancellation(new Cancellable() {
@Override
public void cancel() throws Exception {
canceled.set(true);
call.cancel();
}
});
call.execute(new ApolloSubscriptionCall.Callback<T>() {
@Override
public void onResponse(@Nonnull Response<T> response) {
if (!canceled.get()) {
emitter.onNext(response);
}
}
@Override
public void onFailure(@Nonnull ApolloException e) {
Exceptions.throwIfFatal(e);
if (!canceled.get()) {
emitter.onError(e);
}
}
@Override
public void onCompleted() {
if (!canceled.get()) {
emitter.onCompleted();
}
}
});
}
}, backpressureMode);
}
use of rx.Emitter in project scalecube by scalecube.
the class DefaultStreamProcessor method listen.
@Override
public Observable<StreamMessage> listen() {
return Observable.create(emitter -> {
CompositeSubscription subscriptions = new CompositeSubscription();
emitter.setCancellation(subscriptions::clear);
// message logic: remote read => onMessage
subscriptions.add(channelContext.listenReadSuccess().map(Event::getMessageOrThrow).subscribe(message -> onMessage(message, emitter)));
// error logic: failed remote write => observer error
subscriptions.add(channelContext.listenWriteError().map(Event::getErrorOrThrow).subscribe(emitter::onError));
// connection logic: connection lost => observer error
subscriptions.add(eventStream.listenChannelContextClosed().filter(event -> event.getAddress().equals(channelContext.getAddress())).map(event -> new IOException("ChannelContext closed on address: " + event.getAddress())).subscribe(emitter::onError));
}, Emitter.BackpressureMode.BUFFER);
}
Aggregations