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

hu.akarnokd.rxjava3.operators.ObservableFlatMapLatest Maven / Gradle / Ivy

Go to download

RxJava 3.x extra sources, operators and components and ports of many 1.x companion libraries.

There is a newer version: 3.1.1
Show newest version
/*
 * Copyright 2016-2019 David Karnok
 *
 * 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 hu.akarnokd.rxjava3.operators;

import java.util.concurrent.atomic.*;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.exceptions.Exceptions;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
import io.reactivex.rxjava3.internal.functions.ObjectHelper;
import io.reactivex.rxjava3.internal.util.AtomicThrowable;

/**
 * FlatMap only one {@link ObservableSource} at a time and keep the latest upstream value until it terminates
 * and resume with the {@code ObservableSource} mapped for that latest upstream value.
 * @param  the upstream value type
 * @param  the output type
 * @since 0.19.0
 */
final class ObservableFlatMapLatest extends Observable implements ObservableTransformer {

    final Observable source;

    final Function> mapper;

    ObservableFlatMapLatest(Observable source,
            Function> mapper) {
        this.source = source;
        this.mapper = mapper;
    }

    @Override
    public ObservableSource apply(Observable upstream) {
        return new ObservableFlatMapLatest(upstream, mapper);
    }

    @Override
    protected void subscribeActual(Observer observer) {
        source.subscribe(new FlatMapLatestObserver(observer, mapper));
    }

    static final class FlatMapLatestObserver
    extends AtomicInteger
    implements Observer, Disposable {

        private static final long serialVersionUID = 1251911925259779985L;

        final Observer downstream;

        final Function> mapper;

        final FlatMapLatestInnerObserver innerObserver;

        final AtomicThrowable errors;

        final AtomicReference latest;

        Disposable upstream;

        volatile boolean active;

        volatile boolean done;

        volatile boolean disposed;

        FlatMapLatestObserver(Observer downstream,
                Function> mapper) {
            this.downstream = downstream;
            this.mapper = mapper;
            this.innerObserver = new FlatMapLatestInnerObserver();
            this.errors = new AtomicThrowable();
            this.latest = new AtomicReference();
        }

        @Override
        public void onSubscribe(Disposable d) {
            if (DisposableHelper.validate(upstream, d)) {
                this.upstream = d;
                downstream.onSubscribe(this);
            }
        }

        @Override
        public void onNext(T t) {
            latest.set(t);
            drain();
        }

        @Override
        public void onError(Throwable e) {
            if (errors.tryAddThrowableOrReport(e)) {
                onComplete();
            }
        }

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

        @Override
        public void dispose() {
            disposed = true;
            upstream.dispose();
            DisposableHelper.dispose(innerObserver);
            errors.tryTerminateAndReport();
            if (getAndIncrement() == 0) {
                latest.lazySet(null);
            }
        }

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

        void innerNext(R item) {
            downstream.onNext(item);
        }

        void innerError(Throwable e) {
            if (errors.tryAddThrowableOrReport(e)) {
                innerComplete();
            }
        }

        void innerComplete() {
            active = false;
            drain();
        }

        void drain() {
            if (getAndIncrement() != 0) {
                return;
            }

            do {
                if (disposed) {
                    latest.lazySet(null);
                    return;
                }
                if (!active) {
                    boolean d = done;
                    T v = latest.getAndSet(null);
                    if (d && v == null) {
                        errors.tryTerminateConsumer(downstream);
                        return;
                    }
                    if (v != null) {
                        ObservableSource o;

                        try {
                            o = ObjectHelper.requireNonNull(mapper.apply(v), "The mapper returned a null ObservableSource");
                        } catch (Throwable ex) {
                            Exceptions.throwIfFatal(ex);
                            upstream.dispose();
                            errors.tryAddThrowableOrReport(ex);
                            errors.tryTerminateConsumer(downstream);
                            return;
                        }

                        active = true;
                        o.subscribe(innerObserver);
                    }
                }
            } while (decrementAndGet() != 0);
        }

        final class FlatMapLatestInnerObserver
        extends AtomicReference
        implements Observer {

            private static final long serialVersionUID = -3707363807296094399L;

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

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy