Search in sources :

Example 1 with Emitter

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);
}
Also used : Response(com.apollographql.apollo.api.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Emitter(rx.Emitter) Nonnull(javax.annotation.Nonnull) ApolloException(com.apollographql.apollo.exception.ApolloException) Cancellable(rx.functions.Cancellable) Nonnull(javax.annotation.Nonnull)

Example 2 with Emitter

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);
}
Also used : Response(com.apollographql.apollo.api.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Emitter(rx.Emitter) Nonnull(javax.annotation.Nonnull) ApolloException(com.apollographql.apollo.exception.ApolloException) Cancellable(rx.functions.Cancellable) Nonnull(javax.annotation.Nonnull)

Example 3 with Emitter

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);
}
Also used : Response(com.apollographql.apollo.api.Response) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Emitter(rx.Emitter) Nonnull(javax.annotation.Nonnull) ApolloException(com.apollographql.apollo.exception.ApolloException) Cancellable(rx.functions.Cancellable) Nonnull(javax.annotation.Nonnull)

Example 4 with Emitter

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);
}
Also used : CompositeSubscription(rx.subscriptions.CompositeSubscription) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) Emitter(rx.Emitter) Observer(rx.Observer) Observable(rx.Observable) CompositeSubscription(rx.subscriptions.CompositeSubscription) IOException(java.io.IOException)

Aggregations

AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Emitter (rx.Emitter)4 Response (com.apollographql.apollo.api.Response)3 ApolloException (com.apollographql.apollo.exception.ApolloException)3 Nonnull (javax.annotation.Nonnull)3 Cancellable (rx.functions.Cancellable)3 IOException (java.io.IOException)1 Observable (rx.Observable)1 Observer (rx.Observer)1 CompositeSubscription (rx.subscriptions.CompositeSubscription)1