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

io.reactivex.flowable.internal.operators.FlowableWithLatestFrom 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.atomic.*;

import org.reactivestreams.*;

import hu.akarnokd.reactivestreams.extensions.RelaxedSubscriber;
import io.reactivex.common.exceptions.Exceptions;
import io.reactivex.common.functions.BiFunction;
import io.reactivex.common.internal.functions.ObjectHelper;
import io.reactivex.flowable.Flowable;
import io.reactivex.flowable.internal.subscriptions.SubscriptionHelper;
import io.reactivex.flowable.subscribers.SerializedSubscriber;

public final class FlowableWithLatestFrom extends AbstractFlowableWithUpstream {
    final BiFunction combiner;
    final Publisher other;
    public FlowableWithLatestFrom(Flowable source, BiFunction combiner, Publisher other) {
        super(source);
        this.combiner = combiner;
        this.other = other;
    }

    @Override
    protected void subscribeActual(Subscriber s) {
        final SerializedSubscriber serial = new SerializedSubscriber(s);
        final WithLatestFromSubscriber wlf = new WithLatestFromSubscriber(serial, combiner);

        serial.onSubscribe(wlf);

        other.subscribe(new FlowableWithLatestSubscriber(wlf));

        source.subscribe(wlf);
    }

    static final class WithLatestFromSubscriber extends AtomicReference implements RelaxedSubscriber, Subscription {

        private static final long serialVersionUID = -312246233408980075L;

        final Subscriber actual;
        final BiFunction combiner;

        final AtomicReference s = new AtomicReference();

        final AtomicLong requested = new AtomicLong();

        final AtomicReference other = new AtomicReference();

        WithLatestFromSubscriber(Subscriber actual, BiFunction combiner) {
            this.actual = actual;
            this.combiner = combiner;
        }
        @Override
        public void onSubscribe(Subscription s) {
            SubscriptionHelper.deferredSetOnce(this.s, requested, s);
        }

        @Override
        public void onNext(T t) {
            U u = get();
            if (u != null) {
                R r;
                try {
                    r = ObjectHelper.requireNonNull(combiner.apply(t, u), "The combiner returned a null value");
                } catch (Throwable e) {
                    Exceptions.throwIfFatal(e);
                    cancel();
                    actual.onError(e);
                    return;
                }
                actual.onNext(r);
            }
        }

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

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

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

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

        public boolean setOther(Subscription o) {
            return SubscriptionHelper.setOnce(other, o);
        }

        public void otherError(Throwable e) {
            SubscriptionHelper.cancel(s);
            actual.onError(e);
        }
    }

    final class FlowableWithLatestSubscriber implements RelaxedSubscriber {
        private final WithLatestFromSubscriber wlf;

        FlowableWithLatestSubscriber(WithLatestFromSubscriber wlf) {
            this.wlf = wlf;
        }

        @Override
        public void onSubscribe(Subscription s) {
            if (wlf.setOther(s)) {
                s.request(Long.MAX_VALUE);
            }
        }

        @Override
        public void onNext(U t) {
            wlf.lazySet(t);
        }

        @Override
        public void onError(Throwable t) {
            wlf.otherError(t);
        }

        @Override
        public void onComplete() {
            // nothing to do, the wlf will complete on its own pace
        }
    }
}