| 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.mixed; |
| 15 |
|
| 16 |
import java.util.Objects; |
| 17 |
import java.util.concurrent.atomic.AtomicReference; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.core.*; |
| 20 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 21 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 22 |
import io.reactivex.rxjava3.functions.Function; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 24 |
import io.reactivex.rxjava3.internal.util.*; |
| 25 |
import io.reactivex.rxjava3.operators.SimpleQueue; |
| 26 |
|
| 27 |
/** |
| 28 |
* Maps the upstream items into {@link CompletableSource}s and subscribes to them one after the |
| 29 |
* other completes or terminates (in error-delaying mode). |
| 30 |
* <p>History: 2.1.11 - experimental |
| 31 |
* @param <T> the upstream value type |
| 32 |
* @since 2.2 |
| 33 |
*/ |
| 34 |
public final class FlowableConcatMapCompletable<T> extends Completable { |
| 35 |
|
| 36 |
final Flowable<T> source; |
| 37 |
|
| 38 |
final Function<? super T, ? extends CompletableSource> mapper; |
| 39 |
|
| 40 |
final ErrorMode errorMode; |
| 41 |
|
| 42 |
final int prefetch; |
| 43 |
|
| 44 |
public FlowableConcatMapCompletable(Flowable<T> source, |
| 45 |
Function<? super T, ? extends CompletableSource> mapper, |
| 46 |
ErrorMode errorMode, |
| 47 |
int prefetch) { |
| 48 |
this.source = source; |
| 49 |
this.mapper = mapper; |
| 50 |
this.errorMode = errorMode; |
| 51 |
this.prefetch = prefetch; |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
protected void subscribeActual(CompletableObserver observer) { |
| 56 |
source.subscribe(new ConcatMapCompletableObserver<>(observer, mapper, errorMode, prefetch)); |
| 57 |
} |
| 58 |
|
| 59 |
static final class ConcatMapCompletableObserver<T> |
| 60 |
extends ConcatMapXMainSubscriber<T> |
| 61 |
implements Disposable { |
| 62 |
|
| 63 |
private static final long serialVersionUID = 3610901111000061034L; |
| 64 |
|
| 65 |
final CompletableObserver downstream; |
| 66 |
|
| 67 |
final Function<? super T, ? extends CompletableSource> mapper; |
| 68 |
|
| 69 |
final ConcatMapInnerObserver inner; |
| 70 |
|
| 71 |
volatile boolean active; |
| 72 |
|
| 73 |
int consumed; |
| 74 |
|
| 75 |
ConcatMapCompletableObserver(CompletableObserver downstream, |
| 76 |
Function<? super T, ? extends CompletableSource> mapper, |
| 77 |
ErrorMode errorMode, int prefetch) { |
| 78 |
super(prefetch, errorMode); |
| 79 |
this.downstream = downstream; |
| 80 |
this.mapper = mapper; |
| 81 |
this.inner = new ConcatMapInnerObserver(this); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
void onSubscribeDownstream() { |
| 86 |
downstream.onSubscribe(this); |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
void disposeInner() { |
| 91 |
inner.dispose(); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void dispose() { |
| 96 |
stop(); |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public boolean isDisposed() { |
| 101 |
return cancelled; |
| 102 |
} |
| 103 |
|
| 104 |
void innerError(Throwable ex) { |
| 105 |
if (errors.tryAddThrowableOrReport(ex)) { |
| 106 |
if (errorMode == ErrorMode.IMMEDIATE) { |
| 107 |
upstream.cancel(); |
| 108 |
errors.tryTerminateConsumer(downstream); |
| 109 |
if (getAndIncrement() == 0) { |
| 110 |
queue.clear(); |
| 111 |
} |
| 112 |
} else { |
| 113 |
active = false; |
| 114 |
drain(); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
void innerComplete() { |
| 120 |
active = false; |
| 121 |
drain(); |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
void drain() { |
| 126 |
if (getAndIncrement() != 0) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
ErrorMode errorMode = this.errorMode; |
| 131 |
SimpleQueue<T> queue = this.queue; |
| 132 |
AtomicThrowable errors = this.errors; |
| 133 |
boolean syncFused = this.syncFused; |
| 134 |
|
| 135 |
do { |
| 136 |
if (cancelled) { |
| 137 |
queue.clear(); |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
if (errors.get() != null) { |
| 142 |
if (errorMode == ErrorMode.IMMEDIATE |
| 143 |
|| (errorMode == ErrorMode.BOUNDARY && !active)) { |
| 144 |
queue.clear(); |
| 145 |
errors.tryTerminateConsumer(downstream); |
| 146 |
return; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
if (!active) { |
| 151 |
|
| 152 |
boolean d = done; |
| 153 |
T v; |
| 154 |
try { |
| 155 |
v = queue.poll(); |
| 156 |
} catch (Throwable ex) { |
| 157 |
Exceptions.throwIfFatal(ex); |
| 158 |
upstream.cancel(); |
| 159 |
errors.tryAddThrowableOrReport(ex); |
| 160 |
errors.tryTerminateConsumer(downstream); |
| 161 |
return; |
| 162 |
} |
| 163 |
boolean empty = v == null; |
| 164 |
|
| 165 |
if (d && empty) { |
| 166 |
errors.tryTerminateConsumer(downstream); |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
if (!empty) { |
| 171 |
|
| 172 |
int limit = prefetch - (prefetch >> 1); |
| 173 |
|
| 174 |
if (!syncFused) { |
| 175 |
int c = consumed + 1; |
| 176 |
if (c == limit) { |
| 177 |
consumed = 0; |
| 178 |
upstream.request(limit); |
| 179 |
} else { |
| 180 |
consumed = c; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
CompletableSource cs; |
| 185 |
|
| 186 |
try { |
| 187 |
cs = Objects.requireNonNull(mapper.apply(v), "The mapper returned a null CompletableSource"); |
| 188 |
} catch (Throwable ex) { |
| 189 |
Exceptions.throwIfFatal(ex); |
| 190 |
queue.clear(); |
| 191 |
upstream.cancel(); |
| 192 |
errors.tryAddThrowableOrReport(ex); |
| 193 |
errors.tryTerminateConsumer(downstream); |
| 194 |
return; |
| 195 |
} |
| 196 |
active = true; |
| 197 |
cs.subscribe(inner); |
| 198 |
} |
| 199 |
} |
| 200 |
} while (decrementAndGet() != 0); |
| 201 |
} |
| 202 |
|
| 203 |
static final class ConcatMapInnerObserver extends AtomicReference<Disposable> |
| 204 |
implements CompletableObserver { |
| 205 |
|
| 206 |
private static final long serialVersionUID = 5638352172918776687L; |
| 207 |
|
| 208 |
final ConcatMapCompletableObserver<?> parent; |
| 209 |
|
| 210 |
ConcatMapInnerObserver(ConcatMapCompletableObserver<?> parent) { |
| 211 |
this.parent = parent; |
| 212 |
} |
| 213 |
|
| 214 |
@Override |
| 215 |
public void onSubscribe(Disposable d) { |
| 216 |
DisposableHelper.replace(this, d); |
| 217 |
} |
| 218 |
|
| 219 |
@Override |
| 220 |
public void onError(Throwable e) { |
| 221 |
parent.innerError(e); |
| 222 |
} |
| 223 |
|
| 224 |
@Override |
| 225 |
public void onComplete() { |
| 226 |
parent.innerComplete(); |
| 227 |
} |
| 228 |
|
| 229 |
void dispose() { |
| 230 |
DisposableHelper.dispose(this); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
} |