| 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.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.exceptions.*; |
| 20 |
import io.reactivex.rxjava3.functions.Function; |
| 21 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionArbiter; |
| 22 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 23 |
|
| 24 |
import java.util.Objects; |
| 25 |
|
| 26 |
public final class FlowableOnErrorNext<T> extends AbstractFlowableWithUpstream<T, T> { |
| 27 |
final Function<? super Throwable, ? extends Publisher<? extends T>> nextSupplier; |
| 28 |
|
| 29 |
public FlowableOnErrorNext(Flowable<T> source, |
| 30 |
Function<? super Throwable, ? extends Publisher<? extends T>> nextSupplier) { |
| 31 |
super(source); |
| 32 |
this.nextSupplier = nextSupplier; |
| 33 |
} |
| 34 |
|
| 35 |
@Override |
| 36 |
protected void subscribeActual(Subscriber<? super T> s) { |
| 37 |
OnErrorNextSubscriber<T> parent = new OnErrorNextSubscriber<>(s, nextSupplier); |
| 38 |
s.onSubscribe(parent); |
| 39 |
source.subscribe(parent); |
| 40 |
} |
| 41 |
|
| 42 |
static final class OnErrorNextSubscriber<T> |
| 43 |
extends SubscriptionArbiter |
| 44 |
implements FlowableSubscriber<T> { |
| 45 |
private static final long serialVersionUID = 4063763155303814625L; |
| 46 |
|
| 47 |
final Subscriber<? super T> downstream; |
| 48 |
|
| 49 |
final Function<? super Throwable, ? extends Publisher<? extends T>> nextSupplier; |
| 50 |
|
| 51 |
boolean once; |
| 52 |
|
| 53 |
boolean done; |
| 54 |
|
| 55 |
long produced; |
| 56 |
|
| 57 |
OnErrorNextSubscriber(Subscriber<? super T> actual, Function<? super Throwable, ? extends Publisher<? extends T>> nextSupplier) { |
| 58 |
super(false); |
| 59 |
this.downstream = actual; |
| 60 |
this.nextSupplier = nextSupplier; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void onSubscribe(Subscription s) { |
| 65 |
setSubscription(s); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void onNext(T t) { |
| 70 |
if (done) { |
| 71 |
return; |
| 72 |
} |
| 73 |
if (!once) { |
| 74 |
produced++; |
| 75 |
} |
| 76 |
downstream.onNext(t); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void onError(Throwable t) { |
| 81 |
if (once) { |
| 82 |
if (done) { |
| 83 |
RxJavaPlugins.onError(t); |
| 84 |
return; |
| 85 |
} |
| 86 |
downstream.onError(t); |
| 87 |
return; |
| 88 |
} |
| 89 |
once = true; |
| 90 |
|
| 91 |
Publisher<? extends T> p; |
| 92 |
|
| 93 |
try { |
| 94 |
p = Objects.requireNonNull(nextSupplier.apply(t), "The nextSupplier returned a null Publisher"); |
| 95 |
} catch (Throwable e) { |
| 96 |
Exceptions.throwIfFatal(e); |
| 97 |
downstream.onError(new CompositeException(t, e)); |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
long mainProduced = produced; |
| 102 |
if (mainProduced != 0L) { |
| 103 |
produced(mainProduced); |
| 104 |
} |
| 105 |
|
| 106 |
p.subscribe(this); |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public void onComplete() { |
| 111 |
if (done) { |
| 112 |
return; |
| 113 |
} |
| 114 |
done = true; |
| 115 |
once = true; |
| 116 |
downstream.onComplete(); |
| 117 |
} |
| 118 |
} |
| 119 |
} |