| 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.single; |
| 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.subscriptions.SubscriptionHelper; |
| 26 |
|
| 27 |
/** |
| 28 |
* A Flowable that emits items based on applying a specified function to the item emitted by the |
| 29 |
* source Single, where that function returns a Publisher. |
| 30 |
* <p> |
| 31 |
* <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Single.flatMapPublisher.v3.png" alt=""> |
| 32 |
* <dl> |
| 33 |
* <dt><b>Backpressure:</b></dt> |
| 34 |
* <dd>The returned {@code Flowable} honors the backpressure of the downstream consumer |
| 35 |
* and the {@code Publisher} returned by the mapper function is expected to honor it as well.</dd> |
| 36 |
* <dt><b>Scheduler:</b></dt> |
| 37 |
* <dd>{@code flatMapPublisher} does not operate by default on a particular {@link Scheduler}.</dd> |
| 38 |
* </dl> |
| 39 |
* |
| 40 |
* @param <T> the source value type |
| 41 |
* @param <R> the result value type |
| 42 |
* |
| 43 |
* @see <a href="http://reactivex.io/documentation/operators/flatmap.html">ReactiveX operators documentation: FlatMap</a> |
| 44 |
* @since 2.1.15 |
| 45 |
*/ |
| 46 |
public final class SingleFlatMapPublisher<T, R> extends Flowable<R> { |
| 47 |
|
| 48 |
final SingleSource<T> source; |
| 49 |
final Function<? super T, ? extends Publisher<? extends R>> mapper; |
| 50 |
|
| 51 |
public SingleFlatMapPublisher(SingleSource<T> source, |
| 52 |
Function<? super T, ? extends Publisher<? extends R>> mapper) { |
| 53 |
this.source = source; |
| 54 |
this.mapper = mapper; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
protected void subscribeActual(Subscriber<? super R> downstream) { |
| 59 |
source.subscribe(new SingleFlatMapPublisherObserver<>(downstream, mapper)); |
| 60 |
} |
| 61 |
|
| 62 |
static final class SingleFlatMapPublisherObserver<S, T> extends AtomicLong |
| 63 |
implements SingleObserver<S>, FlowableSubscriber<T>, Subscription { |
| 64 |
|
| 65 |
private static final long serialVersionUID = 7759721921468635667L; |
| 66 |
|
| 67 |
final Subscriber<? super T> downstream; |
| 68 |
final Function<? super S, ? extends Publisher<? extends T>> mapper; |
| 69 |
final AtomicReference<Subscription> parent; |
| 70 |
Disposable disposable; |
| 71 |
|
| 72 |
SingleFlatMapPublisherObserver(Subscriber<? super T> actual, |
| 73 |
Function<? super S, ? extends Publisher<? extends T>> mapper) { |
| 74 |
this.downstream = actual; |
| 75 |
this.mapper = mapper; |
| 76 |
this.parent = new AtomicReference<>(); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void onSubscribe(Disposable d) { |
| 81 |
this.disposable = d; |
| 82 |
downstream.onSubscribe(this); |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public void onSuccess(S value) { |
| 87 |
Publisher<? extends T> f; |
| 88 |
try { |
| 89 |
f = Objects.requireNonNull(mapper.apply(value), "the mapper returned a null Publisher"); |
| 90 |
} catch (Throwable e) { |
| 91 |
Exceptions.throwIfFatal(e); |
| 92 |
downstream.onError(e); |
| 93 |
return; |
| 94 |
} |
| 95 |
if (parent.get() != SubscriptionHelper.CANCELLED) { |
| 96 |
f.subscribe(this); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public void onSubscribe(Subscription s) { |
| 102 |
SubscriptionHelper.deferredSetOnce(parent, this, s); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public void onNext(T t) { |
| 107 |
downstream.onNext(t); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public void onComplete() { |
| 112 |
downstream.onComplete(); |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public void onError(Throwable e) { |
| 117 |
downstream.onError(e); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public void request(long n) { |
| 122 |
SubscriptionHelper.deferredRequest(parent, this, n); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public void cancel() { |
| 127 |
disposable.dispose(); |
| 128 |
SubscriptionHelper.cancel(parent); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
} |