1
1
/**
2
2
* Copyright 2014 Netflix, Inc.
3
- *
3
+ *
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
6
6
* You may obtain a copy of the License at
7
- *
7
+ *
8
8
* http://www.apache.org/licenses/LICENSE-2.0
9
- *
9
+ *
10
10
* Unless required by applicable law or agreed to in writing, software
11
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
20
import rx .Scheduler ;
21
21
import rx .Subscription ;
22
22
import rx .functions .Action0 ;
23
- import rx .functions . Action1 ;
24
- import rx .subscriptions .BooleanSubscription ;
23
+ import rx .internal . schedulers . ScheduledAction ;
24
+ import rx .subscriptions .CompositeSubscription ;
25
25
import rx .subscriptions .Subscriptions ;
26
26
import android .os .Handler ;
27
27
@@ -34,7 +34,7 @@ public class HandlerThreadScheduler extends Scheduler {
34
34
35
35
/**
36
36
* Constructs a {@link HandlerThreadScheduler} using the given {@link Handler}
37
- *
37
+ *
38
38
* @param handler
39
39
* {@link Handler} to use when scheduling actions
40
40
*/
@@ -46,47 +46,42 @@ public HandlerThreadScheduler(Handler handler) {
46
46
public Worker createWorker () {
47
47
return new InnerHandlerThreadScheduler (handler );
48
48
}
49
-
49
+
50
50
private static class InnerHandlerThreadScheduler extends Worker {
51
51
52
52
private final Handler handler ;
53
- private BooleanSubscription innerSubscription = new BooleanSubscription ();
53
+
54
+ private final CompositeSubscription compositeSubscription = new CompositeSubscription ();
54
55
55
56
public InnerHandlerThreadScheduler (Handler handler ) {
56
57
this .handler = handler ;
57
58
}
58
59
59
60
@ Override
60
61
public void unsubscribe () {
61
- innerSubscription .unsubscribe ();
62
+ compositeSubscription .unsubscribe ();
62
63
}
63
64
64
65
@ Override
65
66
public boolean isUnsubscribed () {
66
- return innerSubscription .isUnsubscribed ();
67
+ return compositeSubscription .isUnsubscribed ();
67
68
}
68
69
69
70
@ Override
70
71
public Subscription schedule (final Action0 action , long delayTime , TimeUnit unit ) {
71
- final Runnable runnable = new Runnable () {
72
- @ Override
73
- public void run () {
74
- if (isUnsubscribed ()) {
75
- return ;
76
- }
77
- action .call ();
78
- }
79
- };
80
- handler .postDelayed (runnable , unit .toMillis (delayTime ));
81
- return Subscriptions .create (new Action0 () {
82
-
72
+ final ScheduledAction scheduledAction = new ScheduledAction (action );
73
+ scheduledAction .add (Subscriptions .create (new Action0 () {
83
74
@ Override
84
75
public void call () {
85
- handler .removeCallbacks (runnable );
86
-
76
+ handler .removeCallbacks (scheduledAction );
87
77
}
88
-
89
- });
78
+ }));
79
+ scheduledAction .addParent (compositeSubscription );
80
+ compositeSubscription .add (scheduledAction );
81
+
82
+ handler .postDelayed (scheduledAction , unit .toMillis (delayTime ));
83
+
84
+ return scheduledAction ;
90
85
}
91
86
92
87
@ Override
@@ -95,5 +90,4 @@ public Subscription schedule(final Action0 action) {
95
90
}
96
91
97
92
}
98
-
99
93
}
0 commit comments