| 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.AtomicReference; |
| 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 |
|
| 25 |
/** |
| 26 |
* Maps the success value of a Single onto an ObservableSource and |
| 27 |
* relays its signals to the downstream observer. |
| 28 |
* |
| 29 |
* @param <T> the success value type of the Single source |
| 30 |
* @param <R> the result type of the ObservableSource and this operator |
| 31 |
* @since 2.1.15 |
| 32 |
*/ |
| 33 |
public final class SingleFlatMapObservable<T, R> extends Observable<R> { |
| 34 |
|
| 35 |
final SingleSource<T> source; |
| 36 |
|
| 37 |
final Function<? super T, ? extends ObservableSource<? extends R>> mapper; |
| 38 |
|
| 39 |
public SingleFlatMapObservable(SingleSource<T> source, |
| 40 |
Function<? super T, ? extends ObservableSource<? extends R>> mapper) { |
| 41 |
this.source = source; |
| 42 |
this.mapper = mapper; |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
protected void subscribeActual(Observer<? super R> observer) { |
| 47 |
FlatMapObserver<T, R> parent = new FlatMapObserver<>(observer, mapper); |
| 48 |
observer.onSubscribe(parent); |
| 49 |
source.subscribe(parent); |
| 50 |
} |
| 51 |
|
| 52 |
static final class FlatMapObserver<T, R> |
| 53 |
extends AtomicReference<Disposable> |
| 54 |
implements Observer<R>, SingleObserver<T>, Disposable { |
| 55 |
|
| 56 |
private static final long serialVersionUID = -8948264376121066672L; |
| 57 |
|
| 58 |
final Observer<? super R> downstream; |
| 59 |
|
| 60 |
final Function<? super T, ? extends ObservableSource<? extends R>> mapper; |
| 61 |
|
| 62 |
FlatMapObserver(Observer<? super R> downstream, Function<? super T, ? extends ObservableSource<? extends R>> mapper) { |
| 63 |
this.downstream = downstream; |
| 64 |
this.mapper = mapper; |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public void onNext(R t) { |
| 69 |
downstream.onNext(t); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public void onError(Throwable t) { |
| 74 |
downstream.onError(t); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void onComplete() { |
| 79 |
downstream.onComplete(); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void dispose() { |
| 84 |
DisposableHelper.dispose(this); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public boolean isDisposed() { |
| 89 |
return DisposableHelper.isDisposed(get()); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void onSubscribe(Disposable d) { |
| 94 |
DisposableHelper.replace(this, d); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void onSuccess(T t) { |
| 99 |
ObservableSource<? extends R> o; |
| 100 |
|
| 101 |
try { |
| 102 |
o = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Publisher"); |
| 103 |
} catch (Throwable ex) { |
| 104 |
Exceptions.throwIfFatal(ex); |
| 105 |
downstream.onError(ex); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
if (!isDisposed()) { |
| 110 |
o.subscribe(this); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
} |
| 115 |
} |