15
15
*/
16
16
package rx .internal .operators ;
17
17
18
- import rx .Observable ;
19
- import rx .Observable .Operator ;
20
- import rx .Producer ;
21
- import rx .Scheduler ;
18
+ import rx .*;
19
+ import rx .Observable .OnSubscribe ;
22
20
import rx .Scheduler .Worker ;
23
- import rx .Subscriber ;
24
21
import rx .functions .Action0 ;
25
22
26
23
/**
27
24
* Subscribes Observers on the specified {@code Scheduler}.
28
25
* <p>
29
26
* <img width="640" src="https://github.com/ReactiveX/RxJava/wiki/images/rx-operators/subscribeOn.png" alt="">
27
+ *
28
+ * @param <T> the value type of the actual source
30
29
*/
31
- public class OperatorSubscribeOn <T > implements Operator < T , Observable < T > > {
30
+ public final class OperatorSubscribeOn <T > implements OnSubscribe < T > {
32
31
33
- private final Scheduler scheduler ;
32
+ final Scheduler scheduler ;
33
+ final Observable <T > source ;
34
34
35
- public OperatorSubscribeOn (Scheduler scheduler ) {
35
+ public OperatorSubscribeOn (Observable < T > source , Scheduler scheduler ) {
36
36
this .scheduler = scheduler ;
37
+ this .source = source ;
37
38
}
38
39
39
40
@ Override
40
- public Subscriber <? super Observable < T >> call (final Subscriber <? super T > subscriber ) {
41
+ public void call (final Subscriber <? super T > subscriber ) {
41
42
final Worker inner = scheduler .createWorker ();
42
43
subscriber .add (inner );
43
- return new Subscriber <Observable <T >>(subscriber ) {
44
-
45
- @ Override
46
- public void onCompleted () {
47
- // ignore because this is a nested Observable and we expect only 1 Observable<T> emitted to onNext
48
- }
49
-
50
- @ Override
51
- public void onError (Throwable e ) {
52
- subscriber .onError (e );
53
- }
54
-
44
+
45
+ inner .schedule (new Action0 () {
55
46
@ Override
56
- public void onNext (final Observable <T > o ) {
57
- inner .schedule (new Action0 () {
58
-
47
+ public void call () {
48
+ final Thread t = Thread .currentThread ();
49
+
50
+ Subscriber <T > s = new Subscriber <T >(subscriber ) {
59
51
@ Override
60
- public void call () {
61
- final Thread t = Thread .currentThread ();
62
- o .unsafeSubscribe (new Subscriber <T >(subscriber ) {
63
-
64
- @ Override
65
- public void onCompleted () {
66
- subscriber .onCompleted ();
67
- }
68
-
69
- @ Override
70
- public void onError (Throwable e ) {
71
- subscriber .onError (e );
72
- }
73
-
74
- @ Override
75
- public void onNext (T t ) {
76
- subscriber .onNext (t );
77
- }
78
-
52
+ public void onNext (T t ) {
53
+ subscriber .onNext (t );
54
+ }
55
+
56
+ @ Override
57
+ public void onError (Throwable e ) {
58
+ try {
59
+ subscriber .onError (e );
60
+ } finally {
61
+ inner .unsubscribe ();
62
+ }
63
+ }
64
+
65
+ @ Override
66
+ public void onCompleted () {
67
+ try {
68
+ subscriber .onCompleted ();
69
+ } finally {
70
+ inner .unsubscribe ();
71
+ }
72
+ }
73
+
74
+ @ Override
75
+ public void setProducer (final Producer p ) {
76
+ subscriber .setProducer (new Producer () {
79
77
@ Override
80
- public void setProducer (final Producer producer ) {
81
- subscriber .setProducer (new Producer () {
82
-
83
- @ Override
84
- public void request (final long n ) {
85
- if (Thread .currentThread () == t ) {
86
- // don't schedule if we're already on the thread (primarily for first setProducer call)
87
- // see unit test 'testSetProducerSynchronousRequest' for more context on this
88
- producer .request (n );
89
- } else {
90
- inner .schedule (new Action0 () {
91
-
92
- @ Override
93
- public void call () {
94
- producer .request (n );
95
- }
96
- });
78
+ public void request (final long n ) {
79
+ if (t == Thread .currentThread ()) {
80
+ p .request (n );
81
+ } else {
82
+ inner .schedule (new Action0 () {
83
+ @ Override
84
+ public void call () {
85
+ p .request (n );
97
86
}
98
- }
99
-
100
- });
87
+ });
88
+ }
101
89
}
102
-
103
90
});
104
91
}
105
- });
92
+ };
93
+
94
+ source .unsafeSubscribe (s );
106
95
}
107
-
108
- };
96
+ });
109
97
}
110
- }
98
+ }
0 commit comments