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

hu.akarnokd.rxjava3.operators.ObservableObserveOnDrop 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.core.Scheduler.Worker;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.internal.disposables.DisposableHelper;

/**
 * Schedules the event emission on a Scheduler and drops upstream values while
 * the {@code onNext} with the current item is executing on the given scheduler.
 * 
 * @param  the item type
 * @since 0.18.7
 */
final class ObservableObserveOnDrop extends Observable implements ObservableTransformer {

    final Observable source;

    final Scheduler scheduler;

    ObservableObserveOnDrop(Observable source, Scheduler scheduler) {
        this.source = source;
        this.scheduler = scheduler;
    }

    @Override
    public ObservableSource apply(Observable upstream) {
        return new ObservableObserveOnDrop(upstream, scheduler);
    }

    @Override
    protected void subscribeActual(Observer observer) {
        source.subscribe(new ObserveOnDropObserver(observer, scheduler.createWorker()));
    }

    static final class ObserveOnDropObserver extends AtomicInteger
    implements Observer, Disposable, Runnable {

        private static final long serialVersionUID = -11696478502477044L;

        final Observer downstream;

        final Worker worker;

        final AtomicReference item;

        static final Object WORKING = new Object();

        Disposable upstream;

        volatile boolean disposed;

        volatile boolean done;

        Throwable error;

        ObserveOnDropObserver(Observer downstream, Worker worker) {
            this.downstream = downstream;
            this.worker = worker;
            this.item = new AtomicReference();
        }

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

        @Override
        public void onNext(T t) {
            if (item.compareAndSet(null, t)) {
                drain();
            }
        }

        @Override
        public void onError(Throwable e) {
            error = e;
            done = true;
            drain();
        }

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

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

        @Override
        public void dispose() {
            disposed = true;
            upstream.dispose();
            worker.dispose();
            if (getAndIncrement() == 0) {
                item.lazySet(WORKING);
            }
        }

        void drain() {
            if (getAndIncrement() == 0) {
                worker.schedule(this);
            }
        }

        @Override
        @SuppressWarnings("unchecked")
        public void run() {
            int missed = 1;
            Observer a = downstream;

            for (;;) {

                for (;;) {
                    if (disposed) {
                        item.lazySet(WORKING);
                        return;
                    }

                    boolean d = done;
                    Object v = item.get();
                    boolean empty = v == null;

                    if (d && empty) {
                        Throwable ex = error;
                        if (ex == null) {
                            a.onComplete();
                        } else {
                            a.onError(ex);
                        }
                        worker.dispose();
                        return;
                    }

                    if (empty) {
                        break;
                    }

                    v = item.getAndSet(WORKING);

                    a.onNext((T)v);

                    item.set(null);
                }

                missed = addAndGet(-missed);
                if (missed == 0) {
                    break;
                }
            }
        }
    }
}