@@ -37,45 +37,45 @@ public fun Executor.toCoroutineDispatcher(): CoroutineDispatcher =
37
37
public fun Executor.asCoroutineDispatcher (): CoroutineDispatcher =
38
38
ExecutorCoroutineDispatcher (this )
39
39
40
- private class ExecutorCoroutineDispatcher (
41
- private val executor : Executor
42
- ) : CoroutineDispatcher(), Delay {
40
+ private class ExecutorCoroutineDispatcher (override val executor : Executor ) : ExecutorCoroutineDispatcherBase()
41
+
42
+ internal abstract class ExecutorCoroutineDispatcherBase : CoroutineDispatcher (), Delay {
43
+ abstract val executor: Executor
44
+
43
45
override fun dispatch (context : CoroutineContext , block : Runnable ) = executor.execute(block)
44
46
45
47
override fun scheduleResumeAfterDelay (time : Long , unit : TimeUnit , continuation : CancellableContinuation <Unit >) {
46
- val timeout = if (executor is ScheduledExecutorService )
47
- executor .schedule(ResumeUndispatchedRunnable (this , continuation), time, unit) else
48
- scheduledExecutor.schedule(ResumeRunnable (continuation), time, unit)
48
+ val timeout = (executor as ? ScheduledExecutorService )
49
+ ? .schedule(ResumeUndispatchedRunnable (this , continuation), time, unit)
50
+ ? : scheduledExecutor.schedule(ResumeRunnable (continuation), time, unit)
49
51
continuation.cancelFutureOnCompletion(timeout)
50
52
}
51
53
52
54
override fun invokeOnTimeout (time : Long , unit : TimeUnit , block : Runnable ): DisposableHandle {
53
- val timeout = if (executor is ScheduledExecutorService )
54
- executor .schedule(block, time, unit) else
55
- scheduledExecutor.schedule(block, time, unit)
55
+ val timeout = (executor as ? ScheduledExecutorService )
56
+ ? .schedule(block, time, unit)
57
+ ? : scheduledExecutor.schedule(block, time, unit)
56
58
return DisposableFutureHandle (timeout)
57
59
}
58
60
59
61
override fun toString (): String = executor.toString()
60
- override fun equals (other : Any? ): Boolean = other is ExecutorCoroutineDispatcher && other.executor == = executor
62
+ override fun equals (other : Any? ): Boolean = other is ExecutorCoroutineDispatcherBase && other.executor == = executor
61
63
override fun hashCode (): Int = System .identityHashCode(executor)
62
64
}
63
65
64
- // --- reusing these classes in other places ---
65
-
66
- internal class ResumeUndispatchedRunnable (
67
- private val dispatcher : CoroutineDispatcher ,
68
- private val continuation : CancellableContinuation <Unit >
66
+ internal class ResumeRunnable (
67
+ private val continuation : Continuation <Unit >
69
68
) : Runnable {
70
69
override fun run () {
71
- with ( continuation) { dispatcher.resumeUndispatched (Unit ) }
70
+ continuation.resume (Unit )
72
71
}
73
72
}
74
73
75
- internal class ResumeRunnable (
76
- private val continuation : Continuation <Unit >
74
+ private class ResumeUndispatchedRunnable (
75
+ private val dispatcher : CoroutineDispatcher ,
76
+ private val continuation : CancellableContinuation <Unit >
77
77
) : Runnable {
78
78
override fun run () {
79
- continuation.resume (Unit )
79
+ with ( continuation) { dispatcher.resumeUndispatched (Unit ) }
80
80
}
81
81
}
0 commit comments