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

io.reactivex.observable.internal.operators.ObservableRepeatWhen Maven / Gradle / Ivy

/**
 * Copyright (c) 2016-present, RxJava Contributors.
 *
 * 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 io.reactivex.observable.internal.operators;

import java.util.concurrent.atomic.*;

import io.reactivex.common.Disposable;
import io.reactivex.common.exceptions.Exceptions;
import io.reactivex.common.functions.Function;
import io.reactivex.common.internal.disposables.DisposableHelper;
import io.reactivex.common.internal.functions.ObjectHelper;
import io.reactivex.common.internal.utils.AtomicThrowable;
import io.reactivex.observable.*;
import io.reactivex.observable.internal.disposables.EmptyDisposable;
import io.reactivex.observable.internal.utils.HalfSerializer;
import io.reactivex.observable.subjects.*;

/**
 * Repeatedly subscribe to a source if a handler ObservableSource signals an item.
 *
 * @param  the value type
 */
public final class ObservableRepeatWhen extends AbstractObservableWithUpstream {

    final Function, ? extends ObservableSource> handler;

    public ObservableRepeatWhen(ObservableSource source, Function, ? extends ObservableSource> handler) {
        super(source);
        this.handler = handler;
    }

    @Override
    protected void subscribeActual(Observer observer) {
        Subject signaller = PublishSubject.create().toSerialized();

        ObservableSource other;

        try {
            other = ObjectHelper.requireNonNull(handler.apply(signaller), "The handler returned a null ObservableSource");
        } catch (Throwable ex) {
            Exceptions.throwIfFatal(ex);
            EmptyDisposable.error(ex, observer);
            return;
        }

        RepeatWhenObserver parent = new RepeatWhenObserver(observer, signaller, source);
        observer.onSubscribe(parent);

        other.subscribe(parent.inner);

        parent.subscribeNext();
    }

    static final class RepeatWhenObserver extends AtomicInteger implements Observer, Disposable {

        private static final long serialVersionUID = 802743776666017014L;

        final Observer actual;

        final AtomicInteger wip;

        final AtomicThrowable error;

        final Subject signaller;

        final InnerRepeatObserver inner;

        final AtomicReference d;

        final ObservableSource source;

        volatile boolean active;

        RepeatWhenObserver(Observer actual, Subject signaller, ObservableSource source) {
            this.actual = actual;
            this.signaller = signaller;
            this.source = source;
            this.wip = new AtomicInteger();
            this.error = new AtomicThrowable();
            this.inner = new InnerRepeatObserver();
            this.d = new AtomicReference();
        }

        @Override
        public void onSubscribe(Disposable d) {
            DisposableHelper.replace(this.d, d);
        }

        @Override
        public void onNext(T t) {
            HalfSerializer.onNext(actual, t, this, error);
        }

        @Override
        public void onError(Throwable e) {
            DisposableHelper.dispose(inner);
            HalfSerializer.onError(actual, e, this, error);
        }

        @Override
        public void onComplete() {
            active = false;
            signaller.onNext(0);
        }

        @Override
        public boolean isDisposed() {
            return DisposableHelper.isDisposed(d.get());
        }

        @Override
        public void dispose() {
            DisposableHelper.dispose(d);
            DisposableHelper.dispose(inner);
        }

        void innerNext() {
            subscribeNext();
        }

        void innerError(Throwable ex) {
            DisposableHelper.dispose(d);
            HalfSerializer.onError(actual, ex, this, error);
        }

        void innerComplete() {
            DisposableHelper.dispose(d);
            HalfSerializer.onComplete(actual, this, error);
        }

        void subscribeNext() {
            if (wip.getAndIncrement() == 0) {

                do {
                    if (isDisposed()) {
                        return;
                    }

                    if (!active) {
                        active = true;
                        source.subscribe(this);
                    }
                } while (wip.decrementAndGet() != 0);
            }
        }

        final class InnerRepeatObserver extends AtomicReference implements Observer {

            private static final long serialVersionUID = 3254781284376480842L;

            @Override
            public void onSubscribe(Disposable d) {
                DisposableHelper.setOnce(this, d);
            }

            @Override
            public void onNext(Object t) {
                innerNext();
            }

            @Override
            public void onError(Throwable e) {
                innerError(e);
            }

            @Override
            public void onComplete() {
                innerComplete();
            }
        }
    }
}