| 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.*; |
| 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.AtomicThrowable; |
| 25 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 26 |
|
| 27 |
/** |
| 28 |
* Maps the upstream items into {@link SingleSource}s and switches (subscribes) to the newer ones |
| 29 |
* while disposing the older ones and emits the latest success value if available, optionally delaying |
| 30 |
* errors from the main source or the inner sources. |
| 31 |
* <p>History: 2.1.11 - experimental |
| 32 |
* @param <T> the upstream value type |
| 33 |
* @param <R> the downstream value type |
| 34 |
* @since 2.2 |
| 35 |
*/ |
| 36 |
public final class ObservableSwitchMapSingle<T, R> extends Observable<R> { |
| 37 |
|
| 38 |
final Observable<T> source; |
| 39 |
|
| 40 |
final Function<? super T, ? extends SingleSource<? extends R>> mapper; |
| 41 |
|
| 42 |
final boolean delayErrors; |
| 43 |
|
| 44 |
public ObservableSwitchMapSingle(Observable<T> source, |
| 45 |
Function<? super T, ? extends SingleSource<? extends R>> mapper, |
| 46 |
boolean delayErrors) { |
| 47 |
this.source = source; |
| 48 |
this.mapper = mapper; |
| 49 |
this.delayErrors = delayErrors; |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
protected void subscribeActual(Observer<? super R> observer) { |
| 54 |
if (!ScalarXMapZHelper.tryAsSingle(source, mapper, observer)) { |
| 55 |
source.subscribe(new SwitchMapSingleMainObserver<>(observer, mapper, delayErrors)); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
static final class SwitchMapSingleMainObserver<T, R> extends AtomicInteger |
| 60 |
implements Observer<T>, Disposable { |
| 61 |
|
| 62 |
private static final long serialVersionUID = -5402190102429853762L; |
| 63 |
|
| 64 |
final Observer<? super R> downstream; |
| 65 |
|
| 66 |
final Function<? super T, ? extends SingleSource<? extends R>> mapper; |
| 67 |
|
| 68 |
final boolean delayErrors; |
| 69 |
|
| 70 |
final AtomicThrowable errors; |
| 71 |
|
| 72 |
final AtomicReference<SwitchMapSingleObserver<R>> inner; |
| 73 |
|
| 74 |
static final SwitchMapSingleObserver<Object> INNER_DISPOSED = |
| 75 |
new SwitchMapSingleObserver<>(null); |
| 76 |
|
| 77 |
Disposable upstream; |
| 78 |
|
| 79 |
volatile boolean done; |
| 80 |
|
| 81 |
volatile boolean cancelled; |
| 82 |
|
| 83 |
SwitchMapSingleMainObserver(Observer<? super R> downstream, |
| 84 |
Function<? super T, ? extends SingleSource<? extends R>> mapper, |
| 85 |
boolean delayErrors) { |
| 86 |
this.downstream = downstream; |
| 87 |
this.mapper = mapper; |
| 88 |
this.delayErrors = delayErrors; |
| 89 |
this.errors = new AtomicThrowable(); |
| 90 |
this.inner = new AtomicReference<>(); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public void onSubscribe(Disposable d) { |
| 95 |
if (DisposableHelper.validate(upstream, d)) { |
| 96 |
upstream = d; |
| 97 |
downstream.onSubscribe(this); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 103 |
public void onNext(T t) { |
| 104 |
SwitchMapSingleObserver<R> current = inner.get(); |
| 105 |
if (current != null) { |
| 106 |
current.dispose(); |
| 107 |
} |
| 108 |
|
| 109 |
SingleSource<? extends R> ss; |
| 110 |
|
| 111 |
try { |
| 112 |
ss = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null SingleSource"); |
| 113 |
} catch (Throwable ex) { |
| 114 |
Exceptions.throwIfFatal(ex); |
| 115 |
upstream.dispose(); |
| 116 |
inner.getAndSet((SwitchMapSingleObserver)INNER_DISPOSED); |
| 117 |
onError(ex); |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
SwitchMapSingleObserver<R> observer = new SwitchMapSingleObserver<>(this); |
| 122 |
|
| 123 |
for (;;) { |
| 124 |
current = inner.get(); |
| 125 |
if (current == INNER_DISPOSED) { |
| 126 |
break; |
| 127 |
} |
| 128 |
if (inner.compareAndSet(current, observer)) { |
| 129 |
ss.subscribe(observer); |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public void onError(Throwable t) { |
| 137 |
if (errors.tryAddThrowableOrReport(t)) { |
| 138 |
if (!delayErrors) { |
| 139 |
disposeInner(); |
| 140 |
} |
| 141 |
done = true; |
| 142 |
drain(); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public void onComplete() { |
| 148 |
done = true; |
| 149 |
drain(); |
| 150 |
} |
| 151 |
|
| 152 |
@SuppressWarnings({ "unchecked", "rawtypes" }) |
| 153 |
void disposeInner() { |
| 154 |
SwitchMapSingleObserver<R> current = inner.getAndSet((SwitchMapSingleObserver)INNER_DISPOSED); |
| 155 |
if (current != null && current != INNER_DISPOSED) { |
| 156 |
current.dispose(); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public void dispose() { |
| 162 |
cancelled = true; |
| 163 |
upstream.dispose(); |
| 164 |
disposeInner(); |
| 165 |
errors.tryTerminateAndReport(); |
| 166 |
} |
| 167 |
|
| 168 |
@Override |
| 169 |
public boolean isDisposed() { |
| 170 |
return cancelled; |
| 171 |
} |
| 172 |
|
| 173 |
void innerError(SwitchMapSingleObserver<R> sender, Throwable ex) { |
| 174 |
if (inner.compareAndSet(sender, null)) { |
| 175 |
if (errors.tryAddThrowableOrReport(ex)) { |
| 176 |
if (!delayErrors) { |
| 177 |
upstream.dispose(); |
| 178 |
disposeInner(); |
| 179 |
} |
| 180 |
drain(); |
| 181 |
} |
| 182 |
} else { |
| 183 |
RxJavaPlugins.onError(ex); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
void drain() { |
| 188 |
if (getAndIncrement() != 0) { |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
int missed = 1; |
| 193 |
Observer<? super R> downstream = this.downstream; |
| 194 |
AtomicThrowable errors = this.errors; |
| 195 |
AtomicReference<SwitchMapSingleObserver<R>> inner = this.inner; |
| 196 |
|
| 197 |
for (;;) { |
| 198 |
|
| 199 |
for (;;) { |
| 200 |
if (cancelled) { |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
if (errors.get() != null) { |
| 205 |
if (!delayErrors) { |
| 206 |
errors.tryTerminateConsumer(downstream); |
| 207 |
return; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
boolean d = done; |
| 212 |
SwitchMapSingleObserver<R> current = inner.get(); |
| 213 |
boolean empty = current == null; |
| 214 |
|
| 215 |
if (d && empty) { |
| 216 |
errors.tryTerminateConsumer(downstream); |
| 217 |
return; |
| 218 |
} |
| 219 |
|
| 220 |
if (empty || current.item == null) { |
| 221 |
break; |
| 222 |
} |
| 223 |
|
| 224 |
inner.compareAndSet(current, null); |
| 225 |
|
| 226 |
downstream.onNext(current.item); |
| 227 |
} |
| 228 |
|
| 229 |
missed = addAndGet(-missed); |
| 230 |
if (missed == 0) { |
| 231 |
break; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
static final class SwitchMapSingleObserver<R> |
| 237 |
extends AtomicReference<Disposable> implements SingleObserver<R> { |
| 238 |
|
| 239 |
private static final long serialVersionUID = 8042919737683345351L; |
| 240 |
|
| 241 |
final SwitchMapSingleMainObserver<?, R> parent; |
| 242 |
|
| 243 |
volatile R item; |
| 244 |
|
| 245 |
SwitchMapSingleObserver(SwitchMapSingleMainObserver<?, R> parent) { |
| 246 |
this.parent = parent; |
| 247 |
} |
| 248 |
|
| 249 |
@Override |
| 250 |
public void onSubscribe(Disposable d) { |
| 251 |
DisposableHelper.setOnce(this, d); |
| 252 |
} |
| 253 |
|
| 254 |
@Override |
| 255 |
public void onSuccess(R t) { |
| 256 |
item = t; |
| 257 |
parent.drain(); |
| 258 |
} |
| 259 |
|
| 260 |
@Override |
| 261 |
public void onError(Throwable e) { |
| 262 |
parent.innerError(this, e); |
| 263 |
} |
| 264 |
|
| 265 |
void dispose() { |
| 266 |
DisposableHelper.dispose(this); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
} |