| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.operators.completable; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.*; |
| 17 |
|
| 18 |
import org.reactivestreams.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.disposables.*; |
| 22 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 23 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 24 |
import io.reactivex.rxjava3.internal.util.AtomicThrowable; |
| 25 |
|
| 26 |
public final class CompletableMerge extends Completable { |
| 27 |
final Publisher<? extends CompletableSource> source; |
| 28 |
final int maxConcurrency; |
| 29 |
final boolean delayErrors; |
| 30 |
|
| 31 |
public CompletableMerge(Publisher<? extends CompletableSource> source, int maxConcurrency, boolean delayErrors) { |
| 32 |
this.source = source; |
| 33 |
this.maxConcurrency = maxConcurrency; |
| 34 |
this.delayErrors = delayErrors; |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public void subscribeActual(CompletableObserver observer) { |
| 39 |
CompletableMergeSubscriber parent = new CompletableMergeSubscriber(observer, maxConcurrency, delayErrors); |
| 40 |
source.subscribe(parent); |
| 41 |
} |
| 42 |
|
| 43 |
static final class CompletableMergeSubscriber |
| 44 |
extends AtomicInteger |
| 45 |
implements FlowableSubscriber<CompletableSource>, Disposable { |
| 46 |
|
| 47 |
private static final long serialVersionUID = -2108443387387077490L; |
| 48 |
|
| 49 |
final CompletableObserver downstream; |
| 50 |
final int maxConcurrency; |
| 51 |
final boolean delayErrors; |
| 52 |
|
| 53 |
final AtomicThrowable errors; |
| 54 |
|
| 55 |
final CompositeDisposable set; |
| 56 |
|
| 57 |
Subscription upstream; |
| 58 |
|
| 59 |
CompletableMergeSubscriber(CompletableObserver actual, int maxConcurrency, boolean delayErrors) { |
| 60 |
this.downstream = actual; |
| 61 |
this.maxConcurrency = maxConcurrency; |
| 62 |
this.delayErrors = delayErrors; |
| 63 |
this.set = new CompositeDisposable(); |
| 64 |
this.errors = new AtomicThrowable(); |
| 65 |
lazySet(1); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void dispose() { |
| 70 |
upstream.cancel(); |
| 71 |
set.dispose(); |
| 72 |
errors.tryTerminateAndReport(); |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public boolean isDisposed() { |
| 77 |
return set.isDisposed(); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public void onSubscribe(Subscription s) { |
| 82 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 83 |
this.upstream = s; |
| 84 |
downstream.onSubscribe(this); |
| 85 |
if (maxConcurrency == Integer.MAX_VALUE) { |
| 86 |
s.request(Long.MAX_VALUE); |
| 87 |
} else { |
| 88 |
s.request(maxConcurrency); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public void onNext(CompletableSource t) { |
| 95 |
getAndIncrement(); |
| 96 |
|
| 97 |
MergeInnerObserver inner = new MergeInnerObserver(); |
| 98 |
set.add(inner); |
| 99 |
t.subscribe(inner); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void onError(Throwable t) { |
| 104 |
if (!delayErrors) { |
| 105 |
set.dispose(); |
| 106 |
|
| 107 |
if (errors.tryAddThrowableOrReport(t)) { |
| 108 |
if (getAndSet(0) > 0) { |
| 109 |
errors.tryTerminateConsumer(downstream); |
| 110 |
} |
| 111 |
} |
| 112 |
} else { |
| 113 |
if (errors.tryAddThrowableOrReport(t)) { |
| 114 |
if (decrementAndGet() == 0) { |
| 115 |
errors.tryTerminateConsumer(downstream); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public void onComplete() { |
| 123 |
if (decrementAndGet() == 0) { |
| 124 |
errors.tryTerminateConsumer(downstream); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
void innerError(MergeInnerObserver inner, Throwable t) { |
| 129 |
set.delete(inner); |
| 130 |
if (!delayErrors) { |
| 131 |
upstream.cancel(); |
| 132 |
set.dispose(); |
| 133 |
|
| 134 |
if (errors.tryAddThrowableOrReport(t)) { |
| 135 |
if (getAndSet(0) > 0) { |
| 136 |
errors.tryTerminateConsumer(downstream); |
| 137 |
} |
| 138 |
} |
| 139 |
} else { |
| 140 |
if (errors.tryAddThrowableOrReport(t)) { |
| 141 |
if (decrementAndGet() == 0) { |
| 142 |
errors.tryTerminateConsumer(downstream); |
| 143 |
} else { |
| 144 |
if (maxConcurrency != Integer.MAX_VALUE) { |
| 145 |
upstream.request(1); |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
void innerComplete(MergeInnerObserver inner) { |
| 153 |
set.delete(inner); |
| 154 |
if (decrementAndGet() == 0) { |
| 155 |
errors.tryTerminateConsumer(downstream); |
| 156 |
} else { |
| 157 |
if (maxConcurrency != Integer.MAX_VALUE) { |
| 158 |
upstream.request(1); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
final class MergeInnerObserver |
| 164 |
extends AtomicReference<Disposable> |
| 165 |
implements CompletableObserver, Disposable { |
| 166 |
private static final long serialVersionUID = 251330541679988317L; |
| 167 |
|
| 168 |
@Override |
| 169 |
public void onSubscribe(Disposable d) { |
| 170 |
DisposableHelper.setOnce(this, d); |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public void onError(Throwable e) { |
| 175 |
innerError(this, e); |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public void onComplete() { |
| 180 |
innerComplete(this); |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public boolean isDisposed() { |
| 185 |
return DisposableHelper.isDisposed(get()); |
| 186 |
} |
| 187 |
|
| 188 |
@Override |
| 189 |
public void dispose() { |
| 190 |
DisposableHelper.dispose(this); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
} |