forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2.x: add full implementation for Single.flatMapPublisher so doesn't b…
…atch requests (ReactiveX#6015)
- Loading branch information
1 parent
d24bfbd
commit 377b6e2
Showing
3 changed files
with
228 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
src/main/java/io/reactivex/internal/operators/single/SingleFlatMapPublisher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/** | ||
* Copyright (c) 2016-present, RxJava Contributors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is | ||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See | ||
* the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.reactivex.internal.operators.single; | ||
|
||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import org.reactivestreams.Publisher; | ||
import org.reactivestreams.Subscriber; | ||
import org.reactivestreams.Subscription; | ||
|
||
import io.reactivex.Flowable; | ||
import io.reactivex.FlowableSubscriber; | ||
import io.reactivex.Scheduler; | ||
import io.reactivex.SingleObserver; | ||
import io.reactivex.SingleSource; | ||
import io.reactivex.disposables.Disposable; | ||
import io.reactivex.exceptions.Exceptions; | ||
import io.reactivex.functions.Function; | ||
import io.reactivex.internal.functions.ObjectHelper; | ||
import io.reactivex.internal.subscriptions.SubscriptionHelper; | ||
|
||
/** | ||
* A Flowable that emits items based on applying a specified function to the item emitted by the | ||
* source Single, where that function returns a Publisher. | ||
* <p> | ||
* <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Single.flatMapPublisher.png" alt=""> | ||
* <dl> | ||
* <dt><b>Backpressure:</b></dt> | ||
* <dd>The returned {@code Flowable} honors the backpressure of the downstream consumer | ||
* and the {@code Publisher} returned by the mapper function is expected to honor it as well.</dd> | ||
* <dt><b>Scheduler:</b></dt> | ||
* <dd>{@code flatMapPublisher} does not operate by default on a particular {@link Scheduler}.</dd> | ||
* </dl> | ||
* | ||
* @param <T> the source value type | ||
* @param <R> the result value type | ||
* | ||
* @see <a href="http://reactivex.io/documentation/operators/flatmap.html">ReactiveX operators documentation: FlatMap</a> | ||
* @since 2.1.15 | ||
*/ | ||
public final class SingleFlatMapPublisher<T, R> extends Flowable<R> { | ||
|
||
final SingleSource<T> source; | ||
final Function<? super T, ? extends Publisher<? extends R>> mapper; | ||
|
||
public SingleFlatMapPublisher(SingleSource<T> source, | ||
Function<? super T, ? extends Publisher<? extends R>> mapper) { | ||
this.source = source; | ||
this.mapper = mapper; | ||
} | ||
|
||
@Override | ||
protected void subscribeActual(Subscriber<? super R> actual) { | ||
source.subscribe(new SingleFlatMapPublisherObserver<T, R>(actual, mapper)); | ||
} | ||
|
||
static final class SingleFlatMapPublisherObserver<S, T> extends AtomicLong | ||
implements SingleObserver<S>, FlowableSubscriber<T>, Subscription { | ||
|
||
private static final long serialVersionUID = 7759721921468635667L; | ||
|
||
final Subscriber<? super T> actual; | ||
final Function<? super S, ? extends Publisher<? extends T>> mapper; | ||
final AtomicReference<Subscription> parent; | ||
Disposable disposable; | ||
|
||
SingleFlatMapPublisherObserver(Subscriber<? super T> actual, | ||
Function<? super S, ? extends Publisher<? extends T>> mapper) { | ||
this.actual = actual; | ||
this.mapper = mapper; | ||
this.parent = new AtomicReference<Subscription>(); | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Disposable d) { | ||
this.disposable = d; | ||
actual.onSubscribe(this); | ||
} | ||
|
||
@Override | ||
public void onSuccess(S value) { | ||
Publisher<? extends T> f; | ||
try { | ||
f = ObjectHelper.requireNonNull(mapper.apply(value), "the mapper returned a null Publisher"); | ||
} catch (Throwable e) { | ||
Exceptions.throwIfFatal(e); | ||
actual.onError(e); | ||
return; | ||
} | ||
f.subscribe(this); | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Subscription s) { | ||
SubscriptionHelper.deferredSetOnce(parent, this, s); | ||
} | ||
|
||
@Override | ||
public void onNext(T t) { | ||
actual.onNext(t); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
actual.onComplete(); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
actual.onError(e); | ||
} | ||
|
||
@Override | ||
public void request(long n) { | ||
SubscriptionHelper.deferredRequest(parent, this, n); | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
disposable.dispose(); | ||
SubscriptionHelper.cancel(parent); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters