io.reactivex.flowable.ConnectableFlowable Maven / Gradle / Ivy
Show all versions of rxjava3-flowable Show documentation
/**
* 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.flowable;
import org.reactivestreams.Subscriber;
import io.reactivex.common.Disposable;
import io.reactivex.common.annotations.NonNull;
import io.reactivex.common.functions.Consumer;
import io.reactivex.common.internal.functions.Functions;
import io.reactivex.common.internal.utils.ConnectConsumer;
import io.reactivex.flowable.internal.operators.*;
/**
* A {@code ConnectableObservable} resembles an ordinary {@link Flowable}, except that it does not begin
* emitting items when it is subscribed to, but only when its {@link #connect} method is called. In this way you
* can wait for all intended {@link Subscriber}s to {@link Flowable#subscribe} to the {@code Observable}
* before the {@code Observable} begins emitting items.
*
*
*
* @see RxJava Wiki:
* Connectable Observable Operators
* @param
* the type of items emitted by the {@code ConnectableObservable}
*/
public abstract class ConnectableFlowable extends Flowable {
/**
* Instructs the {@code ConnectableObservable} to begin emitting the items from its underlying
* {@link Flowable} to its {@link Subscriber}s.
*
* @param connection
* the action that receives the connection subscription before the subscription to source happens
* allowing the caller to synchronously disconnect a synchronous source
* @see ReactiveX documentation: Connect
*/
public abstract void connect(@NonNull Consumer super Disposable> connection);
/**
* Instructs the {@code ConnectableObservable} to begin emitting the items from its underlying
* {@link Flowable} to its {@link Subscriber}s.
*
* To disconnect from a synchronous source, use the {@link #connect(io.reactivex.common.functions.Consumer)} method.
*
* @return the subscription representing the connection
* @see ReactiveX documentation: Connect
*/
public final Disposable connect() {
ConnectConsumer cc = new ConnectConsumer();
connect(cc);
return cc.disposable;
}
/**
* Returns an {@code Observable} that stays connected to this {@code ConnectableObservable} as long as there
* is at least one subscription to this {@code ConnectableObservable}.
*
* @return a {@link Flowable}
* @see ReactiveX documentation: RefCount
*/
@NonNull
public Flowable refCount() {
return RxJavaFlowablePlugins.onAssembly(new FlowableRefCount(this));
}
/**
* Returns an Observable that automatically connects to this ConnectableObservable
* when the first Subscriber subscribes.
*
* @return an Observable that automatically connects to this ConnectableObservable
* when the first Subscriber subscribes
*/
@NonNull
public Flowable autoConnect() {
return autoConnect(1);
}
/**
* Returns an Observable that automatically connects to this ConnectableObservable
* when the specified number of Subscribers subscribe to it.
*
* @param numberOfSubscribers the number of subscribers to await before calling connect
* on the ConnectableObservable. A non-positive value indicates
* an immediate connection.
* @return an Observable that automatically connects to this ConnectableObservable
* when the specified number of Subscribers subscribe to it
*/
@NonNull
public Flowable autoConnect(int numberOfSubscribers) {
return autoConnect(numberOfSubscribers, Functions.emptyConsumer());
}
/**
* Returns an Observable that automatically connects to this ConnectableObservable
* when the specified number of Subscribers subscribe to it and calls the
* specified callback with the Subscription associated with the established connection.
*
* @param numberOfSubscribers the number of subscribers to await before calling connect
* on the ConnectableObservable. A non-positive value indicates
* an immediate connection.
* @param connection the callback Action1 that will receive the Subscription representing the
* established connection
* @return an Observable that automatically connects to this ConnectableObservable
* when the specified number of Subscribers subscribe to it and calls the
* specified callback with the Subscription associated with the established connection
*/
@NonNull
public Flowable autoConnect(int numberOfSubscribers, @NonNull Consumer super Disposable> connection) {
if (numberOfSubscribers <= 0) {
this.connect(connection);
return RxJavaFlowablePlugins.onAssembly(this);
}
return RxJavaFlowablePlugins.onAssembly(new FlowableAutoConnect(this, numberOfSubscribers, connection));
}
}