Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableAutoConnect.java

raw
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.flowable;
15
16 import java.util.concurrent.atomic.AtomicInteger;
17
18 import org.reactivestreams.Subscriber;
19
20 import io.reactivex.rxjava3.core.Flowable;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.flowables.ConnectableFlowable;
23 import io.reactivex.rxjava3.functions.Consumer;
24
25 /**
26 * Wraps a {@link ConnectableFlowable} and calls its {@code connect()} method once
27 * the specified number of {@link Subscriber}s have subscribed.
28 *
29 * @param <T> the value type of the chain
30 */
31 public final class FlowableAutoConnect<T> extends Flowable<T> {
32 final ConnectableFlowable<? extends T> source;
33 final int numberOfSubscribers;
34 final Consumer<? super Disposable> connection;
35 final AtomicInteger clients;
36
37 public FlowableAutoConnect(ConnectableFlowable<? extends T> source,
38 int numberOfSubscribers,
39 Consumer<? super Disposable> connection) {
40 this.source = source;
41 this.numberOfSubscribers = numberOfSubscribers;
42 this.connection = connection;
43 this.clients = new AtomicInteger();
44 }
45
46 @Override
47 public void subscribeActual(Subscriber<? super T> child) {
48 source.subscribe(child);
49 if (clients.incrementAndGet() == numberOfSubscribers) {
50 source.connect(connection);
51 }
52 }
53 }