Class ConnectableFlowable<T>

java.lang.Object
io.reactivex.rxjava3.core.Flowable<T>
io.reactivex.rxjava3.flowables.ConnectableFlowable<T>
Type Parameters:
T - the type of items emitted by the ConnectableFlowable
All Implemented Interfaces:
org.reactivestreams.Publisher<T>
Direct Known Subclasses:
FlowablePublish, FlowableReplay

public abstract class ConnectableFlowable<T> extends Flowable<T>
A ConnectableFlowable resembles an ordinary Flowable, except that it does not begin emitting items when it is subscribed to, but only when its connect(io.reactivex.rxjava3.functions.Consumer<? super io.reactivex.rxjava3.disposables.Disposable>) method is called. In this way you can wait for all intended Subscribers to Flowable.subscribe() to the Flowable before the Flowable begins emitting items.

When the upstream terminates, the ConnectableFlowable remains in this terminated state and, depending on the actual underlying implementation, relays cached events to late Subscribers. In order to reuse and restart this ConnectableFlowable, the reset() method has to be called. When called, this ConnectableFlowable will appear as fresh, unconnected source to new Subscribers. Disposing the connection will reset the ConnectableFlowable to its fresh state and there is no need to call reset() in this case.

Note that although connect() and reset() are safe to call from multiple threads, it is recommended a dedicated thread or business logic manages the connection or resetting of a ConnectableFlowable so that there is no unwanted signal loss due to early connect() or reset() calls while Subscribers are still being subscribed to to this ConnectableFlowable to receive signals from the get go.

Since:
2.0.0
See Also:
  • Constructor Details

    • ConnectableFlowable

      public ConnectableFlowable()
  • Method Details

    • connect

      @SchedulerSupport("none") public abstract void connect(@NonNull @NonNull Consumer<? super Disposable> connection)
      Instructs the ConnectableFlowable to begin emitting the items from its underlying Flowable to its Subscribers.
      Scheduler:
      The behavior is determined by the implementor of this abstract class.
      Parameters:
      connection - the action that receives the connection subscription before the subscription to source happens allowing the caller to synchronously disconnect a synchronous source
      Throws:
      NullPointerException - if connection is null
      See Also:
    • reset

      @SchedulerSupport("none") public abstract void reset()
      Resets this ConnectableFlowable into its fresh state if it has terminated.

      Calling this method on a fresh or active ConnectableFlowable has no effect.

      Scheduler:
      The behavior is determined by the implementor of this abstract class.
      Since:
      3.0.0
    • connect

      @NonNull @SchedulerSupport("none") public final @NonNull Disposable connect()
      Instructs the ConnectableFlowable to begin emitting the items from its underlying Flowable to its Subscribers.

      To disconnect from a synchronous source, use the connect(io.reactivex.rxjava3.functions.Consumer) method.

      Scheduler:
      The behavior is determined by the implementor of this abstract class.
      Returns:
      the subscription representing the connection
      See Also:
    • refCount

      Returns a Flowable that stays connected to this ConnectableFlowable as long as there is at least one subscription to this ConnectableFlowable.
      Backpressure:
      The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's backpressure behavior.
      Scheduler:
      This refCount overload does not operate on any particular Scheduler.
      Returns:
      the new Flowable instance
      See Also:
    • refCount

      @CheckReturnValue @SchedulerSupport("none") @BackpressureSupport(PASS_THROUGH) @NonNull public final @NonNull Flowable<T> refCount(int subscriberCount)
      Connects to the upstream ConnectableFlowable if the number of subscribed subscriber reaches the specified count and disconnect if all subscribers have unsubscribed.
      Backpressure:
      The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's backpressure behavior.
      Scheduler:
      This refCount overload does not operate on any particular Scheduler.

      History: 2.1.14 - experimental

      Parameters:
      subscriberCount - the number of subscribers required to connect to the upstream
      Returns:
      the new Flowable instance
      Throws:
      IllegalArgumentException - if subscriberCount is non-positive
      Since:
      2.2
    • refCount

      @CheckReturnValue @SchedulerSupport("io.reactivex:computation") @BackpressureSupport(PASS_THROUGH) @NonNull public final @NonNull Flowable<T> refCount(long timeout, @NonNull @NonNull TimeUnit unit)
      Connects to the upstream ConnectableFlowable if the number of subscribed subscriber reaches 1 and disconnect after the specified timeout if all subscribers have unsubscribed.
      Backpressure:
      The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's backpressure behavior.
      Scheduler:
      This refCount overload operates on the computation Scheduler.

      History: 2.1.14 - experimental

      Parameters:
      timeout - the time to wait before disconnecting after all subscribers unsubscribed
      unit - the time unit of the timeout
      Returns:
      the new Flowable instance
      Throws:
      NullPointerException - if unit is null
      Since:
      2.2
      See Also:
    • refCount

      Connects to the upstream ConnectableFlowable if the number of subscribed subscriber reaches 1 and disconnect after the specified timeout if all subscribers have unsubscribed.
      Backpressure:
      The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's backpressure behavior.
      Scheduler:
      This refCount overload operates on the specified Scheduler.

      History: 2.1.14 - experimental

      Parameters:
      timeout - the time to wait before disconnecting after all subscribers unsubscribed
      unit - the time unit of the timeout
      scheduler - the target scheduler to wait on before disconnecting
      Returns:
      the new Flowable instance
      Throws:
      NullPointerException - if unit or scheduler is null
      Since:
      2.2
    • refCount

      @CheckReturnValue @SchedulerSupport("io.reactivex:computation") @BackpressureSupport(PASS_THROUGH) @NonNull public final @NonNull Flowable<T> refCount(int subscriberCount, long timeout, @NonNull @NonNull TimeUnit unit)
      Connects to the upstream ConnectableFlowable if the number of subscribed subscriber reaches the specified count and disconnect after the specified timeout if all subscribers have unsubscribed.
      Backpressure:
      The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's backpressure behavior.
      Scheduler:
      This refCount overload operates on the computation Scheduler.

      History: 2.1.14 - experimental

      Parameters:
      subscriberCount - the number of subscribers required to connect to the upstream
      timeout - the time to wait before disconnecting after all subscribers unsubscribed
      unit - the time unit of the timeout
      Returns:
      the new Flowable instance
      Throws:
      NullPointerException - if unit is null
      IllegalArgumentException - if subscriberCount is non-positive
      Since:
      2.2
      See Also:
    • refCount

      @CheckReturnValue @SchedulerSupport("custom") @BackpressureSupport(PASS_THROUGH) @NonNull public final @NonNull Flowable<T> refCount(int subscriberCount, long timeout, @NonNull @NonNull TimeUnit unit, @NonNull @NonNull Scheduler scheduler)
      Connects to the upstream ConnectableFlowable if the number of subscribed subscriber reaches the specified count and disconnect after the specified timeout if all subscribers have unsubscribed.
      Backpressure:
      The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's backpressure behavior.
      Scheduler:
      This refCount overload operates on the specified Scheduler.

      History: 2.1.14 - experimental

      Parameters:
      subscriberCount - the number of subscribers required to connect to the upstream
      timeout - the time to wait before disconnecting after all subscribers unsubscribed
      unit - the time unit of the timeout
      scheduler - the target scheduler to wait on before disconnecting
      Returns:
      the new Flowable instance
      Throws:
      NullPointerException - if unit or scheduler is null
      IllegalArgumentException - if subscriberCount is non-positive
      Since:
      2.2
    • autoConnect

      Returns a Flowable that automatically connects (at most once) to this ConnectableFlowable when the first Subscriber subscribes.

      The connection happens after the first subscription and happens at most once during the lifetime of the returned Flowable. If this ConnectableFlowable terminates, the connection is never renewed, no matter how Subscribers come and go. Use refCount() to renew a connection or dispose an active connection when all Subscribers have cancelled their Subscriptions.

      This overload does not allow disconnecting the connection established via connect(Consumer). Use the autoConnect(int, Consumer) overload to gain access to the Disposable representing the only connection.

      Backpressure:
      The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's behavior.
      Scheduler:
      autoConnect does not operate by default on a particular Scheduler.
      Returns:
      a new Flowable instance that automatically connects to this ConnectableFlowable when the first Subscriber subscribes
      See Also:
    • autoConnect

      @NonNull @CheckReturnValue @BackpressureSupport(PASS_THROUGH) @SchedulerSupport("none") public @NonNull Flowable<T> autoConnect(int numberOfSubscribers)
      Returns a Flowable that automatically connects (at most once) to this ConnectableFlowable when the specified number of Subscribers subscribe to it.

      The connection happens after the given number of subscriptions and happens at most once during the lifetime of the returned Flowable. If this ConnectableFlowable terminates, the connection is never renewed, no matter how Subscribers come and go. Use refCount() to renew a connection or dispose an active connection when all Subscribers have cancelled their Subscriptions.

      This overload does not allow disconnecting the connection established via connect(Consumer). Use the autoConnect(int, Consumer) overload to gain access to the Disposable representing the only connection.

      Backpressure:
      The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's behavior.
      Scheduler:
      autoConnect does not operate by default on a particular Scheduler.
      Parameters:
      numberOfSubscribers - the number of subscribers to await before calling connect on the ConnectableFlowable. A non-positive value indicates an immediate connection.
      Returns:
      a new Flowable instance that automatically connects to this ConnectableFlowable when the specified number of Subscribers subscribe to it
    • autoConnect

      @NonNull @CheckReturnValue @BackpressureSupport(PASS_THROUGH) @SchedulerSupport("none") public @NonNull Flowable<T> autoConnect(int numberOfSubscribers, @NonNull @NonNull Consumer<? super Disposable> connection)
      Returns a Flowable that automatically connects (at most once) to this ConnectableFlowable when the specified number of Subscribers subscribe to it and calls the specified callback with the Disposable associated with the established connection.

      The connection happens after the given number of subscriptions and happens at most once during the lifetime of the returned Flowable. If this ConnectableFlowable terminates, the connection is never renewed, no matter how Subscribers come and go. Use refCount() to renew a connection or dispose an active connection when all Subscribers have cancelled their Subscriptions.

      Backpressure:
      The operator itself doesn't interfere with backpressure which is determined by the upstream ConnectableFlowable's behavior.
      Scheduler:
      autoConnect does not operate by default on a particular Scheduler.
      Parameters:
      numberOfSubscribers - the number of subscribers to await before calling connect on the ConnectableFlowable. A non-positive value indicates an immediate connection.
      connection - the callback Consumer that will receive the Disposable representing the established connection
      Returns:
      a new Flowable instance that automatically connects to this ConnectableFlowable when the specified number of Subscribers subscribe to it and calls the specified callback with the Disposable associated with the established connection
      Throws:
      NullPointerException - if connection is null