All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rx.internal.operators.SingleDoOnEvent Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2014 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package rx.internal.operators;

import rx.Single;
import rx.SingleSubscriber;
import rx.exceptions.CompositeException;
import rx.exceptions.Exceptions;
import rx.functions.Action1;

public final class SingleDoOnEvent implements Single.OnSubscribe {
    final Single source;
    final Action1 onSuccess;
    final Action1 onError;

    public SingleDoOnEvent(Single source, Action1 onSuccess, Action1 onError) {
        this.source = source;
        this.onSuccess = onSuccess;
        this.onError = onError;
    }

    @Override
    public void call(SingleSubscriber actual) {
        SingleDoOnEventSubscriber parent = new SingleDoOnEventSubscriber(actual, onSuccess, onError);
        actual.add(parent);
        source.subscribe(parent);
    }

    static final class SingleDoOnEventSubscriber extends SingleSubscriber {
        final SingleSubscriber actual;
        final Action1 onSuccess;
        final Action1 onError;

        SingleDoOnEventSubscriber(SingleSubscriber actual, Action1 onSuccess, Action1 onError) {
            this.actual = actual;
            this.onSuccess = onSuccess;
            this.onError = onError;
        }

        @Override
        public void onSuccess(T value) {
            try {
                onSuccess.call(value);
            } catch (Throwable e) {
                Exceptions.throwOrReport(e, this, value);
                return;
            }

            actual.onSuccess(value);
        }

        @Override
        public void onError(Throwable error) {
            try {
                onError.call(error);
            } catch (Throwable e) {
                Exceptions.throwIfFatal(e);
                actual.onError(new CompositeException(error, e));
                return;
            }

            actual.onError(error);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy