Debian Codegraph @

src/main/java/io/reactivex/rxjava3/observables/ConnectableObservable.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.observables;
15
16 import java.util.Objects;
17 import java.util.concurrent.TimeUnit;
18
19 import io.reactivex.rxjava3.annotations.*;
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.functions.Consumer;
23 import io.reactivex.rxjava3.internal.functions.*;
24 import io.reactivex.rxjava3.internal.operators.observable.*;
25 import io.reactivex.rxjava3.internal.util.ConnectConsumer;
26 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
27 import io.reactivex.rxjava3.schedulers.Schedulers;
28
29 /**
30 * A {@code ConnectableObservable} resembles an ordinary {@link Observable}, except that it does not begin
31 * emitting items when it is subscribed to, but only when its {@link #connect} method is called. In this way you
32 * can wait for all intended {@link Observer}s to {@link Observable#subscribe} to the {@code Observable}
33 * before the {@code Observable} begins emitting items.
34 * <p>
35 * <img width="640" height="510" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/publishConnect.v3.png" alt="">
36 * <p>
37 * When the upstream terminates, the {@code ConnectableObservable} remains in this terminated state and,
38 * depending on the actual underlying implementation, relays cached events to late {@code Observer}s.
39 * In order to reuse and restart this {@code ConnectableObservable}, the {@link #reset()} method has to be called.
40 * When called, this {@code ConnectableObservable} will appear as fresh, unconnected source to new {@code Observer}s.
41 * Disposing the connection will reset the {@code ConnectableObservable} to its fresh state and there is no need to call
42 * {@link #reset()} in this case.
43 * <p>
44 * Note that although {@link #connect()} and {@link #reset()} are safe to call from multiple threads, it is recommended
45 * a dedicated thread or business logic manages the connection or resetting of a {@code ConnectableObservable} so that
46 * there is no unwanted signal loss due to early {@code connect()} or {@code reset()} calls while {@code Observer}s are
47 * still being subscribed to to this {@code ConnectableObservable} to receive signals from the get go.
48 *
49 * @see <a href="https://github.com/ReactiveX/RxJava/wiki/Connectable-Observable-Operators">RxJava Wiki: Connectable Observable Operators</a>
50 * @param <T>
51 * the type of items emitted by the {@code ConnectableObservable}
52 */
53 public abstract class ConnectableObservable<T> extends Observable<T> {
54
55 /**
56 * Instructs the {@code ConnectableObservable} to begin emitting the items from its underlying
57 * {@link Observable} to its {@link Observer}s.
58 * <dl>
59 * <dt><b>Scheduler:</b></dt>
60 * <dd>The behavior is determined by the implementor of this abstract class.</dd>
61 * </dl>
62 *
63 * @param connection
64 * the action that receives the connection subscription before the subscription to source happens
65 * allowing the caller to synchronously disconnect a synchronous source
66 * @throws NullPointerException if {@code connection} is {@code null}
67 * @see <a href="http://reactivex.io/documentation/operators/connect.html">ReactiveX documentation: Connect</a>
68 */
69 @SchedulerSupport(SchedulerSupport.NONE)
70 public abstract void connect(@NonNull Consumer<? super Disposable> connection);
71
72 /**
73 * Resets this {@code ConnectableObservable} into its fresh state if it has terminated
74 * or has been disposed.
75 * <p>
76 * Calling this method on a fresh or active {@code ConnectableObservable} has no effect.
77 * <dl>
78 * <dt><b>Scheduler:</b></dt>
79 * <dd>The behavior is determined by the implementor of this abstract class.</dd>
80 * </dl>
81 * @since 3.0.0
82 */
83 @SchedulerSupport(SchedulerSupport.NONE)
84 public abstract void reset();
85
86 /**
87 * Instructs the {@code ConnectableObservable} to begin emitting the items from its underlying
88 * {@link Observable} to its {@link Observer}s.
89 * <p>
90 * To disconnect from a synchronous source, use the {@link #connect(Consumer)} method.
91 * <dl>
92 * <dt><b>Scheduler:</b></dt>
93 * <dd>The behavior is determined by the implementor of this abstract class.</dd>
94 * </dl>
95 *
96 * @return the {@link Disposable} representing the connection
97 * @see <a href="http://reactivex.io/documentation/operators/connect.html">ReactiveX documentation: Connect</a>
98 */
99 @NonNull
100 @SchedulerSupport(SchedulerSupport.NONE)
101 public final Disposable connect() {
102 ConnectConsumer cc = new ConnectConsumer();
103 connect(cc);
104 return cc.disposable;
105 }
106
107 /**
108 * Returns an {@link Observable} that stays connected to this {@code ConnectableObservable} as long as there
109 * is at least one subscription to this {@code ConnectableObservable}.
110 * <dl>
111 * <dt><b>Scheduler:</b></dt>
112 * <dd>This {@code refCount} overload does not operate on any particular {@link Scheduler}.</dd>
113 * </dl>
114 * @return a new {@code Observable} instance
115 * @see <a href="http://reactivex.io/documentation/operators/refcount.html">ReactiveX documentation: RefCount</a>
116 * @see #refCount(int)
117 * @see #refCount(long, TimeUnit)
118 * @see #refCount(int, long, TimeUnit)
119 */
120 @NonNull
121 @CheckReturnValue
122 @SchedulerSupport(SchedulerSupport.NONE)
123 public Observable<T> refCount() {
124 return RxJavaPlugins.onAssembly(new ObservableRefCount<>(this));
125 }
126
127 /**
128 * Connects to the upstream {@code ConnectableObservable} if the number of subscribed
129 * observers reaches the specified count and disconnect if all {@link Observer}s have unsubscribed.
130 * <dl>
131 * <dt><b>Scheduler:</b></dt>
132 * <dd>This {@code refCount} overload does not operate on any particular {@link Scheduler}.</dd>
133 * </dl>
134 * <p>History: 2.1.14 - experimental
135 * @param observerCount the number of {@code Observer}s required to connect to the upstream
136 * @return the new {@link Observable} instance
137 * @throws IllegalArgumentException if {@code observerCount} is non-positive
138 * @since 2.2
139 */
140 @CheckReturnValue
141 @SchedulerSupport(SchedulerSupport.NONE)
142 @NonNull
143 public final Observable<T> refCount(int observerCount) {
144 return refCount(observerCount, 0, TimeUnit.NANOSECONDS, Schedulers.trampoline());
145 }
146
147 /**
148 * Connects to the upstream {@code ConnectableObservable} if the number of subscribed
149 * observers reaches 1 and disconnect after the specified
150 * timeout if all {@link Observer}s have unsubscribed.
151 * <dl>
152 * <dt><b>Scheduler:</b></dt>
153 * <dd>This {@code refCount} overload operates on the {@code computation} {@link Scheduler}.</dd>
154 * </dl>
155 * <p>History: 2.1.14 - experimental
156 * @param timeout the time to wait before disconnecting after all {@code Observer}s unsubscribed
157 * @param unit the time unit of the timeout
158 * @return the new {@link Observable} instance
159 * @throws NullPointerException if {@code unit} is {@code null}
160 * @see #refCount(long, TimeUnit, Scheduler)
161 * @since 2.2
162 */
163 @CheckReturnValue
164 @SchedulerSupport(SchedulerSupport.COMPUTATION)
165 @NonNull
166 public final Observable<T> refCount(long timeout, @NonNull TimeUnit unit) {
167 return refCount(1, timeout, unit, Schedulers.computation());
168 }
169
170 /**
171 * Connects to the upstream {@code ConnectableObservable} if the number of subscribed
172 * observers reaches 1 and disconnect after the specified
173 * timeout if all {@link Observer}s have unsubscribed.
174 * <dl>
175 * <dt><b>Scheduler:</b></dt>
176 * <dd>This {@code refCount} overload operates on the specified {@link Scheduler}.</dd>
177 * </dl>
178 * <p>History: 2.1.14 - experimental
179 * @param timeout the time to wait before disconnecting after all {@code Observer}s unsubscribed
180 * @param unit the time unit of the timeout
181 * @param scheduler the target scheduler to wait on before disconnecting
182 * @return the new {@link Observable} instance
183 * @throws NullPointerException if {@code unit} or {@code scheduler} is {@code null}
184 * @since 2.2
185 */
186 @CheckReturnValue
187 @SchedulerSupport(SchedulerSupport.CUSTOM)
188 @NonNull
189 public final Observable<T> refCount(long timeout, @NonNull TimeUnit unit, @NonNull Scheduler scheduler) {
190 return refCount(1, timeout, unit, scheduler);
191 }
192
193 /**
194 * Connects to the upstream {@code ConnectableObservable} if the number of subscribed
195 * observers reaches the specified count and disconnect after the specified
196 * timeout if all {@link Observer}s have unsubscribed.
197 * <dl>
198 * <dt><b>Scheduler:</b></dt>
199 * <dd>This {@code refCount} overload operates on the {@code computation} {@link Scheduler}.</dd>
200 * </dl>
201 * <p>History: 2.1.14 - experimental
202 * @param observerCount the number of {@code Observer}s required to connect to the upstream
203 * @param timeout the time to wait before disconnecting after all {@code Observer}s unsubscribed
204 * @param unit the time unit of the timeout
205 * @return the new {@link Observable} instance
206 * @throws NullPointerException if {@code unit} or {@code scheduler} is {@code null}
207 * @throws IllegalArgumentException if {@code observerCount} is non-positive
208 * @see #refCount(int, long, TimeUnit, Scheduler)
209 * @since 2.2
210 */
211 @CheckReturnValue
212 @SchedulerSupport(SchedulerSupport.COMPUTATION)
213 @NonNull
214 public final Observable<T> refCount(int observerCount, long timeout, @NonNull TimeUnit unit) {
215 return refCount(observerCount, timeout, unit, Schedulers.computation());
216 }
217
218 /**
219 * Connects to the upstream {@code ConnectableObservable} if the number of subscribed
220 * observers reaches the specified count and disconnect after the specified
221 * timeout if all {@link Observer}s have unsubscribed.
222 * <dl>
223 * <dt><b>Scheduler:</b></dt>
224 * <dd>This {@code refCount} overload operates on the specified {@link Scheduler}.</dd>
225 * </dl>
226 * <p>History: 2.1.14 - experimental
227 * @param observerCount the number of {@code Observer}s required to connect to the upstream
228 * @param timeout the time to wait before disconnecting after all {@code Observer}s unsubscribed
229 * @param unit the time unit of the timeout
230 * @param scheduler the target scheduler to wait on before disconnecting
231 * @return the new {@link Observable} instance
232 * @throws NullPointerException if {@code unit} or {@code scheduler} is {@code null}
233 * @throws IllegalArgumentException if {@code observerCount} is non-positive
234 * @since 2.2
235 */
236 @CheckReturnValue
237 @SchedulerSupport(SchedulerSupport.CUSTOM)
238 @NonNull
239 public final Observable<T> refCount(int observerCount, long timeout, @NonNull TimeUnit unit, @NonNull Scheduler scheduler) {
240 ObjectHelper.verifyPositive(observerCount, "observerCount");
241 Objects.requireNonNull(unit, "unit is null");
242 Objects.requireNonNull(scheduler, "scheduler is null");
243 return RxJavaPlugins.onAssembly(new ObservableRefCount<>(this, observerCount, timeout, unit, scheduler));
244 }
245
246 /**
247 * Returns an {@link Observable} that automatically connects (at most once) to this {@code ConnectableObservable}
248 * when the first {@link Observer} subscribes.
249 * <p>
250 * <img width="640" height="348" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/autoConnect.o.png" alt="">
251 * <p>
252 * The connection happens after the first subscription and happens at most once
253 * during the lifetime of the returned {@code Observable}. If this {@code ConnectableObservable}
254 * terminates, the connection is never renewed, no matter how {@code Observer}s come
255 * and go. Use {@link #refCount()} to renew a connection or dispose an active
256 * connection when all {@code Observer}s have disposed their {@link Disposable}s.
257 * <p>
258 * This overload does not allow disconnecting the connection established via
259 * {@link #connect(Consumer)}. Use the {@link #autoConnect(int, Consumer)} overload
260 * to gain access to the {@code Disposable} representing the only connection.
261 * <dl>
262 * <dt><b>Scheduler:</b></dt>
263 * <dd>{@code autoConnect} overload does not operate on any particular {@link Scheduler}.</dd>
264 * </dl>
265 *
266 * @return a new {@code Observable} instance that automatically connects to this {@code ConnectableObservable}
267 * when the first {@code Observer} subscribes
268 */
269 @NonNull
270 @CheckReturnValue
271 @SchedulerSupport(SchedulerSupport.NONE)
272 public Observable<T> autoConnect() {
273 return autoConnect(1);
274 }
275
276 /**
277 * Returns an {@link Observable} that automatically connects (at most once) to this {@code ConnectableObservable}
278 * when the specified number of {@link Observer}s subscribe to it.
279 * <p>
280 * <img width="640" height="348" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/autoConnect.o.png" alt="">
281 * <p>
282 * The connection happens after the given number of subscriptions and happens at most once
283 * during the lifetime of the returned {@code Observable}. If this {@code ConnectableObservable}
284 * terminates, the connection is never renewed, no matter how {@code Observer}s come
285 * and go. Use {@link #refCount()} to renew a connection or dispose an active
286 * connection when all {@code Observer}s have disposed their {@link Disposable}s.
287 * <p>
288 * This overload does not allow disconnecting the connection established via
289 * {@link #connect(Consumer)}. Use the {@link #autoConnect(int, Consumer)} overload
290 * to gain access to the {@code Disposable} representing the only connection.
291 * <dl>
292 * <dt><b>Scheduler:</b></dt>
293 * <dd>{@code autoConnect} overload does not operate on any particular {@link Scheduler}.</dd>
294 * </dl>
295 *
296 * @param numberOfObservers the number of subscribers to await before calling connect
297 * on the {@code ConnectableObservable}. A non-positive value indicates
298 * an immediate connection.
299 * @return a new {@code Observable} instance that automatically connects to this {@code ConnectableObservable}
300 * when the specified number of {@code Observer}s subscribe to it
301 */
302 @NonNull
303 @CheckReturnValue
304 @SchedulerSupport(SchedulerSupport.NONE)
305 public Observable<T> autoConnect(int numberOfObservers) {
306 return autoConnect(numberOfObservers, Functions.emptyConsumer());
307 }
308
309 /**
310 * Returns an {@link Observable} that automatically connects (at most once) to this {@code ConnectableObservable}
311 * when the specified number of {@link Observer}s subscribe to it and calls the
312 * specified callback with the {@link Disposable} associated with the established connection.
313 * <p>
314 * <img width="640" height="348" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/autoConnect.o.png" alt="">
315 * <p>
316 * The connection happens after the given number of subscriptions and happens at most once
317 * during the lifetime of the returned {@code Observable}. If this {@code ConnectableObservable}
318 * terminates, the connection is never renewed, no matter how {@code Observer}s come
319 * and go. Use {@link #refCount()} to renew a connection or dispose an active
320 * connection when all {@code Observer}s have disposed their {@code Disposable}s.
321 * <dl>
322 * <dt><b>Scheduler:</b></dt>
323 * <dd>{@code autoConnect} overload does not operate on any particular {@link Scheduler}.</dd>
324 * </dl>
325 *
326 * @param numberOfObservers the number of subscribers to await before calling connect
327 * on the {@code ConnectableObservable}. A non-positive value indicates
328 * an immediate connection.
329 * @param connection the callback {@link Consumer} that will receive the {@code Disposable} representing the
330 * established connection
331 * @return a new {@code Observable} instance that automatically connects to this {@code ConnectableObservable}
332 * when the specified number of {@code Observer}s subscribe to it and calls the
333 * specified callback with the {@code Disposable} associated with the established connection
334 * @throws NullPointerException if {@code connection} is {@code null}
335 */
336 @NonNull
337 @CheckReturnValue
338 @SchedulerSupport(SchedulerSupport.NONE)
339 public Observable<T> autoConnect(int numberOfObservers, @NonNull Consumer<? super Disposable> connection) {
340 Objects.requireNonNull(connection, "connection is null");
341 if (numberOfObservers <= 0) {
342 this.connect(connection);
343 return RxJavaPlugins.onAssembly(this);
344 }
345 return RxJavaPlugins.onAssembly(new ObservableAutoConnect<>(this, numberOfObservers, connection));
346 }
347 }