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