@@ -35,38 +35,35 @@ public fun CoroutineDispatcher.asScheduler(): Scheduler =
35
35
36
36
private class DispatcherScheduler (internal val dispatcher : CoroutineDispatcher ) : Scheduler() {
37
37
38
- val job = SupervisorJob ()
39
- private val scope = CoroutineScope (job)
38
+ private val job = SupervisorJob ()
39
+ private val scope = CoroutineScope (job + dispatcher )
40
40
41
- override fun scheduleDirect (run : Runnable ): Disposable {
41
+ override fun scheduleDirect (block : Runnable ): Disposable {
42
42
if (scope.isActive) {
43
43
return scope.launch {
44
- dispatchBlock(run )
44
+ block.runWithRx( )
45
45
}.asDisposable()
46
46
}
47
47
48
48
return Disposables .disposed()
49
49
}
50
50
51
- override fun scheduleDirect (run : Runnable , delay : Long , unit : TimeUnit ): Disposable {
51
+ override fun scheduleDirect (block : Runnable , delay : Long , unit : TimeUnit ): Disposable {
52
52
if (delay <= 0 ) {
53
- return scheduleDirect(run )
53
+ return scheduleDirect(block )
54
54
}
55
55
56
56
if (scope.isActive) {
57
57
return scope.launch {
58
58
delay(unit.toMillis(delay))
59
- dispatchBlock(run )
59
+ block.runWithRx( )
60
60
}.asDisposable()
61
61
}
62
62
63
63
return Disposables .disposed()
64
64
}
65
65
66
- private fun dispatchBlock (block : Runnable ) {
67
- val decoratedRun = RxJavaPlugins .onSchedule(block)
68
- dispatcher.dispatch(EmptyCoroutineContext , decoratedRun)
69
- }
66
+ private fun Runnable.runWithRx () = RxJavaPlugins .onSchedule(this ).run ()
70
67
71
68
override fun createWorker (): Worker =
72
69
DispatcherWorker (dispatcher, job)
@@ -86,7 +83,7 @@ private class DispatcherScheduler(internal val dispatcher: CoroutineDispatcher)
86
83
override fun schedule (block : Runnable ): Disposable {
87
84
startProcessingQueue()
88
85
if (workerScope.isActive) {
89
- val job = workerScope.launch(Dispatchers . Unconfined , CoroutineStart .LAZY ) {
86
+ val job = workerScope.launch(start = CoroutineStart .LAZY ) {
90
87
block.run ()
91
88
}
92
89
blockChannel.offer(job)
@@ -169,7 +166,12 @@ public class SchedulerCoroutineDispatcher(
169
166
override fun hashCode (): Int = System .identityHashCode(scheduler)
170
167
}
171
168
172
- private fun Job.asDisposable (): Disposable = object : Disposable {
173
- override fun isDisposed (): Boolean = ! isActive
174
- override fun dispose () = cancel()
169
+ private class JobDisposable (private val job : Job ) : Disposable {
170
+ override fun isDisposed (): Boolean = ! job.isActive
171
+
172
+ override fun dispose () {
173
+ job.cancel()
174
+ }
175
175
}
176
+
177
+ public fun Job.asDisposable (): Disposable = JobDisposable (this )
0 commit comments