| 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.flowable; |
| 15 |
|
| 16 |
import org.reactivestreams.Subscriber; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.annotations.Nullable; |
| 19 |
import io.reactivex.rxjava3.core.Flowable; |
| 20 |
import io.reactivex.rxjava3.exceptions.*; |
| 21 |
import io.reactivex.rxjava3.functions.*; |
| 22 |
import io.reactivex.rxjava3.internal.subscribers.*; |
| 23 |
import io.reactivex.rxjava3.internal.util.ExceptionHelper; |
| 24 |
import io.reactivex.rxjava3.operators.ConditionalSubscriber; |
| 25 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 26 |
|
| 27 |
public final class FlowableDoOnEach<T> extends AbstractFlowableWithUpstream<T, T> { |
| 28 |
final Consumer<? super T> onNext; |
| 29 |
final Consumer<? super Throwable> onError; |
| 30 |
final Action onComplete; |
| 31 |
final Action onAfterTerminate; |
| 32 |
|
| 33 |
public FlowableDoOnEach(Flowable<T> source, Consumer<? super T> onNext, |
| 34 |
Consumer<? super Throwable> onError, |
| 35 |
Action onComplete, |
| 36 |
Action onAfterTerminate) { |
| 37 |
super(source); |
| 38 |
this.onNext = onNext; |
| 39 |
this.onError = onError; |
| 40 |
this.onComplete = onComplete; |
| 41 |
this.onAfterTerminate = onAfterTerminate; |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 46 |
if (s instanceof ConditionalSubscriber) { |
| 47 |
source.subscribe(new DoOnEachConditionalSubscriber<>( |
| 48 |
(ConditionalSubscriber<? super T>) s, onNext, onError, onComplete, onAfterTerminate)); |
| 49 |
} else { |
| 50 |
source.subscribe(new DoOnEachSubscriber<>( |
| 51 |
s, onNext, onError, onComplete, onAfterTerminate)); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
static final class DoOnEachSubscriber<T> extends BasicFuseableSubscriber<T, T> { |
| 56 |
final Consumer<? super T> onNext; |
| 57 |
final Consumer<? super Throwable> onError; |
| 58 |
final Action onComplete; |
| 59 |
final Action onAfterTerminate; |
| 60 |
|
| 61 |
DoOnEachSubscriber( |
| 62 |
Subscriber<? super T> actual, |
| 63 |
Consumer<? super T> onNext, |
| 64 |
Consumer<? super Throwable> onError, |
| 65 |
Action onComplete, |
| 66 |
Action onAfterTerminate) { |
| 67 |
super(actual); |
| 68 |
this.onNext = onNext; |
| 69 |
this.onError = onError; |
| 70 |
this.onComplete = onComplete; |
| 71 |
this.onAfterTerminate = onAfterTerminate; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void onNext(T t) { |
| 76 |
if (done) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
if (sourceMode != NONE) { |
| 81 |
downstream.onNext(null); |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
try { |
| 86 |
onNext.accept(t); |
| 87 |
} catch (Throwable e) { |
| 88 |
fail(e); |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
downstream.onNext(t); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public void onError(Throwable t) { |
| 97 |
if (done) { |
| 98 |
RxJavaPlugins.onError(t); |
| 99 |
return; |
| 100 |
} |
| 101 |
done = true; |
| 102 |
boolean relay = true; |
| 103 |
try { |
| 104 |
onError.accept(t); |
| 105 |
} catch (Throwable e) { |
| 106 |
Exceptions.throwIfFatal(e); |
| 107 |
downstream.onError(new CompositeException(t, e)); |
| 108 |
relay = false; |
| 109 |
} |
| 110 |
if (relay) { |
| 111 |
downstream.onError(t); |
| 112 |
} |
| 113 |
|
| 114 |
try { |
| 115 |
onAfterTerminate.run(); |
| 116 |
} catch (Throwable e) { |
| 117 |
Exceptions.throwIfFatal(e); |
| 118 |
RxJavaPlugins.onError(e); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public void onComplete() { |
| 124 |
if (done) { |
| 125 |
return; |
| 126 |
} |
| 127 |
try { |
| 128 |
onComplete.run(); |
| 129 |
} catch (Throwable e) { |
| 130 |
fail(e); |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
done = true; |
| 135 |
downstream.onComplete(); |
| 136 |
|
| 137 |
try { |
| 138 |
onAfterTerminate.run(); |
| 139 |
} catch (Throwable e) { |
| 140 |
Exceptions.throwIfFatal(e); |
| 141 |
RxJavaPlugins.onError(e); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public int requestFusion(int mode) { |
| 147 |
return transitiveBoundaryFusion(mode); |
| 148 |
} |
| 149 |
|
| 150 |
@Nullable |
| 151 |
@Override |
| 152 |
public T poll() throws Throwable { |
| 153 |
T v; |
| 154 |
|
| 155 |
try { |
| 156 |
v = qs.poll(); |
| 157 |
} catch (Throwable ex) { |
| 158 |
Exceptions.throwIfFatal(ex); |
| 159 |
try { |
| 160 |
onError.accept(ex); |
| 161 |
} catch (Throwable exc) { |
| 162 |
Exceptions.throwIfFatal(exc); |
| 163 |
throw new CompositeException(ex, exc); |
| 164 |
} |
| 165 |
throw ExceptionHelper.<Exception>throwIfThrowable(ex); |
| 166 |
} |
| 167 |
|
| 168 |
if (v != null) { |
| 169 |
try { |
| 170 |
try { |
| 171 |
onNext.accept(v); |
| 172 |
} catch (Throwable ex) { |
| 173 |
Exceptions.throwIfFatal(ex); |
| 174 |
try { |
| 175 |
onError.accept(ex); |
| 176 |
} catch (Throwable exc) { |
| 177 |
Exceptions.throwIfFatal(exc); |
| 178 |
throw new CompositeException(ex, exc); |
| 179 |
} |
| 180 |
throw ExceptionHelper.<Exception>throwIfThrowable(ex); |
| 181 |
} |
| 182 |
} finally { |
| 183 |
onAfterTerminate.run(); |
| 184 |
} |
| 185 |
} else { |
| 186 |
if (sourceMode == SYNC) { |
| 187 |
onComplete.run(); |
| 188 |
|
| 189 |
onAfterTerminate.run(); |
| 190 |
} |
| 191 |
} |
| 192 |
return v; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
static final class DoOnEachConditionalSubscriber<T> extends BasicFuseableConditionalSubscriber<T, T> { |
| 197 |
final Consumer<? super T> onNext; |
| 198 |
final Consumer<? super Throwable> onError; |
| 199 |
final Action onComplete; |
| 200 |
final Action onAfterTerminate; |
| 201 |
|
| 202 |
DoOnEachConditionalSubscriber( |
| 203 |
ConditionalSubscriber<? super T> actual, |
| 204 |
Consumer<? super T> onNext, |
| 205 |
Consumer<? super Throwable> onError, |
| 206 |
Action onComplete, |
| 207 |
Action onAfterTerminate) { |
| 208 |
super(actual); |
| 209 |
this.onNext = onNext; |
| 210 |
this.onError = onError; |
| 211 |
this.onComplete = onComplete; |
| 212 |
this.onAfterTerminate = onAfterTerminate; |
| 213 |
} |
| 214 |
|
| 215 |
@Override |
| 216 |
public void onNext(T t) { |
| 217 |
if (done) { |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
if (sourceMode != NONE) { |
| 222 |
downstream.onNext(null); |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
try { |
| 227 |
onNext.accept(t); |
| 228 |
} catch (Throwable e) { |
| 229 |
fail(e); |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
downstream.onNext(t); |
| 234 |
} |
| 235 |
|
| 236 |
@Override |
| 237 |
public boolean tryOnNext(T t) { |
| 238 |
if (done) { |
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
try { |
| 243 |
onNext.accept(t); |
| 244 |
} catch (Throwable e) { |
| 245 |
fail(e); |
| 246 |
return false; |
| 247 |
} |
| 248 |
|
| 249 |
return downstream.tryOnNext(t); |
| 250 |
} |
| 251 |
|
| 252 |
@Override |
| 253 |
public void onError(Throwable t) { |
| 254 |
if (done) { |
| 255 |
RxJavaPlugins.onError(t); |
| 256 |
return; |
| 257 |
} |
| 258 |
done = true; |
| 259 |
boolean relay = true; |
| 260 |
try { |
| 261 |
onError.accept(t); |
| 262 |
} catch (Throwable e) { |
| 263 |
Exceptions.throwIfFatal(e); |
| 264 |
downstream.onError(new CompositeException(t, e)); |
| 265 |
relay = false; |
| 266 |
} |
| 267 |
if (relay) { |
| 268 |
downstream.onError(t); |
| 269 |
} |
| 270 |
|
| 271 |
try { |
| 272 |
onAfterTerminate.run(); |
| 273 |
} catch (Throwable e) { |
| 274 |
Exceptions.throwIfFatal(e); |
| 275 |
RxJavaPlugins.onError(e); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
@Override |
| 280 |
public void onComplete() { |
| 281 |
if (done) { |
| 282 |
return; |
| 283 |
} |
| 284 |
try { |
| 285 |
onComplete.run(); |
| 286 |
} catch (Throwable e) { |
| 287 |
fail(e); |
| 288 |
return; |
| 289 |
} |
| 290 |
|
| 291 |
done = true; |
| 292 |
downstream.onComplete(); |
| 293 |
|
| 294 |
try { |
| 295 |
onAfterTerminate.run(); |
| 296 |
} catch (Throwable e) { |
| 297 |
Exceptions.throwIfFatal(e); |
| 298 |
RxJavaPlugins.onError(e); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
@Override |
| 303 |
public int requestFusion(int mode) { |
| 304 |
return transitiveBoundaryFusion(mode); |
| 305 |
} |
| 306 |
|
| 307 |
@Nullable |
| 308 |
@Override |
| 309 |
public T poll() throws Throwable { |
| 310 |
T v; |
| 311 |
|
| 312 |
try { |
| 313 |
v = qs.poll(); |
| 314 |
} catch (Throwable ex) { |
| 315 |
Exceptions.throwIfFatal(ex); |
| 316 |
try { |
| 317 |
onError.accept(ex); |
| 318 |
} catch (Throwable exc) { |
| 319 |
Exceptions.throwIfFatal(exc); |
| 320 |
throw new CompositeException(ex, exc); |
| 321 |
} |
| 322 |
throw ExceptionHelper.<Exception>throwIfThrowable(ex); |
| 323 |
} |
| 324 |
|
| 325 |
if (v != null) { |
| 326 |
try { |
| 327 |
try { |
| 328 |
onNext.accept(v); |
| 329 |
} catch (Throwable ex) { |
| 330 |
Exceptions.throwIfFatal(ex); |
| 331 |
try { |
| 332 |
onError.accept(ex); |
| 333 |
} catch (Throwable exc) { |
| 334 |
Exceptions.throwIfFatal(exc); |
| 335 |
throw new CompositeException(ex, exc); |
| 336 |
} |
| 337 |
throw ExceptionHelper.<Exception>throwIfThrowable(ex); |
| 338 |
} |
| 339 |
} finally { |
| 340 |
onAfterTerminate.run(); |
| 341 |
} |
| 342 |
} else { |
| 343 |
if (sourceMode == SYNC) { |
| 344 |
onComplete.run(); |
| 345 |
|
| 346 |
onAfterTerminate.run(); |
| 347 |
} |
| 348 |
} |
| 349 |
return v; |
| 350 |
} |
| 351 |
} |
| 352 |
} |