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

io.reactivex.flowable.internal.operators.FlowableTimeoutTimed 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.*;
import java.util.concurrent.atomic.AtomicReference;

import org.reactivestreams.*;

import hu.akarnokd.reactivestreams.extensions.RelaxedSubscriber;
import io.reactivex.common.*;
import io.reactivex.common.Scheduler.Worker;
import io.reactivex.common.internal.disposables.DisposableHelper;
import io.reactivex.flowable.Flowable;
import io.reactivex.flowable.internal.subscribers.FullArbiterSubscriber;
import io.reactivex.flowable.internal.subscriptions.*;
import io.reactivex.flowable.subscribers.SerializedSubscriber;

public final class FlowableTimeoutTimed extends AbstractFlowableWithUpstream {
    final long timeout;
    final TimeUnit unit;
    final Scheduler scheduler;
    final Publisher other;

    static final Disposable NEW_TIMER = new EmptyDispose();

    public FlowableTimeoutTimed(Flowable source,
            long timeout, TimeUnit unit, Scheduler scheduler, Publisher other) {
        super(source);
        this.timeout = timeout;
        this.unit = unit;
        this.scheduler = scheduler;
        this.other = other;
    }

    @Override
    protected void subscribeActual(Subscriber s) {
        if (other == null) {
            source.subscribe(new TimeoutTimedSubscriber(
                    new SerializedSubscriber(s), // because errors can race
                    timeout, unit, scheduler.createWorker()));
        } else {
            source.subscribe(new TimeoutTimedOtherSubscriber(
                    s, // the FullArbiter serializes
                    timeout, unit, scheduler.createWorker(), other));
        }
    }

    static final class TimeoutTimedOtherSubscriber implements RelaxedSubscriber, Disposable {
        final Subscriber actual;
        final long timeout;
        final TimeUnit unit;
        final Scheduler.Worker worker;
        final Publisher other;

        Subscription s;

        final FullArbiter arbiter;

        final AtomicReference timer = new AtomicReference();

        volatile long index;

        volatile boolean done;

        TimeoutTimedOtherSubscriber(Subscriber actual, long timeout, TimeUnit unit, Worker worker,
                Publisher other) {
            this.actual = actual;
            this.timeout = timeout;
            this.unit = unit;
            this.worker = worker;
            this.other = other;
            this.arbiter = new FullArbiter(actual, this, 8);
        }

        @Override
        public void onSubscribe(Subscription s) {
            if (SubscriptionHelper.validate(this.s, s)) {
                this.s = s;
                if (arbiter.setSubscription(s)) {
                    actual.onSubscribe(arbiter);

                    scheduleTimeout(0L);
                }
            }
        }

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

            if (arbiter.onNext(t, s)) {
                scheduleTimeout(idx);
            }
        }

        void scheduleTimeout(final long idx) {
            Disposable d = timer.get();
            if (d != null) {
                d.dispose();
            }

            if (timer.compareAndSet(d, NEW_TIMER)) {
                d = worker.schedule(new TimeoutTask(idx), timeout, unit);

                DisposableHelper.replace(timer, d);
            }
        }

        void subscribeNext() {
            other.subscribe(new FullArbiterSubscriber(arbiter));
        }

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

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

        @Override
        public void dispose() {
            s.cancel();
            worker.dispose();
        }

        @Override
        public boolean isDisposed() {
            return worker.isDisposed();
        }

        final class TimeoutTask implements Runnable {
            private final long idx;

            TimeoutTask(long idx) {
                this.idx = idx;
            }

            @Override
            public void run() {
                if (idx == index) {
                    done = true;
                    s.cancel();
                    DisposableHelper.dispose(timer);

                    subscribeNext();

                    worker.dispose();
                }
            }
        }
    }

    static final class TimeoutTimedSubscriber implements RelaxedSubscriber, Disposable, Subscription {
        final Subscriber actual;
        final long timeout;
        final TimeUnit unit;
        final Scheduler.Worker worker;

        Subscription s;

        final AtomicReference timer = new AtomicReference();

        volatile long index;

        volatile boolean done;

        TimeoutTimedSubscriber(Subscriber actual, long timeout, TimeUnit unit, Worker worker) {
            this.actual = actual;
            this.timeout = timeout;
            this.unit = unit;
            this.worker = worker;
        }

        @Override
        public void onSubscribe(Subscription s) {
            if (SubscriptionHelper.validate(this.s, s)) {
                this.s = s;
                actual.onSubscribe(this);
                scheduleTimeout(0L);
            }
        }

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

            actual.onNext(t);

            scheduleTimeout(idx);
        }

        void scheduleTimeout(final long idx) {
            Disposable d = timer.get();
            if (d != null) {
                d.dispose();
            }

            if (timer.compareAndSet(d, NEW_TIMER)) {
                d = worker.schedule(new TimeoutTask(idx), timeout, unit);

                DisposableHelper.replace(timer, d);
            }
        }

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

            actual.onError(t);
            worker.dispose();
        }

        @Override
        public void onComplete() {
            if (done) {
                return;
            }
            done = true;

            actual.onComplete();
            worker.dispose();
        }

        @Override
        public void dispose() {
            s.cancel();
            worker.dispose();
        }

        @Override
        public boolean isDisposed() {
            return worker.isDisposed();
        }

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

        @Override
        public void cancel() {
            dispose();
        }

        final class TimeoutTask implements Runnable {
            private final long idx;

            TimeoutTask(long idx) {
                this.idx = idx;
            }

            @Override
            public void run() {
                if (idx == index) {
                    done = true;
                    dispose();

                    actual.onError(new TimeoutException());
                }
            }
        }
    }

    static final class EmptyDispose implements Disposable {
        @Override
        public void dispose() { }

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


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy