| 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.observable; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.core.*; |
| 17 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 18 |
import io.reactivex.rxjava3.exceptions.*; |
| 19 |
import io.reactivex.rxjava3.functions.Function; |
| 20 |
import io.reactivex.rxjava3.internal.disposables.SequentialDisposable; |
| 21 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 22 |
|
| 23 |
public final class ObservableOnErrorNext<T> extends AbstractObservableWithUpstream<T, T> { |
| 24 |
final Function<? super Throwable, ? extends ObservableSource<? extends T>> nextSupplier; |
| 25 |
|
| 26 |
public ObservableOnErrorNext(ObservableSource<T> source, |
| 27 |
Function<? super Throwable, ? extends ObservableSource<? extends T>> nextSupplier) { |
| 28 |
super(source); |
| 29 |
this.nextSupplier = nextSupplier; |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
public void subscribeActual(Observer<? super T> t) { |
| 34 |
OnErrorNextObserver<T> parent = new OnErrorNextObserver<>(t, nextSupplier); |
| 35 |
t.onSubscribe(parent.arbiter); |
| 36 |
source.subscribe(parent); |
| 37 |
} |
| 38 |
|
| 39 |
static final class OnErrorNextObserver<T> implements Observer<T> { |
| 40 |
final Observer<? super T> downstream; |
| 41 |
final Function<? super Throwable, ? extends ObservableSource<? extends T>> nextSupplier; |
| 42 |
final SequentialDisposable arbiter; |
| 43 |
|
| 44 |
boolean once; |
| 45 |
|
| 46 |
boolean done; |
| 47 |
|
| 48 |
OnErrorNextObserver(Observer<? super T> actual, Function<? super Throwable, ? extends ObservableSource<? extends T>> nextSupplier) { |
| 49 |
this.downstream = actual; |
| 50 |
this.nextSupplier = nextSupplier; |
| 51 |
this.arbiter = new SequentialDisposable(); |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public void onSubscribe(Disposable d) { |
| 56 |
arbiter.replace(d); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public void onNext(T t) { |
| 61 |
if (done) { |
| 62 |
return; |
| 63 |
} |
| 64 |
downstream.onNext(t); |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public void onError(Throwable t) { |
| 69 |
if (once) { |
| 70 |
if (done) { |
| 71 |
RxJavaPlugins.onError(t); |
| 72 |
return; |
| 73 |
} |
| 74 |
downstream.onError(t); |
| 75 |
return; |
| 76 |
} |
| 77 |
once = true; |
| 78 |
|
| 79 |
ObservableSource<? extends T> p; |
| 80 |
|
| 81 |
try { |
| 82 |
p = nextSupplier.apply(t); |
| 83 |
} catch (Throwable e) { |
| 84 |
Exceptions.throwIfFatal(e); |
| 85 |
downstream.onError(new CompositeException(t, e)); |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
if (p == null) { |
| 90 |
NullPointerException npe = new NullPointerException("Observable is null"); |
| 91 |
npe.initCause(t); |
| 92 |
downstream.onError(npe); |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
p.subscribe(this); |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public void onComplete() { |
| 101 |
if (done) { |
| 102 |
return; |
| 103 |
} |
| 104 |
done = true; |
| 105 |
once = true; |
| 106 |
downstream.onComplete(); |
| 107 |
} |
| 108 |
} |
| 109 |
} |