rx.internal.operators.OnSubscribeFlatMapSingle Maven / Gradle / Ivy
/**
* Copyright 2017 Netflix, Inc.
*
* 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 rx.internal.operators;
import java.util.Queue;
import java.util.concurrent.atomic.*;
import rx.*;
import rx.exceptions.Exceptions;
import rx.functions.Func1;
import rx.internal.util.ExceptionsUtils;
import rx.internal.util.atomic.MpscLinkedAtomicQueue;
import rx.internal.util.unsafe.*;
import rx.plugins.RxJavaHooks;
import rx.subscriptions.CompositeSubscription;
/**
* Maps upstream values to Singles and merges them, up to a given
* number of them concurrently, optionally delaying errors.
* History: 1.2.7 - experimental
* @param the upstream value type
* @param the inner Singles and result value type
* @since 1.3
*/
public final class OnSubscribeFlatMapSingle implements Observable.OnSubscribe {
final Observable source;
final Func1 super T, ? extends Single extends R>> mapper;
final boolean delayErrors;
final int maxConcurrency;
public OnSubscribeFlatMapSingle(Observable source, Func1 super T, ? extends Single extends R>> mapper,
boolean delayErrors, int maxConcurrency) {
if (mapper == null) {
throw new NullPointerException("mapper is null");
}
if (maxConcurrency <= 0) {
throw new IllegalArgumentException("maxConcurrency > 0 required but it was " + maxConcurrency);
}
this.source = source;
this.mapper = mapper;
this.delayErrors = delayErrors;
this.maxConcurrency = maxConcurrency;
}
@Override
public void call(Subscriber super R> child) {
FlatMapSingleSubscriber parent = new FlatMapSingleSubscriber(child, mapper, delayErrors, maxConcurrency);
child.add(parent.set);
child.add(parent.requested);
child.setProducer(parent.requested);
source.unsafeSubscribe(parent);
}
static final class FlatMapSingleSubscriber extends Subscriber {
final Subscriber super R> actual;
final Func1 super T, ? extends Single extends R>> mapper;
final boolean delayErrors;
final int maxConcurrency;
final AtomicInteger wip;
final AtomicInteger active;
final CompositeSubscription set;
final AtomicReference errors;
final Queue