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

io.reactivex.flowable.internal.operators.FlowableTimeout Maven / Gradle / Ivy

The newest version!
/**
 * 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.flowable.internal.operators;

import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;

import org.reactivestreams.*;

import hu.akarnokd.reactivestreams.extensions.RelaxedSubscriber;
import io.reactivex.common.*;
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.flowable.Flowable;
import io.reactivex.flowable.internal.subscribers.FullArbiterSubscriber;
import io.reactivex.flowable.internal.subscriptions.*;
import io.reactivex.flowable.subscribers.*;

public final class FlowableTimeout extends AbstractFlowableWithUpstream {
    final Publisher firstTimeoutIndicator;
    final Function> itemTimeoutIndicator;
    final Publisher other;

    public FlowableTimeout(
            Flowable source,
            Publisher firstTimeoutIndicator,
            Function> itemTimeoutIndicator,
            Publisher other) {
        super(source);
        this.firstTimeoutIndicator = firstTimeoutIndicator;
        this.itemTimeoutIndicator = itemTimeoutIndicator;
        this.other = other;
    }

    @Override
    protected void subscribeActual(Subscriber s) {
        if (other == null) {
            source.subscribe(new TimeoutSubscriber(
                    new SerializedSubscriber(s),
                    firstTimeoutIndicator, itemTimeoutIndicator));
        } else {
            source.subscribe(new TimeoutOtherSubscriber(
                    s, firstTimeoutIndicator, itemTimeoutIndicator, other));
        }
    }

    static final class TimeoutSubscriber implements RelaxedSubscriber, Subscription, OnTimeout {
        final Subscriber actual;
        final Publisher firstTimeoutIndicator;
        final Function> itemTimeoutIndicator;

        Subscription s;

        volatile boolean cancelled;

        volatile long index;

        final AtomicReference timeout = new AtomicReference();

        TimeoutSubscriber(Subscriber actual,
                Publisher firstTimeoutIndicator,
                Function> itemTimeoutIndicator) {
            this.actual = actual;
            this.firstTimeoutIndicator = firstTimeoutIndicator;
            this.itemTimeoutIndicator = itemTimeoutIndicator;
        }

        @Override
        public void onSubscribe(Subscription s) {
            if (!SubscriptionHelper.validate(this.s, s)) {
                return;
            }
            this.s = s;

            if (cancelled) {
                return;
            }

            Subscriber a = actual;

            Publisher p = firstTimeoutIndicator;

            if (p != null) {
                TimeoutInnerSubscriber tis = new TimeoutInnerSubscriber(this, 0);

                if (timeout.compareAndSet(null, tis)) {
                    a.onSubscribe(this);
                    p.subscribe(tis);
                }
            } else {
                a.onSubscribe(this);
            }
        }

        @Override
        public void onNext(T t) {
            long idx = index + 1;
            index = idx;

            actual.onNext(t);

            Disposable d = timeout.get();
            if (d != null) {
                d.dispose();
            }

            Publisher p;

            try {
                p = ObjectHelper.requireNonNull(itemTimeoutIndicator.apply(t), "The publisher returned is null");
            } catch (Throwable e) {
                Exceptions.throwIfFatal(e);
                cancel();
                actual.onError(e);
                return;
            }

            TimeoutInnerSubscriber tis = new TimeoutInnerSubscriber(this, idx);

            if (timeout.compareAndSet(d, tis)) {
                p.subscribe(tis);
            }
        }

        @Override
        public void onError(Throwable t) {
            cancel();
            actual.onError(t);
        }

        @Override
        public void onComplete() {
            cancel();
            actual.onComplete();
        }

        @Override
        public void request(long n) {
            s.request(n);
        }

        @Override
        public void cancel() {
            cancelled = true;
            s.cancel();
            DisposableHelper.dispose(timeout);
        }

        @Override
        public void timeout(long idx) {
            if (idx == index) {
                cancel();
                actual.onError(new TimeoutException());
            }
        }
    }

    interface OnTimeout {
        void timeout(long index);

        void onError(Throwable e);
    }

    static final class TimeoutInnerSubscriber extends DisposableSubscriber {
        final OnTimeout parent;
        final long index;

        boolean done;

        TimeoutInnerSubscriber(OnTimeout parent, final long index) {
            this.parent = parent;
            this.index = index;
        }

        @Override
        public void onNext(Object t) {
            if (done) {
                return;
            }
            done = true;
            cancel();
            parent.timeout(index);
        }

        @Override
        public void onError(Throwable t) {
            if (done) {
                RxJavaCommonPlugins.onError(t);
                return;
            }
            done = true;
            parent.onError(t);
        }

        @Override
        public void onComplete() {
            if (done) {
                return;
            }
            done = true;
            parent.timeout(index);
        }
    }

    static final class TimeoutOtherSubscriber implements RelaxedSubscriber, Disposable, OnTimeout {
        final Subscriber actual;
        final Publisher firstTimeoutIndicator;
        final Function> itemTimeoutIndicator;
        final Publisher other;
        final FullArbiter arbiter;

        Subscription s;

        boolean done;

        volatile boolean cancelled;

        volatile long index;

        final AtomicReference timeout = new AtomicReference();

        TimeoutOtherSubscriber(Subscriber actual,
                Publisher firstTimeoutIndicator,
                Function> itemTimeoutIndicator, Publisher other) {
            this.actual = actual;
            this.firstTimeoutIndicator = firstTimeoutIndicator;
            this.itemTimeoutIndicator = itemTimeoutIndicator;
            this.other = other;
            this.arbiter = new FullArbiter(actual, this, 8);
        }

        @Override
        public void onSubscribe(Subscription s) {
            if (!SubscriptionHelper.validate(this.s, s)) {
                return;
            }
            this.s = s;

            if (!arbiter.setSubscription(s)) {
                return;
            }
            Subscriber a = actual;

            Publisher p = firstTimeoutIndicator;

            if (p != null) {
                TimeoutInnerSubscriber tis = new TimeoutInnerSubscriber(this, 0);

                if (timeout.compareAndSet(null, tis)) {
                    a.onSubscribe(arbiter);
                    p.subscribe(tis);
                }
            } else {
                a.onSubscribe(arbiter);
            }
        }

        @Override
        public void onNext(T t) {
            if (done) {
                return;
            }
            long idx = index + 1;
            index = idx;

            if (!arbiter.onNext(t, s)) {
                return;
            }

            Disposable d = timeout.get();
            if (d != null) {
                d.dispose();
            }

            Publisher p;

            try {
                p = ObjectHelper.requireNonNull(itemTimeoutIndicator.apply(t), "The publisher returned is null");
            } catch (Throwable e) {
                Exceptions.throwIfFatal(e);
                actual.onError(e);
                return;
            }

            TimeoutInnerSubscriber tis = new TimeoutInnerSubscriber(this, idx);

            if (timeout.compareAndSet(d, tis)) {
                p.subscribe(tis);
            }
        }

        @Override
        public void onError(Throwable t) {
            if (done) {
                RxJavaCommonPlugins.onError(t);
                return;
            }
            done = true;
            dispose();
            arbiter.onError(t, s);
        }

        @Override
        public void onComplete() {
            if (done) {
                return;
            }
            done = true;
            dispose();
            arbiter.onComplete(s);
        }

        @Override
        public void dispose() {
            cancelled = true;
            s.cancel();
            DisposableHelper.dispose(timeout);
        }

        @Override
        public boolean isDisposed() {
            return cancelled;
        }

        @Override
        public void timeout(long idx) {
            if (idx == index) {
                dispose();
                other.subscribe(new FullArbiterSubscriber(arbiter));
            }
        }
    }
}