| 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 each upstream item into a {@link SingleSource}, subscribes to them one after the other terminates |
| 29 |
* and relays their success values, optionally delaying any errors till the main and inner sources |
| 30 |
* terminate. |
| 31 |
* <p>History: 2.1.11 - experimental |
| 32 |
* @param <T> the upstream element type |
| 33 |
* @param <R> the output element type |
| 34 |
* @since 2.2 |
| 35 |
*/ |
| 36 |
public final class ObservableConcatMapSingle<T, R> extends Observable<R> { |
| 37 |
|
| 38 |
final ObservableSource<T> source; |
| 39 |
|
| 40 |
final Function<? super T, ? extends SingleSource<? extends R>> mapper; |
| 41 |
|
| 42 |
final ErrorMode errorMode; |
| 43 |
|
| 44 |
final int prefetch; |
| 45 |
|
| 46 |
public ObservableConcatMapSingle(ObservableSource<T> source, |
| 47 |
Function<? super T, ? extends SingleSource<? extends R>> mapper, |
| 48 |
ErrorMode errorMode, int prefetch) { |
| 49 |
this.source = source; |
| 50 |
this.mapper = mapper; |
| 51 |
this.errorMode = errorMode; |
| 52 |
this.prefetch = prefetch; |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
protected void subscribeActual(Observer<? super R> observer) { |
| 57 |
if (!ScalarXMapZHelper.tryAsSingle(source, mapper, observer)) { |
| 58 |
source.subscribe(new ConcatMapSingleMainObserver<>(observer, mapper, prefetch, errorMode)); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
static final class ConcatMapSingleMainObserver<T, R> |
| 63 |
extends ConcatMapXMainObserver<T> { |
| 64 |
|
| 65 |
private static final long serialVersionUID = -9140123220065488293L; |
| 66 |
|
| 67 |
final Observer<? super R> downstream; |
| 68 |
|
| 69 |
final Function<? super T, ? extends SingleSource<? extends R>> mapper; |
| 70 |
|
| 71 |
final ConcatMapSingleObserver<R> inner; |
| 72 |
|
| 73 |
R item; |
| 74 |
|
| 75 |
volatile int state; |
| 76 |
|
| 77 |
/** No inner SingleSource is running. */ |
| 78 |
static final int STATE_INACTIVE = 0; |
| 79 |
/** An inner SingleSource is running but there are no results yet. */ |
| 80 |
static final int STATE_ACTIVE = 1; |
| 81 |
/** The inner SingleSource succeeded with a value in {@link #item}. */ |
| 82 |
static final int STATE_RESULT_VALUE = 2; |
| 83 |
|
| 84 |
ConcatMapSingleMainObserver(Observer<? super R> downstream, |
| 85 |
Function<? super T, ? extends SingleSource<? extends R>> mapper, |
| 86 |
int prefetch, ErrorMode errorMode) { |
| 87 |
super(prefetch, errorMode); |
| 88 |
this.downstream = downstream; |
| 89 |
this.mapper = mapper; |
| 90 |
this.inner = new ConcatMapSingleObserver<>(this); |
| 91 |
} |
| 92 |
|
| 93 |
void innerSuccess(R item) { |
| 94 |
this.item = item; |
| 95 |
this.state = STATE_RESULT_VALUE; |
| 96 |
drain(); |
| 97 |
} |
| 98 |
|
| 99 |
void innerError(Throwable ex) { |
| 100 |
if (errors.tryAddThrowableOrReport(ex)) { |
| 101 |
if (errorMode != ErrorMode.END) { |
| 102 |
upstream.dispose(); |
| 103 |
} |
| 104 |
this.state = STATE_INACTIVE; |
| 105 |
drain(); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
void disposeInner() { |
| 111 |
inner.dispose(); |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
void onSubscribeDownstream() { |
| 116 |
downstream.onSubscribe(this); |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
void clearValue() { |
| 121 |
item = null; |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
void drain() { |
| 126 |
if (getAndIncrement() != 0) { |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
int missed = 1; |
| 131 |
Observer<? super R> downstream = this.downstream; |
| 132 |
ErrorMode errorMode = this.errorMode; |
| 133 |
SimpleQueue<T> queue = this.queue; |
| 134 |
AtomicThrowable errors = this.errors; |
| 135 |
|
| 136 |
for (;;) { |
| 137 |
|
| 138 |
for (;;) { |
| 139 |
if (disposed) { |
| 140 |
queue.clear(); |
| 141 |
item = null; |
| 142 |
break; |
| 143 |
} |
| 144 |
|
| 145 |
int s = state; |
| 146 |
|
| 147 |
if (errors.get() != null) { |
| 148 |
if (errorMode == ErrorMode.IMMEDIATE |
| 149 |
|| (errorMode == ErrorMode.BOUNDARY && s == STATE_INACTIVE)) { |
| 150 |
queue.clear(); |
| 151 |
item = null; |
| 152 |
errors.tryTerminateConsumer(downstream); |
| 153 |
return; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if (s == STATE_INACTIVE) { |
| 158 |
boolean d = done; |
| 159 |
T v; |
| 160 |
|
| 161 |
try { |
| 162 |
v = queue.poll(); |
| 163 |
} catch (Throwable ex) { |
| 164 |
Exceptions.throwIfFatal(ex); |
| 165 |
disposed = true; |
| 166 |
upstream.dispose(); |
| 167 |
errors.tryAddThrowableOrReport(ex); |
| 168 |
errors.tryTerminateConsumer(downstream); |
| 169 |
return; |
| 170 |
} |
| 171 |
boolean empty = v == null; |
| 172 |
|
| 173 |
if (d && empty) { |
| 174 |
errors.tryTerminateConsumer(downstream); |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
if (empty) { |
| 179 |
break; |
| 180 |
} |
| 181 |
|
| 182 |
SingleSource<? extends R> ss; |
| 183 |
|
| 184 |
try { |
| 185 |
ss = Objects.requireNonNull(mapper.apply(v), "The mapper returned a null SingleSource"); |
| 186 |
} catch (Throwable ex) { |
| 187 |
Exceptions.throwIfFatal(ex); |
| 188 |
upstream.dispose(); |
| 189 |
queue.clear(); |
| 190 |
errors.tryAddThrowableOrReport(ex); |
| 191 |
errors.tryTerminateConsumer(downstream); |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
state = STATE_ACTIVE; |
| 196 |
ss.subscribe(inner); |
| 197 |
break; |
| 198 |
} else if (s == STATE_RESULT_VALUE) { |
| 199 |
R w = item; |
| 200 |
item = null; |
| 201 |
downstream.onNext(w); |
| 202 |
|
| 203 |
state = STATE_INACTIVE; |
| 204 |
} else { |
| 205 |
break; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
missed = addAndGet(-missed); |
| 210 |
if (missed == 0) { |
| 211 |
break; |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
static final class ConcatMapSingleObserver<R> |
| 217 |
extends AtomicReference<Disposable> |
| 218 |
implements SingleObserver<R> { |
| 219 |
|
| 220 |
private static final long serialVersionUID = -3051469169682093892L; |
| 221 |
|
| 222 |
final ConcatMapSingleMainObserver<?, R> parent; |
| 223 |
|
| 224 |
ConcatMapSingleObserver(ConcatMapSingleMainObserver<?, R> parent) { |
| 225 |
this.parent = parent; |
| 226 |
} |
| 227 |
|
| 228 |
@Override |
| 229 |
public void onSubscribe(Disposable d) { |
| 230 |
DisposableHelper.replace(this, d); |
| 231 |
} |
| 232 |
|
| 233 |
@Override |
| 234 |
public void onSuccess(R t) { |
| 235 |
parent.innerSuccess(t); |
| 236 |
} |
| 237 |
|
| 238 |
@Override |
| 239 |
public void onError(Throwable e) { |
| 240 |
parent.innerError(e); |
| 241 |
} |
| 242 |
|
| 243 |
void dispose() { |
| 244 |
DisposableHelper.dispose(this); |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
} |