forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRxJavaHooks.java
More file actions
1239 lines (1115 loc) · 43.2 KB
/
RxJavaHooks.java
File metadata and controls
1239 lines (1115 loc) · 43.2 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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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 rx.plugins;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.concurrent.ScheduledExecutorService;
import rx.*;
import rx.annotations.Experimental;
import rx.functions.*;
import rx.internal.operators.*;
/**
* Utility class that holds hooks for various Observable, Single and Completable lifecycle-related
* points as well as Scheduler hooks.
* <p>
* The class features a lockdown state, see {@link #lockdown()} and {@link #isLockdown()}, to
* prevent further changes to the hooks.
*/
@Experimental
public final class RxJavaHooks {
/**
* Prevents changing the hook callbacks when set to true.
*/
/* test */ static volatile boolean lockdown;
static volatile Action1<Throwable> onError;
@SuppressWarnings("rawtypes")
static volatile Func1<Observable.OnSubscribe, Observable.OnSubscribe> onObservableCreate;
@SuppressWarnings("rawtypes")
static volatile Func1<Single.OnSubscribe, Single.OnSubscribe> onSingleCreate;
static volatile Func1<Completable.OnSubscribe, Completable.OnSubscribe> onCompletableCreate;
@SuppressWarnings("rawtypes")
static volatile Func2<Observable, Observable.OnSubscribe, Observable.OnSubscribe> onObservableStart;
@SuppressWarnings("rawtypes")
static volatile Func2<Single, Single.OnSubscribe, Single.OnSubscribe> onSingleStart;
static volatile Func2<Completable, Completable.OnSubscribe, Completable.OnSubscribe> onCompletableStart;
static volatile Func1<Scheduler, Scheduler> onComputationScheduler;
static volatile Func1<Scheduler, Scheduler> onIOScheduler;
static volatile Func1<Scheduler, Scheduler> onNewThreadScheduler;
static volatile Func1<Action0, Action0> onScheduleAction;
static volatile Func1<Subscription, Subscription> onObservableReturn;
static volatile Func1<Subscription, Subscription> onSingleReturn;
static volatile Func0<? extends ScheduledExecutorService> onGenericScheduledExecutorService;
static volatile Func1<Throwable, Throwable> onObservableSubscribeError;
static volatile Func1<Throwable, Throwable> onSingleSubscribeError;
static volatile Func1<Throwable, Throwable> onCompletableSubscribeError;
@SuppressWarnings("rawtypes")
static volatile Func1<Observable.Operator, Observable.Operator> onObservableLift;
@SuppressWarnings("rawtypes")
static volatile Func1<Observable.Operator, Observable.Operator> onSingleLift;
static volatile Func1<Completable.Operator, Completable.Operator> onCompletableLift;
/** Initialize with the default delegation to the original RxJavaPlugins. */
static {
init();
}
/** Utility class. */
private RxJavaHooks() {
throw new IllegalStateException("No instances!");
}
/**
* Initialize the hooks via delegating to RxJavaPlugins.
*/
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation"})
static void init() {
onError = new Action1<Throwable>() {
@Override
public void call(Throwable e) {
RxJavaPlugins.getInstance().getErrorHandler().handleError(e);
}
};
onObservableStart = new Func2<Observable, Observable.OnSubscribe, Observable.OnSubscribe>() {
@Override
public Observable.OnSubscribe call(Observable t1, Observable.OnSubscribe t2) {
return RxJavaPlugins.getInstance().getObservableExecutionHook().onSubscribeStart(t1, t2);
}
};
onObservableReturn = new Func1<Subscription, Subscription>() {
@Override
public Subscription call(Subscription f) {
return RxJavaPlugins.getInstance().getObservableExecutionHook().onSubscribeReturn(f);
}
};
onSingleStart = new Func2<Single, Single.OnSubscribe, Single.OnSubscribe>() {
@Override
public Single.OnSubscribe call(Single t1, Single.OnSubscribe t2) {
RxJavaSingleExecutionHook hook = RxJavaPlugins.getInstance().getSingleExecutionHook();
if (hook == RxJavaSingleExecutionHookDefault.getInstance()) {
return t2;
}
return new SingleFromObservable(hook.onSubscribeStart(t1,
new SingleToObservable(t2)));
}
};
onSingleReturn = new Func1<Subscription, Subscription>() {
@Override
public Subscription call(Subscription f) {
return RxJavaPlugins.getInstance().getSingleExecutionHook().onSubscribeReturn(f);
}
};
onCompletableStart = new Func2<Completable, Completable.OnSubscribe, Completable.OnSubscribe>() {
@Override
public Completable.OnSubscribe call(Completable t1, Completable.OnSubscribe t2) {
return RxJavaPlugins.getInstance().getCompletableExecutionHook().onSubscribeStart(t1, t2);
}
};
onScheduleAction = new Func1<Action0, Action0>() {
@Override
public Action0 call(Action0 a) {
return RxJavaPlugins.getInstance().getSchedulersHook().onSchedule(a);
}
};
onObservableSubscribeError = new Func1<Throwable, Throwable>() {
@Override
public Throwable call(Throwable t) {
return RxJavaPlugins.getInstance().getObservableExecutionHook().onSubscribeError(t);
}
};
onObservableLift = new Func1<Observable.Operator, Observable.Operator>() {
@Override
public Observable.Operator call(Observable.Operator t) {
return RxJavaPlugins.getInstance().getObservableExecutionHook().onLift(t);
}
};
onSingleSubscribeError = new Func1<Throwable, Throwable>() {
@Override
public Throwable call(Throwable t) {
return RxJavaPlugins.getInstance().getSingleExecutionHook().onSubscribeError(t);
}
};
onSingleLift = new Func1<Observable.Operator, Observable.Operator>() {
@Override
public Observable.Operator call(Observable.Operator t) {
return RxJavaPlugins.getInstance().getSingleExecutionHook().onLift(t);
}
};
onCompletableSubscribeError = new Func1<Throwable, Throwable>() {
@Override
public Throwable call(Throwable t) {
return RxJavaPlugins.getInstance().getCompletableExecutionHook().onSubscribeError(t);
}
};
onCompletableLift = new Func1<Completable.Operator, Completable.Operator>() {
@Override
public Completable.Operator call(Completable.Operator t) {
return RxJavaPlugins.getInstance().getCompletableExecutionHook().onLift(t);
}
};
initCreate();
}
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
static void initCreate() {
onObservableCreate = new Func1<Observable.OnSubscribe, Observable.OnSubscribe>() {
@Override
public Observable.OnSubscribe call(Observable.OnSubscribe f) {
return RxJavaPlugins.getInstance().getObservableExecutionHook().onCreate(f);
}
};
onSingleCreate = new Func1<rx.Single.OnSubscribe, rx.Single.OnSubscribe>() {
@Override
public rx.Single.OnSubscribe call(rx.Single.OnSubscribe f) {
return RxJavaPlugins.getInstance().getSingleExecutionHook().onCreate(f);
}
};
onCompletableCreate = new Func1<Completable.OnSubscribe, Completable.OnSubscribe>() {
@Override
public Completable.OnSubscribe call(Completable.OnSubscribe f) {
return RxJavaPlugins.getInstance().getCompletableExecutionHook().onCreate(f);
}
};
}
/**
* Reset all hook callbacks to those of the current RxJavaPlugins handlers.
*
* @see #clear()
*/
public static void reset() {
if (lockdown) {
return;
}
init();
onComputationScheduler = null;
onIOScheduler = null;
onNewThreadScheduler = null;
onGenericScheduledExecutorService = null;
}
/**
* Clears all hooks to be no-op (and pass-through)
* and onError hook to signal errors to the caller thread's
* UncaughtExceptionHandler.
*
* @see #reset()
*/
public static void clear() {
if (lockdown) {
return;
}
onError = null;
onObservableCreate = null;
onObservableStart = null;
onObservableReturn = null;
onObservableSubscribeError = null;
onObservableLift = null;
onSingleCreate = null;
onSingleStart = null;
onSingleReturn = null;
onSingleSubscribeError = null;
onSingleLift = null;
onCompletableCreate = null;
onCompletableStart = null;
onCompletableSubscribeError = null;
onCompletableLift = null;
onComputationScheduler = null;
onIOScheduler = null;
onNewThreadScheduler = null;
onScheduleAction = null;
onGenericScheduledExecutorService = null;
}
/**
* Prevents changing the hooks.
*/
public static void lockdown() {
lockdown = true;
}
/**
* Returns true if the hooks can no longer be changed.
* @return true if the hooks can no longer be changed
*/
public static boolean isLockdown() {
return lockdown;
}
/**
* Consume undeliverable Throwables (acts as a global catch).
* @param ex the exception to handle
*/
public static void onError(Throwable ex) {
Action1<Throwable> f = onError;
if (f != null) {
try {
f.call(ex);
return;
} catch (Throwable pluginException) {
/*
* We don't want errors from the plugin to affect normal flow.
* Since the plugin should never throw this is a safety net
* and will complain loudly to System.err so it gets fixed.
*/
System.err.println("The onError handler threw an Exception. It shouldn't. => " + pluginException.getMessage()); // NOPMD
pluginException.printStackTrace(); // NOPMD
signalUncaught(pluginException);
}
}
signalUncaught(ex);
}
static void signalUncaught(Throwable ex) {
Thread current = Thread.currentThread();
UncaughtExceptionHandler handler = current.getUncaughtExceptionHandler();
handler.uncaughtException(current, ex);
}
/**
* Hook to call when an Observable is created.
* @param <T> the value type
* @param onSubscribe the original OnSubscribe logic
* @return the original or replacement OnSubscribe instance
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Observable.OnSubscribe<T> onCreate(Observable.OnSubscribe<T> onSubscribe) {
Func1<Observable.OnSubscribe, Observable.OnSubscribe> f = onObservableCreate;
if (f != null) {
return f.call(onSubscribe);
}
return onSubscribe;
}
/**
* Hook to call when a Single is created.
* @param <T> the value type
* @param onSubscribe the original OnSubscribe logic
* @return the original or replacement OnSubscribe instance
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Single.OnSubscribe<T> onCreate(Single.OnSubscribe<T> onSubscribe) {
Func1<Single.OnSubscribe, Single.OnSubscribe> f = onSingleCreate;
if (f != null) {
return f.call(onSubscribe);
}
return onSubscribe;
}
/**
* Hook to call when a Completable is created.
* @param onSubscribe the original OnSubscribe logic
* @return the original or replacement OnSubscribe instance
*/
public static Completable.OnSubscribe onCreate(Completable.OnSubscribe onSubscribe) {
Func1<Completable.OnSubscribe, Completable.OnSubscribe> f = onCompletableCreate;
if (f != null) {
return f.call(onSubscribe);
}
return onSubscribe;
}
/**
* Hook to call when the Schedulers.computation() is called.
* @param scheduler the default computation scheduler
* @return the default of alternative scheduler
*/
public static Scheduler onComputationScheduler(Scheduler scheduler) {
Func1<Scheduler, Scheduler> f = onComputationScheduler;
if (f != null) {
return f.call(scheduler);
}
return scheduler;
}
/**
* Hook to call when the Schedulers.io() is called.
* @param scheduler the default io scheduler
* @return the default of alternative scheduler
*/
public static Scheduler onIOScheduler(Scheduler scheduler) {
Func1<Scheduler, Scheduler> f = onIOScheduler;
if (f != null) {
return f.call(scheduler);
}
return scheduler;
}
/**
* Hook to call when the Schedulers.newThread() is called.
* @param scheduler the default new thread scheduler
* @return the default of alternative scheduler
*/
public static Scheduler onNewThreadScheduler(Scheduler scheduler) {
Func1<Scheduler, Scheduler> f = onNewThreadScheduler;
if (f != null) {
return f.call(scheduler);
}
return scheduler;
}
/**
* Hook to call before the action is scheduled, allows
* decorating the original action.
* @param action the original action
* @return the original or alternative action
*/
public static Action0 onScheduledAction(Action0 action) {
Func1<Action0, Action0> f = onScheduleAction;
if (f != null) {
return f.call(action);
}
return action;
}
/**
* Hook to call before the child subscriber is subscribed to the OnSubscribe action.
* @param <T> the value type
* @param instance the parent Observable instance
* @param onSubscribe the original OnSubscribe action
* @return the original or alternative action that will be subscribed to
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Observable.OnSubscribe<T> onObservableStart(Observable<T> instance, Observable.OnSubscribe<T> onSubscribe) {
Func2<Observable, Observable.OnSubscribe, Observable.OnSubscribe> f = onObservableStart;
if (f != null) {
return f.call(instance, onSubscribe);
}
return onSubscribe;
}
/**
* Hook to call before the Observable.subscribe() method is about to return a Subscription.
* @param subscription the original subscription
* @return the original or alternative subscription that will be returned
*/
public static Subscription onObservableReturn(Subscription subscription) {
Func1<Subscription, Subscription> f = onObservableReturn;
if (f != null) {
return f.call(subscription);
}
return subscription;
}
/**
* Hook to call if the Observable.subscribe() crashes for some reason.
* @param error the error
* @return the original error or alternative Throwable to be thrown
*/
public static Throwable onObservableError(Throwable error) {
Func1<Throwable, Throwable> f = onObservableSubscribeError;
if (f != null) {
return f.call(error);
}
return error;
}
/**
* Hook to call before the child subscriber would subscribe to an Operator.
* @param <T> the input value type
* @param <R> the output value type
* @param operator the original operator
* @return the original or alternative operator that will be subscribed to
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T, R> Observable.Operator<R, T> onObservableLift(Observable.Operator<R, T> operator) {
Func1<Observable.Operator, Observable.Operator> f = onObservableLift;
if (f != null) {
return f.call(operator);
}
return operator;
}
/**
* Hook to call before the child subscriber is subscribed to the OnSubscribe action.
* @param <T> the value type
* @param instance the parent Single instance
* @param onSubscribe the original OnSubscribe action
* @return the original or alternative action that will be subscribed to
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T> Single.OnSubscribe<T> onSingleStart(Single<T> instance, Single.OnSubscribe<T> onSubscribe) {
Func2<Single, Single.OnSubscribe, Single.OnSubscribe> f = onSingleStart;
if (f != null) {
return f.call(instance, onSubscribe);
}
return onSubscribe;
}
/**
* Hook to call before the Single.subscribe() method is about to return a Subscription.
* @param subscription the original subscription
* @return the original or alternative subscription that will be returned
*/
public static Subscription onSingleReturn(Subscription subscription) {
Func1<Subscription, Subscription> f = onSingleReturn;
if (f != null) {
return f.call(subscription);
}
return subscription;
}
/**
* Hook to call if the Single.subscribe() crashes for some reason.
* @param error the error
* @return the original error or alternative Throwable to be thrown
*/
public static Throwable onSingleError(Throwable error) {
Func1<Throwable, Throwable> f = onSingleSubscribeError;
if (f != null) {
return f.call(error);
}
return error;
}
/**
* Hook to call before the child subscriber would subscribe to an Operator.
* @param <T> the input value type
* @param <R> the output value type
* @param operator the original operator
* @return the original or alternative operator that will be subscribed to
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static <T, R> Observable.Operator<R, T> onSingleLift(Observable.Operator<R, T> operator) {
Func1<Observable.Operator, Observable.Operator> f = onSingleLift;
if (f != null) {
return f.call(operator);
}
return operator;
}
/**
* Hook to call before the child subscriber is subscribed to the OnSubscribe action.
* @param <T> the value type
* @param instance the parent Completable instance
* @param onSubscribe the original OnSubscribe action
* @return the original or alternative action that will be subscribed to
*/
public static <T> Completable.OnSubscribe onCompletableStart(Completable instance, Completable.OnSubscribe onSubscribe) {
Func2<Completable, Completable.OnSubscribe, Completable.OnSubscribe> f = onCompletableStart;
if (f != null) {
return f.call(instance, onSubscribe);
}
return onSubscribe;
}
/**
* Hook to call if the Completable.subscribe() crashes for some reason.
* @param error the error
* @return the original error or alternative Throwable to be thrown
*/
public static Throwable onCompletableError(Throwable error) {
Func1<Throwable, Throwable> f = onCompletableSubscribeError;
if (f != null) {
return f.call(error);
}
return error;
}
/**
* Hook to call before the child subscriber would subscribe to an Operator.
* @param <T> the input value type
* @param <R> the output value type
* @param operator the original operator
* @return the original or alternative operator that will be subscribed to
*/
public static <T, R> Completable.Operator onCompletableLift(Completable.Operator operator) {
Func1<Completable.Operator, Completable.Operator> f = onCompletableLift;
if (f != null) {
return f.call(operator);
}
return operator;
}
/**
* Sets the global error consumer action unless a lockdown is in effect.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter has the effect that
* errors are routed to the current thread's {@link UncaughtExceptionHandler}.
* @param onError the action that will receive undeliverable Throwables
*/
public static void setOnError(Action1<Throwable> onError) {
if (lockdown) {
return;
}
RxJavaHooks.onError = onError;
}
/**
* Sets the Completable's onCreate hook function unless a lockdown is in effect.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same object.
* @param onCompletableCreate the function that takes the original CompletableOnSubscribe
* and should return a CompletableOnSubscribe.
*/
public static void setOnCompletableCreate(
Func1<Completable.OnSubscribe, Completable.OnSubscribe> onCompletableCreate) {
if (lockdown) {
return;
}
RxJavaHooks.onCompletableCreate = onCompletableCreate;
}
/**
* Sets the Observable onCreate hook function unless a lockdown is in effect.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same object.
* @param onObservableCreate the function that takes the original OnSubscribe
* and should return a OnSubscribe.
*/
@SuppressWarnings("rawtypes")
public static void setOnObservableCreate(
Func1<Observable.OnSubscribe, Observable.OnSubscribe> onObservableCreate) {
if (lockdown) {
return;
}
RxJavaHooks.onObservableCreate = onObservableCreate;
}
/**
* Sets the Single onCreate hook function unless a lockdown is in effect.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same object.
* @param onSingleCreate the function that takes the original OnSubscribe
* and should return a OnSubscribe.
*/
@SuppressWarnings("rawtypes")
public static void setOnSingleCreate(Func1<Single.OnSubscribe, Single.OnSubscribe> onSingleCreate) {
if (lockdown) {
return;
}
RxJavaHooks.onSingleCreate = onSingleCreate;
}
/**
* Sets the hook function for returning a scheduler when the Schedulers.computation() is called
* unless a lockdown is in effect.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same object.
* @param onComputationScheduler the function that receives the original computation scheduler
* and should return a scheduler.
*/
public static void setOnComputationScheduler(Func1<Scheduler, Scheduler> onComputationScheduler) {
if (lockdown) {
return;
}
RxJavaHooks.onComputationScheduler = onComputationScheduler;
}
/**
* Sets the hook function for returning a scheduler when the Schedulers.io() is called
* unless a lockdown is in effect.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same object.
* @param onIOScheduler the function that receives the original io scheduler
* and should return a scheduler.
*/
public static void setOnIOScheduler(Func1<Scheduler, Scheduler> onIOScheduler) {
if (lockdown) {
return;
}
RxJavaHooks.onIOScheduler = onIOScheduler;
}
/**
* Sets the hook function for returning a scheduler when the Schedulers.newThread() is called
* unless a lockdown is in effect.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same object.
* @param onNewThreadScheduler the function that receives the original new thread scheduler
* and should return a scheduler.
*/
public static void setOnNewThreadScheduler(Func1<Scheduler, Scheduler> onNewThreadScheduler) {
if (lockdown) {
return;
}
RxJavaHooks.onNewThreadScheduler = onNewThreadScheduler;
}
/**
* Sets the hook function that is called before an action is scheduled, allowing
* decorating that function, unless a lockdown is in effect.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same object.
* @param onScheduleAction the function that receives the original action and should
* return an Action0.
*/
public static void setOnScheduleAction(Func1<Action0, Action0> onScheduleAction) {
if (lockdown) {
return;
}
RxJavaHooks.onScheduleAction = onScheduleAction;
}
/**
* Sets the hook function that is called when a subscriber subscribes to a Completable
* unless a lockdown is in effect.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same CompletableOnSubscribe object.
* @param onCompletableStart the function that is called with the current Completable instance,
* its CompletableOnSubscribe function and should return a CompletableOnSubscribe function
* that gets actually subscribed to.
*/
public static void setOnCompletableStart(
Func2<Completable, Completable.OnSubscribe, Completable.OnSubscribe> onCompletableStart) {
if (lockdown) {
return;
}
RxJavaHooks.onCompletableStart = onCompletableStart;
}
/**
* Sets the hook function that is called when a subscriber subscribes to a Observable
* unless a lockdown is in effect.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same OnSubscribe object.
* @param onObservableStart the function that is called with the current Observable instance,
* its OnSubscribe function and should return a OnSubscribe function
* that gets actually subscribed to.
*/
@SuppressWarnings("rawtypes")
public static void setOnObservableStart(
Func2<Observable, Observable.OnSubscribe, Observable.OnSubscribe> onObservableStart) {
if (lockdown) {
return;
}
RxJavaHooks.onObservableStart = onObservableStart;
}
/**
* Sets the hook function that is called when a subscriber subscribes to a Single
* unless a lockdown is in effect.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same OnSubscribe object.
* @param onSingleStart the function that is called with the current Single instance,
* its OnSubscribe function and should return a OnSubscribe function
* that gets actually subscribed to.
*/
@SuppressWarnings("rawtypes")
public static void setOnSingleStart(Func2<Single, Single.OnSubscribe, Single.OnSubscribe> onSingleStart) {
if (lockdown) {
return;
}
RxJavaHooks.onSingleStart = onSingleStart;
}
/**
* Sets a hook function that is called when the Observable.subscribe() call
* is about to return a Subscription unless a lockdown is in effect.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same object.
* @param onObservableReturn the function that is called with the Subscriber that has been
* subscribed to the OnSubscribe function and returns a Subscription that will be returned by
* subscribe().
*/
public static void setOnObservableReturn(Func1<Subscription, Subscription> onObservableReturn) {
if (lockdown) {
return;
}
RxJavaHooks.onObservableReturn = onObservableReturn;
}
/**
* Sets a hook function that is called when the Single.subscribe() call
* is about to return a Subscription unless a lockdown is in effect.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same object.
* @param onSingleReturn the function that is called with the SingleSubscriber that has been
* subscribed to the OnSubscribe function and returns a Subscription that will be returned by
* subscribe().
*/
public static void setOnSingleReturn(Func1<Subscription, Subscription> onSingleReturn) {
if (lockdown) {
return;
}
RxJavaHooks.onSingleReturn = onSingleReturn;
}
/**
* Sets a hook function that is called when the Single.subscribe() call
* fails with an exception.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same object.
* @param onSingleSubscribeError the function that is called with the crash exception and should return
* an exception.
*/
public static void setOnSingleSubscribeError(Func1<Throwable, Throwable> onSingleSubscribeError) {
if (lockdown) {
return;
}
RxJavaHooks.onSingleSubscribeError = onSingleSubscribeError;
}
/**
* Returns the current Single onSubscribeError hook function or null if it is
* set to the default pass-through.
* <p>
* This operation is thread-safe.
* @return the current hook function
*/
public static Func1<Throwable, Throwable> getOnSingleSubscribeError() {
return onSingleSubscribeError;
}
/**
* Sets a hook function that is called when the Completable.subscribe() call
* fails with an exception.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same object.
* @param onCompletableSubscribeError the function that is called with the crash exception and should return
* an exception.
*/
public static void setOnCompletableSubscribeError(Func1<Throwable, Throwable> onCompletableSubscribeError) {
if (lockdown) {
return;
}
RxJavaHooks.onCompletableSubscribeError = onCompletableSubscribeError;
}
/**
* Returns the current Completable onSubscribeError hook function or null if it is
* set to the default pass-through.
* <p>
* This operation is thread-safe.
* @return the current hook function
*/
public static Func1<Throwable, Throwable> getOnCompletableSubscribeError() {
return onCompletableSubscribeError;
}
/**
* Sets a hook function that is called when the Observable.subscribe() call
* fails with an exception.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same object.
* @param onObservableSubscribeError the function that is called with the crash exception and should return
* an exception.
*/
public static void setOnObservableSubscribeError(Func1<Throwable, Throwable> onObservableSubscribeError) {
if (lockdown) {
return;
}
RxJavaHooks.onObservableSubscribeError = onObservableSubscribeError;
}
/**
* Returns the current Observable onSubscribeError hook function or null if it is
* set to the default pass-through.
* <p>
* This operation is thread-safe.
* @return the current hook function
*/
public static Func1<Throwable, Throwable> getOnObservableSubscribeError() {
return onObservableSubscribeError;
}
/**
* Sets a hook function that is called with an operator when an Observable operator built with
* lift() gets subscribed to.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same object.
* @param onObservableLift the function that is called with original Operator and should
* return an Operator instance.
*/
@SuppressWarnings("rawtypes")
public static void setOnObservableLift(Func1<Observable.Operator, Observable.Operator> onObservableLift) {
if (lockdown) {
return;
}
RxJavaHooks.onObservableLift = onObservableLift;
}
/**
* Returns the current Observable onLift hook function or null if it is
* set to the default pass-through.
* <p>
* This operation is thread-safe.
* @return the current hook function
*/
@SuppressWarnings("rawtypes")
public static Func1<Observable.Operator, Observable.Operator> getOnObservableLift() {
return onObservableLift;
}
/**
* Sets a hook function that is called with an operator when an Single operator built with
* lift() gets subscribed to.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same object.
* @param onSingleLift the function that is called with original Operator and should
* return an Operator instance.
*/
@SuppressWarnings("rawtypes")
public static void setOnSingleLift(Func1<Observable.Operator, Observable.Operator> onSingleLift) {
if (lockdown) {
return;
}
RxJavaHooks.onSingleLift = onSingleLift;
}
/**
* Returns the current Single onLift hook function or null if it is
* set to the default pass-through.
* <p>
* This operation is thread-safe.
* @return the current hook function
*/
@SuppressWarnings("rawtypes")
public static Func1<Observable.Operator, Observable.Operator> getOnSingleLift() {
return onSingleLift;
}
/**
* Sets a hook function that is called with an operator when a Completable operator built with
* lift() gets subscribed to.
* <p>
* This operation is thread-safe.
* <p>
* Calling with a {@code null} parameter restores the default behavior:
* the hook returns the same object.
* @param onCompletableLift the function that is called with original Operator and should
* return an Operator instance.
*/
public static void setOnCompletableLift(Func1<Completable.Operator, Completable.Operator> onCompletableLift) {
if (lockdown) {
return;
}
RxJavaHooks.onCompletableLift = onCompletableLift;
}
/**
* Returns the current Completable onLift hook function or null if it is
* set to the default pass-through.
* <p>
* This operation is thread-safe.
* @return the current hook function
*/
public static Func1<Completable.Operator, Completable.Operator> getOnCompletableLift() {