| 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 ObservableConcatMapCompletable<T> extends Completable { |
| 35 |
|
| 36 |
final Observable<T> source; |
| 37 |
|
| 38 |
final Function<? super T, ? extends CompletableSource> mapper; |
| 39 |
|
| 40 |
final ErrorMode errorMode; |
| 41 |
|
| 42 |
final int prefetch; |
| 43 |
|
| 44 |
public ObservableConcatMapCompletable(Observable<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 |
if (!ScalarXMapZHelper.tryAsCompletable(source, mapper, observer)) { |
| 57 |
source.subscribe(new ConcatMapCompletableObserver<>(observer, mapper, errorMode, prefetch)); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
static final class ConcatMapCompletableObserver<T> |
| 62 |
extends ConcatMapXMainObserver<T> { |
| 63 |
|
| 64 |
private static final long serialVersionUID = 3610901111000061034L; |
| 65 |
|
| 66 |
final CompletableObserver downstream; |
| 67 |
|
| 68 |
final Function<? super T, ? extends CompletableSource> mapper; |
| 69 |
|
| 70 |
final ConcatMapInnerObserver inner; |
| 71 |
|
| 72 |
volatile boolean active; |
| 73 |
|
| 74 |
ConcatMapCompletableObserver(CompletableObserver downstream, |
| 75 |
Function<? super T, ? extends CompletableSource> mapper, |
| 76 |
ErrorMode errorMode, int prefetch) { |
| 77 |
super(prefetch, errorMode); |
| 78 |
this.downstream = downstream; |
| 79 |
this.mapper = mapper; |
| 80 |
this.inner = new ConcatMapInnerObserver(this); |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
void onSubscribeDownstream() { |
| 85 |
downstream.onSubscribe(this); |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
void disposeInner() { |
| 90 |
inner.dispose(); |
| 91 |
} |
| 92 |
|
| 93 |
void innerError(Throwable ex) { |
| 94 |
if (errors.tryAddThrowableOrReport(ex)) { |
| 95 |
if (errorMode != ErrorMode.END) { |
| 96 |
upstream.dispose(); |
| 97 |
} |
| 98 |
active = false; |
| 99 |
drain(); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
void innerComplete() { |
| 104 |
active = false; |
| 105 |
drain(); |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
void drain() { |
| 110 |
if (getAndIncrement() != 0) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
AtomicThrowable errors = this.errors; |
| 115 |
ErrorMode errorMode = this.errorMode; |
| 116 |
SimpleQueue<T> queue = this.queue; |
| 117 |
|
| 118 |
do { |
| 119 |
if (disposed) { |
| 120 |
queue.clear(); |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
if (errors.get() != null) { |
| 125 |
if (errorMode == ErrorMode.IMMEDIATE |
| 126 |
|| (errorMode == ErrorMode.BOUNDARY && !active)) { |
| 127 |
disposed = true; |
| 128 |
queue.clear(); |
| 129 |
errors.tryTerminateConsumer(downstream); |
| 130 |
return; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if (!active) { |
| 135 |
|
| 136 |
boolean d = done; |
| 137 |
boolean empty = true; |
| 138 |
CompletableSource cs = null; |
| 139 |
try { |
| 140 |
T v = queue.poll(); |
| 141 |
if (v != null) { |
| 142 |
cs = Objects.requireNonNull(mapper.apply(v), "The mapper returned a null CompletableSource"); |
| 143 |
empty = false; |
| 144 |
} |
| 145 |
} catch (Throwable ex) { |
| 146 |
Exceptions.throwIfFatal(ex); |
| 147 |
disposed = true; |
| 148 |
queue.clear(); |
| 149 |
upstream.dispose(); |
| 150 |
errors.tryAddThrowableOrReport(ex); |
| 151 |
errors.tryTerminateConsumer(downstream); |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
if (d && empty) { |
| 156 |
disposed = true; |
| 157 |
errors.tryTerminateConsumer(downstream); |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
if (!empty) { |
| 162 |
active = true; |
| 163 |
cs.subscribe(inner); |
| 164 |
} |
| 165 |
} |
| 166 |
} while (decrementAndGet() != 0); |
| 167 |
} |
| 168 |
|
| 169 |
static final class ConcatMapInnerObserver extends AtomicReference<Disposable> |
| 170 |
implements CompletableObserver { |
| 171 |
|
| 172 |
private static final long serialVersionUID = 5638352172918776687L; |
| 173 |
|
| 174 |
final ConcatMapCompletableObserver<?> parent; |
| 175 |
|
| 176 |
ConcatMapInnerObserver(ConcatMapCompletableObserver<?> parent) { |
| 177 |
this.parent = parent; |
| 178 |
} |
| 179 |
|
| 180 |
@Override |
| 181 |
public void onSubscribe(Disposable d) { |
| 182 |
DisposableHelper.replace(this, d); |
| 183 |
} |
| 184 |
|
| 185 |
@Override |
| 186 |
public void onError(Throwable e) { |
| 187 |
parent.innerError(e); |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public void onComplete() { |
| 192 |
parent.innerComplete(); |
| 193 |
} |
| 194 |
|
| 195 |
void dispose() { |
| 196 |
DisposableHelper.dispose(this); |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
} |