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