Search in sources :

Example 1 with Cancellable

use of rx.functions.Cancellable 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 Cancellable

use of rx.functions.Cancellable 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 Cancellable

use of rx.functions.Cancellable 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)

Aggregations

Response (com.apollographql.apollo.api.Response)3 ApolloException (com.apollographql.apollo.exception.ApolloException)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Nonnull (javax.annotation.Nonnull)3 Emitter (rx.Emitter)3 Cancellable (rx.functions.Cancellable)3