| 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.observable; |
| 15 |
|
| 16 |
import java.util.ArrayDeque; |
| 17 |
import java.util.Objects; |
| 18 |
import java.util.concurrent.atomic.AtomicInteger; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 22 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 23 |
import io.reactivex.rxjava3.functions.Function; |
| 24 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 25 |
import io.reactivex.rxjava3.internal.observers.*; |
| 26 |
import io.reactivex.rxjava3.internal.util.*; |
| 27 |
import io.reactivex.rxjava3.operators.QueueDisposable; |
| 28 |
import io.reactivex.rxjava3.operators.SimpleQueue; |
| 29 |
import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue; |
| 30 |
|
| 31 |
public final class ObservableConcatMapEager<T, R> extends AbstractObservableWithUpstream<T, R> { |
| 32 |
|
| 33 |
final Function<? super T, ? extends ObservableSource<? extends R>> mapper; |
| 34 |
|
| 35 |
final ErrorMode errorMode; |
| 36 |
|
| 37 |
final int maxConcurrency; |
| 38 |
|
| 39 |
final int prefetch; |
| 40 |
|
| 41 |
public ObservableConcatMapEager(ObservableSource<T> source, |
| 42 |
Function<? super T, ? extends ObservableSource<? extends R>> mapper, |
| 43 |
ErrorMode errorMode, |
| 44 |
int maxConcurrency, int prefetch) { |
| 45 |
super(source); |
| 46 |
this.mapper = mapper; |
| 47 |
this.errorMode = errorMode; |
| 48 |
this.maxConcurrency = maxConcurrency; |
| 49 |
this.prefetch = prefetch; |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
protected void subscribeActual(Observer<? super R> observer) { |
| 54 |
source.subscribe(new ConcatMapEagerMainObserver<>(observer, mapper, maxConcurrency, prefetch, errorMode)); |
| 55 |
} |
| 56 |
|
| 57 |
static final class ConcatMapEagerMainObserver<T, R> |
| 58 |
extends AtomicInteger |
| 59 |
implements Observer<T>, Disposable, InnerQueuedObserverSupport<R> { |
| 60 |
|
| 61 |
private static final long serialVersionUID = 8080567949447303262L; |
| 62 |
|
| 63 |
final Observer<? super R> downstream; |
| 64 |
|
| 65 |
final Function<? super T, ? extends ObservableSource<? extends R>> mapper; |
| 66 |
|
| 67 |
final int maxConcurrency; |
| 68 |
|
| 69 |
final int prefetch; |
| 70 |
|
| 71 |
final ErrorMode errorMode; |
| 72 |
|
| 73 |
final AtomicThrowable errors; |
| 74 |
|
| 75 |
final ArrayDeque<InnerQueuedObserver<R>> observers; |
| 76 |
|
| 77 |
SimpleQueue<T> queue; |
| 78 |
|
| 79 |
Disposable upstream; |
| 80 |
|
| 81 |
volatile boolean done; |
| 82 |
|
| 83 |
int sourceMode; |
| 84 |
|
| 85 |
volatile boolean cancelled; |
| 86 |
|
| 87 |
InnerQueuedObserver<R> current; |
| 88 |
|
| 89 |
int activeCount; |
| 90 |
|
| 91 |
ConcatMapEagerMainObserver(Observer<? super R> actual, |
| 92 |
Function<? super T, ? extends ObservableSource<? extends R>> mapper, |
| 93 |
int maxConcurrency, int prefetch, ErrorMode errorMode) { |
| 94 |
this.downstream = actual; |
| 95 |
this.mapper = mapper; |
| 96 |
this.maxConcurrency = maxConcurrency; |
| 97 |
this.prefetch = prefetch; |
| 98 |
this.errorMode = errorMode; |
| 99 |
this.errors = new AtomicThrowable(); |
| 100 |
this.observers = new ArrayDeque<>(); |
| 101 |
} |
| 102 |
|
| 103 |
@SuppressWarnings("unchecked") |
| 104 |
@Override |
| 105 |
public void onSubscribe(Disposable d) { |
| 106 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 107 |
this.upstream = d; |
| 108 |
|
| 109 |
if (d instanceof QueueDisposable) { |
| 110 |
QueueDisposable<T> qd = (QueueDisposable<T>) d; |
| 111 |
|
| 112 |
int m = qd.requestFusion(QueueDisposable.ANY); |
| 113 |
if (m == QueueDisposable.SYNC) { |
| 114 |
sourceMode = m; |
| 115 |
queue = qd; |
| 116 |
done = true; |
| 117 |
|
| 118 |
downstream.onSubscribe(this); |
| 119 |
|
| 120 |
drain(); |
| 121 |
return; |
| 122 |
} |
| 123 |
if (m == QueueDisposable.ASYNC) { |
| 124 |
sourceMode = m; |
| 125 |
queue = qd; |
| 126 |
|
| 127 |
downstream.onSubscribe(this); |
| 128 |
|
| 129 |
return; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
queue = new SpscLinkedArrayQueue<>(prefetch); |
| 134 |
|
| 135 |
downstream.onSubscribe(this); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public void onNext(T value) { |
| 141 |
if (sourceMode == QueueDisposable.NONE) { |
| 142 |
queue.offer(value); |
| 143 |
} |
| 144 |
drain(); |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public void onError(Throwable e) { |
| 149 |
if (errors.tryAddThrowableOrReport(e)) { |
| 150 |
done = true; |
| 151 |
drain(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void onComplete() { |
| 157 |
done = true; |
| 158 |
drain(); |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public void dispose() { |
| 163 |
if (cancelled) { |
| 164 |
return; |
| 165 |
} |
| 166 |
cancelled = true; |
| 167 |
upstream.dispose(); |
| 168 |
errors.tryTerminateAndReport(); |
| 169 |
|
| 170 |
drainAndDispose(); |
| 171 |
} |
| 172 |
|
| 173 |
void drainAndDispose() { |
| 174 |
if (getAndIncrement() == 0) { |
| 175 |
do { |
| 176 |
queue.clear(); |
| 177 |
disposeAll(); |
| 178 |
} while (decrementAndGet() != 0); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
@Override |
| 183 |
public boolean isDisposed() { |
| 184 |
return cancelled; |
| 185 |
} |
| 186 |
|
| 187 |
void disposeAll() { |
| 188 |
InnerQueuedObserver<R> inner = current; |
| 189 |
|
| 190 |
if (inner != null) { |
| 191 |
inner.dispose(); |
| 192 |
} |
| 193 |
|
| 194 |
for (;;) { |
| 195 |
|
| 196 |
inner = observers.poll(); |
| 197 |
|
| 198 |
if (inner == null) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
inner.dispose(); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
@Override |
| 207 |
public void innerNext(InnerQueuedObserver<R> inner, R value) { |
| 208 |
inner.queue().offer(value); |
| 209 |
drain(); |
| 210 |
} |
| 211 |
|
| 212 |
@Override |
| 213 |
public void innerError(InnerQueuedObserver<R> inner, Throwable e) { |
| 214 |
if (errors.tryAddThrowableOrReport(e)) { |
| 215 |
if (errorMode == ErrorMode.IMMEDIATE) { |
| 216 |
upstream.dispose(); |
| 217 |
} |
| 218 |
inner.setDone(); |
| 219 |
drain(); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
@Override |
| 224 |
public void innerComplete(InnerQueuedObserver<R> inner) { |
| 225 |
inner.setDone(); |
| 226 |
drain(); |
| 227 |
} |
| 228 |
|
| 229 |
@Override |
| 230 |
public void drain() { |
| 231 |
if (getAndIncrement() != 0) { |
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
int missed = 1; |
| 236 |
|
| 237 |
SimpleQueue<T> q = queue; |
| 238 |
ArrayDeque<InnerQueuedObserver<R>> observers = this.observers; |
| 239 |
Observer<? super R> a = this.downstream; |
| 240 |
ErrorMode errorMode = this.errorMode; |
| 241 |
|
| 242 |
outer: |
| 243 |
for (;;) { |
| 244 |
|
| 245 |
int ac = activeCount; |
| 246 |
|
| 247 |
while (ac != maxConcurrency) { |
| 248 |
if (cancelled) { |
| 249 |
q.clear(); |
| 250 |
disposeAll(); |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
if (errorMode == ErrorMode.IMMEDIATE) { |
| 255 |
Throwable ex = errors.get(); |
| 256 |
if (ex != null) { |
| 257 |
q.clear(); |
| 258 |
disposeAll(); |
| 259 |
|
| 260 |
errors.tryTerminateConsumer(downstream); |
| 261 |
return; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
T v; |
| 266 |
ObservableSource<? extends R> source; |
| 267 |
|
| 268 |
try { |
| 269 |
v = q.poll(); |
| 270 |
|
| 271 |
if (v == null) { |
| 272 |
break; |
| 273 |
} |
| 274 |
|
| 275 |
source = Objects.requireNonNull(mapper.apply(v), "The mapper returned a null ObservableSource"); |
| 276 |
} catch (Throwable ex) { |
| 277 |
Exceptions.throwIfFatal(ex); |
| 278 |
upstream.dispose(); |
| 279 |
q.clear(); |
| 280 |
disposeAll(); |
| 281 |
errors.tryAddThrowableOrReport(ex); |
| 282 |
errors.tryTerminateConsumer(downstream); |
| 283 |
return; |
| 284 |
} |
| 285 |
|
| 286 |
InnerQueuedObserver<R> inner = new InnerQueuedObserver<>(this, prefetch); |
| 287 |
|
| 288 |
observers.offer(inner); |
| 289 |
|
| 290 |
source.subscribe(inner); |
| 291 |
|
| 292 |
ac++; |
| 293 |
} |
| 294 |
|
| 295 |
activeCount = ac; |
| 296 |
|
| 297 |
if (cancelled) { |
| 298 |
q.clear(); |
| 299 |
disposeAll(); |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
if (errorMode == ErrorMode.IMMEDIATE) { |
| 304 |
Throwable ex = errors.get(); |
| 305 |
if (ex != null) { |
| 306 |
q.clear(); |
| 307 |
disposeAll(); |
| 308 |
|
| 309 |
errors.tryTerminateConsumer(downstream); |
| 310 |
return; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
InnerQueuedObserver<R> active = current; |
| 315 |
|
| 316 |
if (active == null) { |
| 317 |
if (errorMode == ErrorMode.BOUNDARY) { |
| 318 |
Throwable ex = errors.get(); |
| 319 |
if (ex != null) { |
| 320 |
q.clear(); |
| 321 |
disposeAll(); |
| 322 |
|
| 323 |
errors.tryTerminateConsumer(a); |
| 324 |
return; |
| 325 |
} |
| 326 |
} |
| 327 |
boolean d = done; |
| 328 |
|
| 329 |
active = observers.poll(); |
| 330 |
|
| 331 |
boolean empty = active == null; |
| 332 |
|
| 333 |
if (d && empty) { |
| 334 |
Throwable ex = errors.get(); |
| 335 |
if (ex != null) { |
| 336 |
q.clear(); |
| 337 |
disposeAll(); |
| 338 |
|
| 339 |
errors.tryTerminateConsumer(a); |
| 340 |
} else { |
| 341 |
a.onComplete(); |
| 342 |
} |
| 343 |
return; |
| 344 |
} |
| 345 |
|
| 346 |
if (!empty) { |
| 347 |
current = active; |
| 348 |
} |
| 349 |
|
| 350 |
} |
| 351 |
|
| 352 |
if (active != null) { |
| 353 |
SimpleQueue<R> aq = active.queue(); |
| 354 |
|
| 355 |
for (;;) { |
| 356 |
if (cancelled) { |
| 357 |
q.clear(); |
| 358 |
disposeAll(); |
| 359 |
return; |
| 360 |
} |
| 361 |
|
| 362 |
boolean d = active.isDone(); |
| 363 |
|
| 364 |
if (errorMode == ErrorMode.IMMEDIATE) { |
| 365 |
Throwable ex = errors.get(); |
| 366 |
if (ex != null) { |
| 367 |
q.clear(); |
| 368 |
disposeAll(); |
| 369 |
|
| 370 |
errors.tryTerminateConsumer(a); |
| 371 |
return; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
R w; |
| 376 |
|
| 377 |
try { |
| 378 |
w = aq.poll(); |
| 379 |
} catch (Throwable ex) { |
| 380 |
Exceptions.throwIfFatal(ex); |
| 381 |
errors.tryAddThrowableOrReport(ex); |
| 382 |
|
| 383 |
current = null; |
| 384 |
activeCount--; |
| 385 |
continue outer; |
| 386 |
} |
| 387 |
|
| 388 |
boolean empty = w == null; |
| 389 |
|
| 390 |
if (d && empty) { |
| 391 |
current = null; |
| 392 |
activeCount--; |
| 393 |
continue outer; |
| 394 |
} |
| 395 |
|
| 396 |
if (empty) { |
| 397 |
break; |
| 398 |
} |
| 399 |
|
| 400 |
a.onNext(w); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
missed = addAndGet(-missed); |
| 405 |
if (missed == 0) { |
| 406 |
break; |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
} |