6
6
import rx .Subscriber ;
7
7
import rx .Subscription ;
8
8
import rx .functions .Action1 ;
9
+ import rx .functions .Action2 ;
9
10
import rx .functions .Actions ;
10
11
import rx .functions .Func1 ;
11
12
import rx .functions .Functions ;
17
18
*
18
19
* @author gscampbell
19
20
*/
20
- public class DebugHook extends RxJavaObservableExecutionHook {
21
+ public class DebugHook < C > extends RxJavaObservableExecutionHook {
21
22
private final Func1 onNextHook ;
22
- private final Action1 <DebugNotification > events ;
23
+ private final Func1 <DebugNotification , C > start ;
24
+ private final Action1 <C > complete ;
25
+ private final Action2 <C , Throwable > error ;
23
26
24
27
/**
25
28
* Creates a new instance of the DebugHook RxJava plug-in that can be passed into
@@ -31,18 +34,26 @@ public class DebugHook extends RxJavaObservableExecutionHook {
31
34
* @param events
32
35
* This action is invoked as each notification is generated
33
36
*/
34
- public DebugHook (Func1 onNextDataHook , Action1 <DebugNotification > events ) {
37
+ public DebugHook (Func1 onNextDataHook , Func1 <DebugNotification , C > start , Action1 <C > complete , Action2 <C , Throwable > error ) {
38
+ this .complete = complete ;
39
+ this .error = error ;
35
40
this .onNextHook = onNextDataHook == null ? Functions .identity () : onNextDataHook ;
36
- this .events = events == null ? Actions .empty () : events ;
41
+ this .start = ( Func1 < DebugNotification , C >) ( start == null ? Actions .empty () : start ) ;
37
42
}
38
43
39
44
@ Override
40
- public <T > OnSubscribe <T > onSubscribeStart (Observable <? extends T > observableInstance , final OnSubscribe <T > f ) {
45
+ public <T > OnSubscribe <T > onSubscribeStart (final Observable <? extends T > observableInstance , final OnSubscribe <T > f ) {
41
46
return new OnSubscribe <T >() {
42
47
@ Override
43
48
public void call (Subscriber <? super T > o ) {
44
- events .call (DebugNotification .createSubscribe (o , f ));
45
- f .call (wrapOutbound (null , o ));
49
+ C context = start .call (DebugNotification .createSubscribe (o , observableInstance , f ));
50
+ try {
51
+ f .call (wrapOutbound (null , o ));
52
+ complete .call (context );
53
+ }
54
+ catch (Throwable e ) {
55
+ error .call (context , e );
56
+ }
46
57
}
47
58
};
48
59
}
@@ -54,12 +65,7 @@ public <T> Subscription onSubscribeReturn(Observable<? extends T> observableInst
54
65
55
66
@ Override
56
67
public <T > OnSubscribe <T > onCreate (final OnSubscribe <T > f ) {
57
- return new OnSubscribe <T >() {
58
- @ Override
59
- public void call (Subscriber <? super T > o ) {
60
- f .call (wrapInbound (null , o ));
61
- }
62
- };
68
+ return new OnCreateWrapper <T >(f );
63
69
}
64
70
65
71
@ Override
@@ -81,19 +87,36 @@ public <T> Subscription onAdd(Subscriber<T> subscriber, Subscription s) {
81
87
private <R > Subscriber <? super R > wrapOutbound (Operator <? extends R , ?> bind , Subscriber <? super R > o ) {
82
88
if (o instanceof DebugSubscriber ) {
83
89
if (bind != null )
84
- ((DebugSubscriber <R >) o ).setFrom (bind );
90
+ ((DebugSubscriber <R , C >) o ).setFrom (bind );
85
91
return o ;
86
92
}
87
- return new DebugSubscriber <R >(onNextHook , events , o , bind , null );
93
+ return new DebugSubscriber <R , C >(onNextHook , start , complete , error , o , bind , null );
88
94
}
89
95
90
96
@ SuppressWarnings ("unchecked" )
91
97
private <T > Subscriber <? super T > wrapInbound (Operator <?, ? super T > bind , Subscriber <? super T > o ) {
92
98
if (o instanceof DebugSubscriber ) {
93
99
if (bind != null )
94
- ((DebugSubscriber <T >) o ).setTo (bind );
100
+ ((DebugSubscriber <T , C >) o ).setTo (bind );
95
101
return o ;
96
102
}
97
- return new DebugSubscriber <T >(onNextHook , events , o , null , bind );
103
+ return new DebugSubscriber <T , C >(onNextHook , start , complete , error , o , null , bind );
104
+ }
105
+
106
+ public final class OnCreateWrapper <T > implements OnSubscribe <T > {
107
+ private final OnSubscribe <T > f ;
108
+
109
+ private OnCreateWrapper (OnSubscribe <T > f ) {
110
+ this .f = f ;
111
+ }
112
+
113
+ @ Override
114
+ public void call (Subscriber <? super T > o ) {
115
+ f .call (wrapInbound (null , o ));
116
+ }
117
+
118
+ public OnSubscribe <T > getActual () {
119
+ return f ;
120
+ }
98
121
}
99
122
}
0 commit comments