package io.reactivex.internal.operators.completable;
import java.util.concurrent.atomic.*;
import org.reactivestreams.*;
import io.reactivex.*;
import io.reactivex.disposables.*;
import io.reactivex.internal.disposables.DisposableHelper;
import io.reactivex.internal.subscriptions.SubscriptionHelper;
import io.reactivex.internal.util.*;
import io.reactivex.plugins.RxJavaPlugins;
public final class CompletableMerge extends Completable {
final Publisher<? extends CompletableSource> source;
final int maxConcurrency;
final boolean delayErrors;
public CompletableMerge(Publisher<? extends CompletableSource> source, int maxConcurrency, boolean delayErrors) {
this.source = source;
this.maxConcurrency = maxConcurrency;
this.delayErrors = delayErrors;
}
@Override
public void subscribeActual(CompletableObserver observer) {
CompletableMergeSubscriber parent = new CompletableMergeSubscriber(observer, maxConcurrency, delayErrors);
source.subscribe(parent);
}
static final class CompletableMergeSubscriber
extends AtomicInteger
implements FlowableSubscriber<CompletableSource>, Disposable {
private static final long serialVersionUID = -2108443387387077490L;
final CompletableObserver downstream;
final int maxConcurrency;
final boolean delayErrors;
final AtomicThrowable error;
final CompositeDisposable set;
Subscription upstream;
CompletableMergeSubscriber(CompletableObserver actual, int maxConcurrency, boolean delayErrors) {
this.downstream = actual;
this.maxConcurrency = maxConcurrency;
this.delayErrors = delayErrors;
this.set = new CompositeDisposable();
this.error = new AtomicThrowable();
lazySet(1);
}
@Override
public void dispose() {
upstream.cancel();
set.dispose();
}
@Override
public boolean isDisposed() {
return set.isDisposed();
}
@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.validate(this.upstream, s)) {
this.upstream = s;
downstream.onSubscribe(this);
if (maxConcurrency == Integer.MAX_VALUE) {
s.request(Long.MAX_VALUE);
} else {
s.request(maxConcurrency);
}
}
}
@Override
public void onNext(CompletableSource t) {
getAndIncrement();
MergeInnerObserver inner = new MergeInnerObserver();
set.add(inner);
t.subscribe(inner);
}
@Override
public void onError(Throwable t) {
if (!delayErrors) {
set.dispose();
if (error.addThrowable(t)) {
if (getAndSet(0) > 0) {
downstream.onError(error.terminate());
}
} else {
RxJavaPlugins.onError(t);
}
} else {
if (error.addThrowable(t)) {
if (decrementAndGet() == 0) {
downstream.onError(error.terminate());
}
} else {
RxJavaPlugins.onError(t);
}
}
}
@Override
public void onComplete() {
if (decrementAndGet() == 0) {
Throwable ex = error.get();
if (ex != null) {
downstream.onError(error.terminate());
} else {
downstream.onComplete();
}
}
}
void innerError(MergeInnerObserver inner, Throwable t) {
set.delete(inner);
if (!delayErrors) {
upstream.cancel();
set.dispose();
if (error.addThrowable(t)) {
if (getAndSet(0) > 0) {
downstream.onError(error.terminate());
}
} else {
RxJavaPlugins.onError(t);
}
} else {
if (error.addThrowable(t)) {
if (decrementAndGet() == 0) {
downstream.onError(error.terminate());
} else {
if (maxConcurrency != Integer.MAX_VALUE) {
upstream.request(1);
}
}
} else {
RxJavaPlugins.onError(t);
}
}
}
void innerComplete(MergeInnerObserver inner) {
set.delete(inner);
if (decrementAndGet() == 0) {
Throwable ex = error.get();
if (ex != null) {
downstream.onError(ex);
} else {
downstream.onComplete();
}
} else {
if (maxConcurrency != Integer.MAX_VALUE) {
upstream.request(1);
}
}
}
final class MergeInnerObserver
extends AtomicReference<Disposable>
implements CompletableObserver, Disposable {
private static final long serialVersionUID = 251330541679988317L;
@Override
public void onSubscribe(Disposable d) {
DisposableHelper.setOnce(this, d);
}
@Override
public void onError(Throwable e) {
innerError(this, e);
}
@Override
public void onComplete() {
innerComplete(this);
}
@Override
public boolean isDisposed() {
return DisposableHelper.isDisposed(get());
}
@Override
public void dispose() {
DisposableHelper.dispose(this);
}
}
}
}