-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathRxJavaPlugins.java
574 lines (479 loc) · 19.1 KB
/
RxJavaPlugins.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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/**
* Copyright 2016 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 io.reactivex.plugins;
import java.lang.Thread.UncaughtExceptionHandler;
import org.reactivestreams.Subscriber;
import io.reactivex.*;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.*;
/**
* Utility class to inject handlers to certain standard RxJava operations.
*/
public final class RxJavaPlugins {
static volatile Consumer<Throwable> errorHandler;
static volatile Function<Runnable, Runnable> onScheduleHandler;
static volatile Function<Scheduler, Scheduler> onInitComputationHandler;
static volatile Function<Scheduler, Scheduler> onInitSingleHandler;
static volatile Function<Scheduler, Scheduler> onInitIoHandler;
static volatile Function<Scheduler, Scheduler> onInitNewThreadHandler;
static volatile Function<Scheduler, Scheduler> onComputationHandler;
static volatile Function<Scheduler, Scheduler> onSingleHandler;
static volatile Function<Scheduler, Scheduler> onIoHandler;
static volatile Function<Scheduler, Scheduler> onNewThreadHandler;
@SuppressWarnings("rawtypes")
static volatile Function<Flowable, Flowable> onFlowableAssembly;
@SuppressWarnings("rawtypes")
static volatile Function<Observable, Observable> onObservableAssembly;
@SuppressWarnings("rawtypes")
static volatile Function<Single, Single> onSingleAssembly;
static volatile Function<Completable, Completable> onCompletableAssembly;
@SuppressWarnings("rawtypes")
static volatile BiFunction<Flowable, Subscriber, Subscriber> onFlowableSubscribe;
@SuppressWarnings("rawtypes")
static volatile BiFunction<Observable, Observer, Observer> onObservableSubscribe;
@SuppressWarnings("rawtypes")
static volatile BiFunction<Single, SingleObserver, SingleObserver> onSingleSubscribe;
static volatile BiFunction<Completable, CompletableObserver, CompletableObserver> onCompletableSubscribe;
/** Prevents changing the plugins. */
private static volatile boolean lockdown;
/**
* Prevents changing the plugins from then on.
* <p>This allows container-like environments to prevent clients
* messing with plugins.
*/
public static void lockdown() {
lockdown = true;
}
/**
* Returns true if the plugins were locked down.
* @return true if the plugins were locked down
*/
public static boolean isLockdown() {
return lockdown;
}
public static Function<Scheduler, Scheduler> getComputationSchedulerHandler() {
return onComputationHandler;
}
public static Consumer<Throwable> getErrorHandler() {
return errorHandler;
}
public static Function<Scheduler, Scheduler> getInitComputationSchedulerHandler() {
return onInitComputationHandler;
}
public static Function<Scheduler, Scheduler> getInitIoSchedulerHandler() {
return onInitIoHandler;
}
public static Function<Scheduler, Scheduler> getInitNewThreadSchedulerHandler() {
return onInitNewThreadHandler;
}
public static Function<Scheduler, Scheduler> getInitSingleSchedulerHandler() {
return onInitSingleHandler;
}
public static Function<Scheduler, Scheduler> getIoSchedulerHandler() {
return onIoHandler;
}
public static Function<Scheduler, Scheduler> getNewThreadSchedulerHandler() {
return onNewThreadHandler;
}
public static Function<Runnable, Runnable> getScheduleHandler() {
return onScheduleHandler;
}
public static Function<Scheduler, Scheduler> getSingleSchedulerHandler() {
return onSingleHandler;
}
public static Scheduler initComputationScheduler(Scheduler defaultScheduler) {
Function<Scheduler, Scheduler> f = onInitComputationHandler;
if (f == null) {
return defaultScheduler;
}
return apply(f, defaultScheduler); // JIT will skip this
}
public static Scheduler initIoScheduler(Scheduler defaultScheduler) {
Function<Scheduler, Scheduler> f = onInitIoHandler;
if (f == null) {
return defaultScheduler;
}
return apply(f, defaultScheduler);
}
public static Scheduler initNewThreadScheduler(Scheduler defaultScheduler) {
Function<Scheduler, Scheduler> f = onInitNewThreadHandler;
if (f == null) {
return defaultScheduler;
}
return apply(f, defaultScheduler);
}
public static Scheduler initSingleScheduler(Scheduler defaultScheduler) {
Function<Scheduler, Scheduler> f = onInitSingleHandler;
if (f == null) {
return defaultScheduler;
}
return apply(f, defaultScheduler);
}
public static Scheduler onComputationScheduler(Scheduler defaultScheduler) {
Function<Scheduler, Scheduler> f = onComputationHandler;
if (f == null) {
return defaultScheduler;
}
return apply(f, defaultScheduler);
}
/**
* Called when an undeliverable error occurs.
* @param error the error to report
*/
public static void onError(Throwable error) {
Consumer<Throwable> f = errorHandler;
if (f != null) {
try {
f.accept(error);
return;
} catch (Throwable e) {
// Exceptions.throwIfFatal(e); TODO decide
if (error == null) {
error = new NullPointerException();
}
e.printStackTrace(); // NOPMD
}
} else {
if (error == null) {
error = new NullPointerException();
}
}
error.printStackTrace(); // NOPMD
UncaughtExceptionHandler handler = Thread.currentThread().getUncaughtExceptionHandler();
handler.uncaughtException(Thread.currentThread(), error);
}
public static Scheduler onIoScheduler(Scheduler defaultScheduler) {
Function<Scheduler, Scheduler> f = onIoHandler;
if (f == null) {
return defaultScheduler;
}
return apply(f, defaultScheduler);
}
public static Scheduler onNewThreadScheduler(Scheduler defaultScheduler) {
Function<Scheduler, Scheduler> f = onNewThreadHandler;
if (f == null) {
return defaultScheduler;
}
return apply(f, defaultScheduler);
}
/**
* Called when a task is scheduled.
* @param run the runnable instance
* @return the replacement runnable
*/
public static Runnable onSchedule(Runnable run) {
Function<Runnable, Runnable> f = onScheduleHandler;
if (f == null) {
return run;
}
return apply(f, run);
}
public static Scheduler onSingleScheduler(Scheduler defaultScheduler) {
Function<Scheduler, Scheduler> f = onSingleHandler;
if (f == null) {
return defaultScheduler;
}
return apply(f, defaultScheduler);
}
/**
* Removes all handlers and resets to default behavior.
*/
public static void reset() {
setErrorHandler(null);
setScheduleHandler(null);
setComputationSchedulerHandler(null);
setInitComputationSchedulerHandler(null);
setIoSchedulerHandler(null);
setInitIoSchedulerHandler(null);
setSingleSchedulerHandler(null);
setInitSingleSchedulerHandler(null);
setNewThreadSchedulerHandler(null);
setInitNewThreadSchedulerHandler(null);
setOnFlowableAssembly(null);
setOnFlowableSubscribe(null);
setOnObservableAssembly(null);
setOnObservableSubscribe(null);
setOnSingleAssembly(null);
setOnSingleSubscribe(null);
setOnCompletableAssembly(null);
setOnCompletableSubscribe(null);
}
public static void setComputationSchedulerHandler(Function<Scheduler, Scheduler> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onComputationHandler = handler;
}
public static void setErrorHandler(Consumer<Throwable> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
errorHandler = handler;
}
public static void setInitComputationSchedulerHandler(Function<Scheduler, Scheduler> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onInitComputationHandler = handler;
}
public static void setInitIoSchedulerHandler(Function<Scheduler, Scheduler> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onInitIoHandler = handler;
}
public static void setInitNewThreadSchedulerHandler(Function<Scheduler, Scheduler> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onInitNewThreadHandler = handler;
}
public static void setInitSingleSchedulerHandler(Function<Scheduler, Scheduler> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onInitSingleHandler = handler;
}
public static void setIoSchedulerHandler(Function<Scheduler, Scheduler> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onIoHandler = handler;
}
public static void setNewThreadSchedulerHandler(Function<Scheduler, Scheduler> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onNewThreadHandler = handler;
}
public static void setScheduleHandler(Function<Runnable, Runnable> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onScheduleHandler = handler;
}
public static void setSingleSchedulerHandler(Function<Scheduler, Scheduler> handler) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
onSingleHandler = handler;
}
/**
* Rewokes the lockdown, only for testing purposes.
*/
/* test. */static void unlock() {
lockdown = false;
}
public static Function<Completable, Completable> getOnCompletableAssembly() {
return onCompletableAssembly;
}
public static BiFunction<Completable, CompletableObserver, CompletableObserver> getOnCompletableSubscribe() {
return onCompletableSubscribe;
}
@SuppressWarnings("rawtypes")
public static Function<Flowable, Flowable> getOnFlowableAssembly() {
return onFlowableAssembly;
}
@SuppressWarnings("rawtypes")
public static BiFunction<Flowable, Subscriber, Subscriber> getOnFlowableSubscribe() {
return onFlowableSubscribe;
}
@SuppressWarnings("rawtypes")
public static Function<Single, Single> getOnSingleAssembly() {
return onSingleAssembly;
}
@SuppressWarnings("rawtypes")
public static BiFunction<Single, SingleObserver, SingleObserver> getOnSingleSubscribe() {
return onSingleSubscribe;
}
@SuppressWarnings("rawtypes")
public static Function<Observable, Observable> getOnObservableAssembly() {
return onObservableAssembly;
}
@SuppressWarnings("rawtypes")
public static BiFunction<Observable, Observer, Observer> getOnObservableSubscribe() {
return onObservableSubscribe;
}
public static void setOnCompletableAssembly(Function<Completable, Completable> onCompletableAssembly) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
RxJavaPlugins.onCompletableAssembly = onCompletableAssembly;
}
public static void setOnCompletableSubscribe(
BiFunction<Completable, CompletableObserver, CompletableObserver> onCompletableSubscribe) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
RxJavaPlugins.onCompletableSubscribe = onCompletableSubscribe;
}
@SuppressWarnings("rawtypes")
public static void setOnFlowableAssembly(Function<Flowable, Flowable> onFlowableAssembly) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
RxJavaPlugins.onFlowableAssembly = onFlowableAssembly;
}
@SuppressWarnings("rawtypes")
public static void setOnFlowableSubscribe(BiFunction<Flowable, Subscriber, Subscriber> onFlowableSubscribe) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
RxJavaPlugins.onFlowableSubscribe = onFlowableSubscribe;
}
@SuppressWarnings("rawtypes")
public static void setOnObservableAssembly(Function<Observable, Observable> onObservableAssembly) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
RxJavaPlugins.onObservableAssembly = onObservableAssembly;
}
@SuppressWarnings("rawtypes")
public static void setOnObservableSubscribe(
BiFunction<Observable, Observer, Observer> onObservableSubscribe) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
RxJavaPlugins.onObservableSubscribe = onObservableSubscribe;
}
@SuppressWarnings("rawtypes")
public static void setOnSingleAssembly(Function<Single, Single> onSingleAssembly) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
RxJavaPlugins.onSingleAssembly = onSingleAssembly;
}
@SuppressWarnings("rawtypes")
public static void setOnSingleSubscribe(BiFunction<Single, SingleObserver, SingleObserver> onSingleSubscribe) {
if (lockdown) {
throw new IllegalStateException("Plugins can't be changed anymore");
}
RxJavaPlugins.onSingleSubscribe = onSingleSubscribe;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Subscriber<? super T> onSubscribe(Flowable<T> source, Subscriber<? super T> subscriber) {
BiFunction<Flowable, Subscriber, Subscriber> f = onFlowableSubscribe;
if (f != null) {
return apply(f, source, subscriber);
}
return subscriber;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Observer<? super T> onSubscribe(Observable<T> source, Observer<? super T> observer) {
BiFunction<Observable, Observer, Observer> f = onObservableSubscribe;
if (f != null) {
return apply(f, source, observer);
}
return observer;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> SingleObserver<? super T> onSubscribe(Single<T> source, SingleObserver<? super T> subscriber) {
BiFunction<Single, SingleObserver, SingleObserver> f = onSingleSubscribe;
if (f != null) {
return apply(f, source, subscriber);
}
return subscriber;
}
public static CompletableObserver onSubscribe(Completable source, CompletableObserver subscriber) {
BiFunction<Completable, CompletableObserver, CompletableObserver> f = onCompletableSubscribe;
if (f != null) {
return apply(f, source, subscriber);
}
return subscriber;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Flowable<T> onAssembly(Flowable<T> source) {
Function<Flowable, Flowable> f = onFlowableAssembly;
if (f != null) {
return apply(f, source);
}
return source;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Observable<T> onAssembly(Observable<T> source) {
Function<Observable, Observable> f = onObservableAssembly;
if (f != null) {
return apply(f, source);
}
return source;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Single<T> onAssembly(Single<T> source) {
Function<Single, Single> f = onSingleAssembly;
if (f != null) {
return apply(f, source);
}
return source;
}
public static Completable onAssembly(Completable source) {
Function<Completable, Completable> f = onCompletableAssembly;
if (f != null) {
return apply(f, source);
}
return source;
}
/** Singleton consumer that calls RxJavaPlugins.onError. */
static final Consumer<Throwable> CONSUME_BY_RXJAVA_PLUGIN = new Consumer<Throwable>() {
@Override
public void accept(Throwable e) {
RxJavaPlugins.onError(e);
}
};
/**
* Returns a consumer which relays the received Throwable to RxJavaPlugins.onError().
* @return the consumer
*/
public static Consumer<Throwable> errorConsumer() {
return CONSUME_BY_RXJAVA_PLUGIN;
}
/**
* Wraps the call to the function in try-catch and propagates thrown
* checked exceptions as runtimeexception.
* @param <T> the input type
* @param <R> the output type
* @param f the function to call, not null (not verified)
* @param t the parameter value to the function
* @return the result of the function call
*/
static <T, R> R apply(Function<T, R> f, T t) {
try {
return f.apply(t);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
throw Exceptions.propagate(ex);
}
}
/**
* Wraps the call to the function in try-catch and propagates thrown
* checked exceptions as runtimeexception.
* @param <T> the first input type
* @param <U> the second input type
* @param <R> the output type
* @param f the function to call, not null (not verified)
* @param t the first parameter value to the function
* @param u the second parameter value to the function
* @return the result of the function call
*/
static <T, U, R> R apply(BiFunction<T, U, R> f, T t, U u) {
try {
return f.apply(t, u);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
throw Exceptions.propagate(ex);
}
}
/** Helper class, no instances. */
private RxJavaPlugins() {
throw new IllegalStateException("No instances!");
}
}