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

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

There is a newer version: 1.3.8
Show newest version
/**
 * Copyright 2016 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 java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicBoolean;

import rx.*;
import rx.Single.OnSubscribe;
import rx.plugins.RxJavaHooks;

/**
 * Relay the source signals if the other doesn't terminate before.
 *
 * @param  the value type
 */
public final class SingleTakeUntilCompletable implements Single.OnSubscribe {

    final Single.OnSubscribe source;

    final Completable other;

    public SingleTakeUntilCompletable(OnSubscribe source, Completable other) {
        this.source = source;
        this.other = other;
    }

    @Override
    public void call(SingleSubscriber t) {
        TakeUntilSourceSubscriber parent = new TakeUntilSourceSubscriber(t);
        t.add(parent);

        other.subscribe(parent);
        source.call(parent);
    }

    static final class TakeUntilSourceSubscriber extends SingleSubscriber
    implements CompletableSubscriber {

        final SingleSubscriber actual;

        final AtomicBoolean once;

        TakeUntilSourceSubscriber(SingleSubscriber actual) {
            this.actual = actual;
            this.once = new AtomicBoolean();
        }

        @Override
        public void onSubscribe(Subscription d) {
            add(d);
        }

        @Override
        public void onSuccess(T value) {
            if (once.compareAndSet(false, true)) {
                unsubscribe();

                actual.onSuccess(value);
            }
        }

        @Override
        public void onError(Throwable error) {
            if (once.compareAndSet(false, true)) {
                unsubscribe();
                actual.onError(error);
            } else {
                RxJavaHooks.onError(error);
            }
        }

        @Override
        public void onCompleted() {
            onError(new CancellationException("Single::takeUntil(Completable) - Stream was canceled before emitting a terminal event."));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy