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

io.reactivex.rxjava3.internal.operators.flowable.FlowableRepeatWhen Maven / Gradle / Ivy

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

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.rxjava3.internal.operators.flowable;

import java.util.Objects;
import java.util.concurrent.atomic.*;

import org.reactivestreams.*;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.exceptions.Exceptions;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.internal.subscriptions.*;
import io.reactivex.rxjava3.processors.*;
import io.reactivex.rxjava3.subscribers.SerializedSubscriber;

public final class FlowableRepeatWhen extends AbstractFlowableWithUpstream {
    final Function, ? extends Publisher> handler;

    public FlowableRepeatWhen(Flowable source,
            Function, ? extends Publisher> handler) {
        super(source);
        this.handler = handler;
    }

    @Override
    public void subscribeActual(Subscriber s) {

        SerializedSubscriber z = new SerializedSubscriber<>(s);

        FlowableProcessor processor = UnicastProcessor.create(8).toSerialized();

        Publisher when;

        try {
            when = Objects.requireNonNull(handler.apply(processor), "handler returned a null Publisher");
        } catch (Throwable ex) {
            Exceptions.throwIfFatal(ex);
            EmptySubscription.error(ex, s);
            return;
        }

        WhenReceiver receiver = new WhenReceiver<>(source);

        RepeatWhenSubscriber subscriber = new RepeatWhenSubscriber<>(z, processor, receiver);

        receiver.subscriber = subscriber;

        s.onSubscribe(subscriber);

        when.subscribe(receiver);

        receiver.onNext(0);
    }

    static final class WhenReceiver
    extends AtomicInteger
    implements FlowableSubscriber, Subscription {

        private static final long serialVersionUID = 2827772011130406689L;

        final Publisher source;

        final AtomicReference upstream;

        final AtomicLong requested;

        WhenSourceSubscriber subscriber;

        WhenReceiver(Publisher source) {
            this.source = source;
            this.upstream = new AtomicReference<>();
            this.requested = new AtomicLong();
        }

        @Override
        public void onSubscribe(Subscription s) {
            SubscriptionHelper.deferredSetOnce(upstream, requested, s);
        }

        @Override
        public void onNext(Object t) {
            if (getAndIncrement() == 0) {
                for (;;) {
                    if (upstream.get() == SubscriptionHelper.CANCELLED) {
                        return;
                    }

                    source.subscribe(subscriber);

                    if (decrementAndGet() == 0) {
                        break;
                    }
                }
            }
        }

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

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

        @Override
        public void request(long n) {
            SubscriptionHelper.deferredRequest(upstream, requested, n);
        }

        @Override
        public void cancel() {
            SubscriptionHelper.cancel(upstream);
        }
    }

    abstract static class WhenSourceSubscriber extends SubscriptionArbiter implements FlowableSubscriber {

        private static final long serialVersionUID = -5604623027276966720L;

        protected final Subscriber downstream;

        protected final FlowableProcessor processor;

        protected final Subscription receiver;

        private long produced;

        WhenSourceSubscriber(Subscriber actual, FlowableProcessor processor,
                Subscription receiver) {
            super(false);
            this.downstream = actual;
            this.processor = processor;
            this.receiver = receiver;
        }

        @Override
        public final void onSubscribe(Subscription s) {
            setSubscription(s);
        }

        @Override
        public final void onNext(T t) {
            produced++;
            downstream.onNext(t);
        }

        protected final void again(U signal) {
            setSubscription(EmptySubscription.INSTANCE);
            long p = produced;
            if (p != 0L) {
                produced = 0L;
                produced(p);
            }
            receiver.request(1);
            processor.onNext(signal);
        }

        @Override
        public final void cancel() {
            super.cancel();
            receiver.cancel();
        }
    }

    static final class RepeatWhenSubscriber extends WhenSourceSubscriber {

        private static final long serialVersionUID = -2680129890138081029L;

        RepeatWhenSubscriber(Subscriber actual, FlowableProcessor processor,
                Subscription receiver) {
            super(actual, processor, receiver);
        }

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

        @Override
        public void onComplete() {
            again(0);
        }
    }
}