forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnSubscribeAmb.java
More file actions
447 lines (414 loc) · 16.3 KB
/
Copy pathOnSubscribeAmb.java
File metadata and controls
447 lines (414 loc) · 16.3 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
* Copyright 2014 Netflix, Inc.
*
* 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 rx.internal.operators;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicReference;
import rx.*;
import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.functions.Action0;
import rx.subscriptions.Subscriptions;
/**
* Given multiple {@link Observable}s, propagates the one that first emits an item.
* @param <T> the value type
*/
public final class OnSubscribeAmb<T> implements OnSubscribe<T> {
//give default access instead of private as a micro-optimization
//for access from anonymous classes below
final Iterable<? extends Observable<? extends T>> sources;
/**
* Given two {@link Observable}s, propagates the one that first emits an item.
*
* @param <T> the common value base type
* @param o1
* the first {@code Observable}
* @param o2
* the second {@code Observable}
* @return an {@code Observable} that mirrors the one of the source {@code Observable}s that was first to
* emit an item
*/
public static <T> OnSubscribe<T> amb(Observable<? extends T> o1, Observable<? extends T> o2) {
List<Observable<? extends T>> sources = new ArrayList<Observable<? extends T>>();
sources.add(o1);
sources.add(o2);
return amb(sources);
}
/**
* Given three {@link Observable}s, propagates the one that first emits an item.
*
* @param <T> the common value base type
* @param o1
* the first {@code Observable}
* @param o2
* the second {@code Observable}
* @param o3
* the third {@code Observable}
* @return an {@code Observable} that mirrors the one of the source {@code Observable}s that was first to
* emit an item
*/
public static <T> OnSubscribe<T> amb(Observable<? extends T> o1, Observable<? extends T> o2, Observable<? extends T> o3) {
List<Observable<? extends T>> sources = new ArrayList<Observable<? extends T>>();
sources.add(o1);
sources.add(o2);
sources.add(o3);
return amb(sources);
}
/**
* Given four {@link Observable}s, propagates the one that first emits an item.
*
* @param <T> the common value base type
* @param o1
* the first {@code Observable}
* @param o2
* the second {@code Observable}
* @param o3
* the third {@code Observable}
* @param o4
* the fourth {@code Observable}
* @return an {@code Observable} that mirrors the one of the source {@code Observable}s that was first to
* emit an item
*/
public static <T> OnSubscribe<T> amb(Observable<? extends T> o1, Observable<? extends T> o2, Observable<? extends T> o3, Observable<? extends T> o4) {
List<Observable<? extends T>> sources = new ArrayList<Observable<? extends T>>();
sources.add(o1);
sources.add(o2);
sources.add(o3);
sources.add(o4);
return amb(sources);
}
/**
* Given five {@link Observable}s, propagates the one that first emits an item.
*
* @param <T> the common value base type
* @param o1
* the first {@code Observable}
* @param o2
* the second {@code Observable}
* @param o3
* the third {@code Observable}
* @param o4
* the fourth {@code Observable}
* @param o5
* the fifth {@code Observable}
* @return an {@code Observable} that mirrors the one of the source {@code Observable}s that was first to
* emit an item
*/
public static <T> OnSubscribe<T> amb(Observable<? extends T> o1, Observable<? extends T> o2, Observable<? extends T> o3, Observable<? extends T> o4, Observable<? extends T> o5) {
List<Observable<? extends T>> sources = new ArrayList<Observable<? extends T>>();
sources.add(o1);
sources.add(o2);
sources.add(o3);
sources.add(o4);
sources.add(o5);
return amb(sources);
}
/**
* Given six {@link Observable}s, propagates the one that first emits an item.
*
* @param <T> the common value base type
* @param o1
* the first {@code Observable}
* @param o2
* the second {@code Observable}
* @param o3
* the third {@code Observable}
* @param o4
* the fourth {@code Observable}
* @param o5
* the fifth {@code Observable}
* @param o6
* the sixth {@code Observable}
* @return an {@code Observable} that mirrors the one of the source {@code Observable}s that was first to
* emit an item
*/
public static <T> OnSubscribe<T> amb(Observable<? extends T> o1, Observable<? extends T> o2, Observable<? extends T> o3, Observable<? extends T> o4, Observable<? extends T> o5, Observable<? extends T> o6) {
List<Observable<? extends T>> sources = new ArrayList<Observable<? extends T>>();
sources.add(o1);
sources.add(o2);
sources.add(o3);
sources.add(o4);
sources.add(o5);
sources.add(o6);
return amb(sources);
}
/**
* Given seven {@link Observable}s, propagates the one that first emits an item.
*
* @param <T> the common value base type
* @param o1
* the first {@code Observable}
* @param o2
* the second {@code Observable}
* @param o3
* the third {@code Observable}
* @param o4
* the fourth {@code Observable}
* @param o5
* the fifth {@code Observable}
* @param o6
* the sixth {@code Observable}
* @param o7
* the seventh {@code Observable}
* @return an {@code Observable} that mirrors the one of the source {@code Observable}s that was first to
* emit an item
*/
public static <T> OnSubscribe<T> amb(Observable<? extends T> o1, Observable<? extends T> o2, Observable<? extends T> o3, Observable<? extends T> o4, Observable<? extends T> o5, Observable<? extends T> o6, Observable<? extends T> o7) {
List<Observable<? extends T>> sources = new ArrayList<Observable<? extends T>>();
sources.add(o1);
sources.add(o2);
sources.add(o3);
sources.add(o4);
sources.add(o5);
sources.add(o6);
sources.add(o7);
return amb(sources);
}
/**
* Given eight {@link Observable}s, propagates the one that first emits an item.
*
* @param <T> the common value base type
* @param o1
* the first {@code Observable}
* @param o2
* the second {@code Observable}
* @param o3
* the third {@code Observable}
* @param o4
* the fourth {@code Observable}
* @param o5
* the fifth {@code Observable}
* @param o6
* the sixth {@code Observable}
* @param o7
* the seventh {@code Observable}
* @param o8
* the eighth {@code Observable}
* @return an {@code Observable} that mirrors the one of the source {@code Observable}s that was first to
* emit an item
*/
public static <T> OnSubscribe<T> amb(Observable<? extends T> o1, Observable<? extends T> o2, Observable<? extends T> o3, Observable<? extends T> o4, Observable<? extends T> o5, Observable<? extends T> o6, Observable<? extends T> o7, Observable<? extends T> o8) {
List<Observable<? extends T>> sources = new ArrayList<Observable<? extends T>>();
sources.add(o1);
sources.add(o2);
sources.add(o3);
sources.add(o4);
sources.add(o5);
sources.add(o6);
sources.add(o7);
sources.add(o8);
return amb(sources);
}
/**
* Given nine {@link Observable}s, propagates the one that first emits an item.
*
* @param <T> the common value base type
* @param o1
* the first {@code Observable}
* @param o2
* the second {@code Observable}
* @param o3
* the third {@code Observable}
* @param o4
* the fourth {@code Observable}
* @param o5
* the fifth {@code Observable}
* @param o6
* the sixth {@code Observable}
* @param o7
* the seventh {@code Observable}
* @param o8
* the eighth {@code Observable}
* @param o9
* the ninth {@code Observable}
* @return an {@code Observable} that mirrors the one of the source {@code Observable}s that was first to
* emit an item
*/
public static <T> OnSubscribe<T> amb(Observable<? extends T> o1, Observable<? extends T> o2, Observable<? extends T> o3, Observable<? extends T> o4, Observable<? extends T> o5, Observable<? extends T> o6, Observable<? extends T> o7, Observable<? extends T> o8, Observable<? extends T> o9) {
List<Observable<? extends T>> sources = new ArrayList<Observable<? extends T>>();
sources.add(o1);
sources.add(o2);
sources.add(o3);
sources.add(o4);
sources.add(o5);
sources.add(o6);
sources.add(o7);
sources.add(o8);
sources.add(o9);
return amb(sources);
}
/**
* Given a set of {@link Observable}s, propagates the one that first emits an item.
*
* @param <T> the common value base type
* @param sources
* an {@code Iterable} of {@code Observable}s
* @return an {@code Observable} that mirrors the one of the {@code Observable}s in {@code sources} that was
* the first to emit an item
*/
public static <T> OnSubscribe<T> amb(final Iterable<? extends Observable<? extends T>> sources) {
return new OnSubscribeAmb<T>(sources);
}
static final class AmbSubscriber<T> extends Subscriber<T> {
private final Subscriber<? super T> subscriber;
private final Selection<T> selection;
private boolean chosen;
AmbSubscriber(long requested, Subscriber<? super T> subscriber, Selection<T> selection) {
this.subscriber = subscriber;
this.selection = selection;
// initial request
request(requested);
}
private void requestMore(long n) {
request(n);
}
@Override
public void onNext(T t) {
if (isSelected()) {
subscriber.onNext(t);
}
}
@Override
public void onCompleted() {
if (isSelected()) {
subscriber.onCompleted();
}
}
@Override
public void onError(Throwable e) {
if (isSelected()) {
subscriber.onError(e);
}
}
private boolean isSelected() {
if (chosen) {
return true;
}
if (selection.get() == this) {
// fast-path
chosen = true;
return true;
} else {
if (selection.compareAndSet(null, this)) {
selection.unsubscribeOthers(this);
chosen = true;
return true;
} else {
// we lost so unsubscribe ... and force cleanup again due to possible race conditions
selection.unsubscribeLosers();
return false;
}
}
}
}
@SuppressWarnings("serial")
static final class Selection<T> extends AtomicReference<AmbSubscriber<T>> {
final Collection<AmbSubscriber<T>> ambSubscribers = new ConcurrentLinkedQueue<AmbSubscriber<T>>();
public void unsubscribeLosers() {
AmbSubscriber<T> winner = get();
if (winner != null) {
unsubscribeOthers(winner);
}
}
public void unsubscribeOthers(AmbSubscriber<T> notThis) {
for (AmbSubscriber<T> other : ambSubscribers) {
if (other != notThis) {
other.unsubscribe();
}
}
ambSubscribers.clear();
}
}
private OnSubscribeAmb(Iterable<? extends Observable<? extends T>> sources) {
this.sources = sources;
}
@Override
public void call(final Subscriber<? super T> subscriber) {
final Selection<T> selection = new Selection<T>();
//setup unsubscription of all the subscribers to the sources
subscriber.add(Subscriptions.create(new Action0() {
@Override
public void call() {
AmbSubscriber<T> c;
if ((c = selection.get()) != null) {
// there is a single winner so we unsubscribe it
c.unsubscribe();
}
// if we are racing with others still existing, we'll also unsubscribe them
// if subscriptions are occurring as this is happening then this call may not
// unsubscribe everything. We protect ourselves though by doing another unsubscribe check
// after the subscription loop below
unsubscribeAmbSubscribers(selection.ambSubscribers);
}
}));
//need to subscribe to all the sources
for (Observable<? extends T> source : sources) {
if (subscriber.isUnsubscribed()) {
break;
}
AmbSubscriber<T> ambSubscriber = new AmbSubscriber<T>(0, subscriber, selection);
selection.ambSubscribers.add(ambSubscriber);
// check again if choice has been made so can stop subscribing
// if all sources were backpressure aware then this check
// would be pointless given that 0 was requested above from each ambSubscriber
AmbSubscriber<T> c;
if ((c = selection.get()) != null) {
// Already chose one, the rest can be skipped and we can clean up
selection.unsubscribeOthers(c);
return;
}
source.unsafeSubscribe(ambSubscriber);
}
// while subscribing unsubscription may have occurred so we clean up after
if (subscriber.isUnsubscribed()) {
unsubscribeAmbSubscribers(selection.ambSubscribers);
}
subscriber.setProducer(new Producer() {
@Override
public void request(long n) {
AmbSubscriber<T> c;
if ((c = selection.get()) != null) {
// propagate the request to that single Subscriber that won
c.requestMore(n);
} else {
//propagate the request to all the amb subscribers
for (AmbSubscriber<T> ambSubscriber: selection.ambSubscribers) {
if (!ambSubscriber.isUnsubscribed()) {
// make a best endeavours check to not waste requests
// if first emission has already occurred
if (selection.get() == ambSubscriber) {
ambSubscriber.requestMore(n);
// don't need to request from other subscribers because choice has been made
// and request has gone to choice
return;
} else {
ambSubscriber.requestMore(n);
}
}
}
}
}
});
}
static <T> void unsubscribeAmbSubscribers(Collection<AmbSubscriber<T>> ambSubscribers) {
if (!ambSubscribers.isEmpty()) {
for (AmbSubscriber<T> other : ambSubscribers) {
other.unsubscribe();
}
ambSubscribers.clear();
}
}
}