20
20
import java .util .concurrent .atomic .AtomicReference ;
21
21
22
22
import rx .Completable ;
23
+ import rx .Completable .CompletableOnSubscribe ;
24
+ import rx .Completable .CompletableSubscriber ;
23
25
import rx .Observable ;
24
26
import rx .Scheduler ;
25
27
import rx .Subscription ;
26
28
import rx .annotations .Experimental ;
27
29
import rx .functions .Action0 ;
28
- import rx .functions .Action1 ;
29
30
import rx .functions .Func1 ;
30
31
import rx .internal .operators .BufferUntilSubscriber ;
31
32
import rx .subjects .PublishSubject ;
74
75
@ Experimental
75
76
public class ScheduleWhen extends Scheduler {
76
77
private final Scheduler actualScheduler ;
77
- private final PublishSubject <Completable > workerQueue ;
78
+ private final PublishSubject <Observable < Completable > > workerQueue ;
78
79
79
- public ScheduleWhen (Func1 <Observable <Completable >, Completable > combine , Scheduler actualScheduler ) {
80
+ public ScheduleWhen (Func1 <Observable <Observable < Completable > >, Completable > combine , Scheduler actualScheduler ) {
80
81
this .actualScheduler = actualScheduler ;
81
82
// workers are converted into completables and put in this queue.
82
83
this .workerQueue = PublishSubject .create ();
@@ -87,18 +88,24 @@ public ScheduleWhen(Func1<Observable<Completable>, Completable> combine, Schedul
87
88
88
89
@ Override
89
90
public Worker createWorker () {
91
+ final Worker actualWorker = actualScheduler .createWorker ();
90
92
// a queue for the actions submitted while worker is waiting to get to
91
93
// the subscribe to off the workerQueue.
92
- final BufferUntilSubscriber <ScheduledAction > actionQueue = BufferUntilSubscriber .create ();
93
- final Worker actualWorker = actualScheduler .createWorker ();
94
-
94
+ final BufferUntilSubscriber <ScheduledAction > actionQueue = BufferUntilSubscriber .<ScheduledAction >create ();
95
95
// convert the work of scheduling all the actions into a completable
96
- Completable completable = actionQueue .doOnNext (new Action1 <ScheduledAction >() {
96
+ Observable < Completable > actions = actionQueue .map (new Func1 <ScheduledAction , Completable >() {
97
97
@ Override
98
- public void call (ScheduledAction action ) {
99
- action .call (actualWorker );
98
+ public Completable call (final ScheduledAction action ) {
99
+ return Completable .create (new CompletableOnSubscribe () {
100
+ @ Override
101
+ public void call (CompletableSubscriber actionCompletable ) {
102
+ actionCompletable .onSubscribe (action );
103
+ action .call (actualWorker );
104
+ actionCompletable .onCompleted ();
105
+ }
106
+ });
100
107
}
101
- }). toCompletable () ;
108
+ });
102
109
103
110
// a worker that queues the action to the actionQueue subject.
104
111
Worker worker = new Worker () {
@@ -137,7 +144,7 @@ public Subscription schedule(final Action0 action) {
137
144
};
138
145
139
146
// enqueue the completable that process actions put in reply subject
140
- workerQueue .onNext (completable );
147
+ workerQueue .onNext (actions );
141
148
142
149
// return the worker that adds actions to the reply subject
143
150
return worker ;
0 commit comments