| 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.flowable; |
| 15 |
|
| 16 |
import java.util.Objects; |
| 17 |
import java.util.concurrent.atomic.*; |
| 18 |
|
| 19 |
import org.reactivestreams.Subscription; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.disposables.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 24 |
import io.reactivex.rxjava3.functions.Function; |
| 25 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 26 |
import io.reactivex.rxjava3.internal.fuseable.FuseToFlowable; |
| 27 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 28 |
import io.reactivex.rxjava3.internal.util.AtomicThrowable; |
| 29 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 30 |
|
| 31 |
/** |
| 32 |
* Maps a sequence of values into CompletableSources and awaits their termination. |
| 33 |
* @param <T> the value type |
| 34 |
*/ |
| 35 |
public final class FlowableFlatMapCompletableCompletable<T> extends Completable implements FuseToFlowable<T> { |
| 36 |
|
| 37 |
final Flowable<T> source; |
| 38 |
|
| 39 |
final Function<? super T, ? extends CompletableSource> mapper; |
| 40 |
|
| 41 |
final int maxConcurrency; |
| 42 |
|
| 43 |
final boolean delayErrors; |
| 44 |
|
| 45 |
public FlowableFlatMapCompletableCompletable(Flowable<T> source, |
| 46 |
Function<? super T, ? extends CompletableSource> mapper, boolean delayErrors, |
| 47 |
int maxConcurrency) { |
| 48 |
this.source = source; |
| 49 |
this.mapper = mapper; |
| 50 |
this.delayErrors = delayErrors; |
| 51 |
this.maxConcurrency = maxConcurrency; |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
protected void subscribeActual(CompletableObserver observer) { |
| 56 |
source.subscribe(new FlatMapCompletableMainSubscriber<>(observer, mapper, delayErrors, maxConcurrency)); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public Flowable<T> fuseToFlowable() { |
| 61 |
return RxJavaPlugins.onAssembly(new FlowableFlatMapCompletable<>(source, mapper, delayErrors, maxConcurrency)); |
| 62 |
} |
| 63 |
|
| 64 |
static final class FlatMapCompletableMainSubscriber<T> extends AtomicInteger |
| 65 |
implements FlowableSubscriber<T>, Disposable { |
| 66 |
private static final long serialVersionUID = 8443155186132538303L; |
| 67 |
|
| 68 |
final CompletableObserver downstream; |
| 69 |
|
| 70 |
final AtomicThrowable errors; |
| 71 |
|
| 72 |
final Function<? super T, ? extends CompletableSource> mapper; |
| 73 |
|
| 74 |
final boolean delayErrors; |
| 75 |
|
| 76 |
final CompositeDisposable set; |
| 77 |
|
| 78 |
final int maxConcurrency; |
| 79 |
|
| 80 |
Subscription upstream; |
| 81 |
|
| 82 |
volatile boolean disposed; |
| 83 |
|
| 84 |
FlatMapCompletableMainSubscriber(CompletableObserver observer, |
| 85 |
Function<? super T, ? extends CompletableSource> mapper, boolean delayErrors, |
| 86 |
int maxConcurrency) { |
| 87 |
this.downstream = observer; |
| 88 |
this.mapper = mapper; |
| 89 |
this.delayErrors = delayErrors; |
| 90 |
this.errors = new AtomicThrowable(); |
| 91 |
this.set = new CompositeDisposable(); |
| 92 |
this.maxConcurrency = maxConcurrency; |
| 93 |
this.lazySet(1); |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public void onSubscribe(Subscription s) { |
| 98 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 99 |
this.upstream = s; |
| 100 |
|
| 101 |
downstream.onSubscribe(this); |
| 102 |
|
| 103 |
int m = maxConcurrency; |
| 104 |
if (m == Integer.MAX_VALUE) { |
| 105 |
s.request(Long.MAX_VALUE); |
| 106 |
} else { |
| 107 |
s.request(m); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public void onNext(T value) { |
| 114 |
CompletableSource cs; |
| 115 |
|
| 116 |
try { |
| 117 |
cs = Objects.requireNonNull(mapper.apply(value), "The mapper returned a null CompletableSource"); |
| 118 |
} catch (Throwable ex) { |
| 119 |
Exceptions.throwIfFatal(ex); |
| 120 |
upstream.cancel(); |
| 121 |
onError(ex); |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
getAndIncrement(); |
| 126 |
|
| 127 |
InnerObserver inner = new InnerObserver(); |
| 128 |
|
| 129 |
if (!disposed && set.add(inner)) { |
| 130 |
cs.subscribe(inner); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public void onError(Throwable e) { |
| 136 |
if (errors.tryAddThrowableOrReport(e)) { |
| 137 |
if (delayErrors) { |
| 138 |
if (decrementAndGet() == 0) { |
| 139 |
errors.tryTerminateConsumer(downstream); |
| 140 |
} else { |
| 141 |
if (maxConcurrency != Integer.MAX_VALUE) { |
| 142 |
upstream.request(1); |
| 143 |
} |
| 144 |
} |
| 145 |
} else { |
| 146 |
disposed = true; |
| 147 |
upstream.cancel(); |
| 148 |
set.dispose(); |
| 149 |
errors.tryTerminateConsumer(downstream); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public void onComplete() { |
| 156 |
if (decrementAndGet() == 0) { |
| 157 |
errors.tryTerminateConsumer(downstream); |
| 158 |
} else { |
| 159 |
if (maxConcurrency != Integer.MAX_VALUE) { |
| 160 |
upstream.request(1); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
@Override |
| 166 |
public void dispose() { |
| 167 |
disposed = true; |
| 168 |
upstream.cancel(); |
| 169 |
set.dispose(); |
| 170 |
errors.tryTerminateAndReport(); |
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public boolean isDisposed() { |
| 175 |
return set.isDisposed(); |
| 176 |
} |
| 177 |
|
| 178 |
void innerComplete(InnerObserver inner) { |
| 179 |
set.delete(inner); |
| 180 |
onComplete(); |
| 181 |
} |
| 182 |
|
| 183 |
void innerError(InnerObserver inner, Throwable e) { |
| 184 |
set.delete(inner); |
| 185 |
onError(e); |
| 186 |
} |
| 187 |
|
| 188 |
final class InnerObserver extends AtomicReference<Disposable> implements CompletableObserver, Disposable { |
| 189 |
private static final long serialVersionUID = 8606673141535671828L; |
| 190 |
|
| 191 |
@Override |
| 192 |
public void onSubscribe(Disposable d) { |
| 193 |
DisposableHelper.setOnce(this, d); |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
public void onComplete() { |
| 198 |
innerComplete(this); |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public void onError(Throwable e) { |
| 203 |
innerError(this, e); |
| 204 |
} |
| 205 |
|
| 206 |
@Override |
| 207 |
public void dispose() { |
| 208 |
DisposableHelper.dispose(this); |
| 209 |
} |
| 210 |
|
| 211 |
@Override |
| 212 |
public boolean isDisposed() { |
| 213 |
return DisposableHelper.isDisposed(get()); |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
} |