1
1
package kotlinx.coroutines.experimental.reactor
2
2
3
- import kotlinx.coroutines.experimental.CancellableContinuation
4
- import kotlinx.coroutines.experimental.CoroutineDispatcher
5
- import kotlinx.coroutines.experimental.Delay
6
- import kotlinx.coroutines.experimental.DisposableHandle
7
- import kotlinx.coroutines.experimental.disposeOnCompletion
8
- import reactor.core.Cancellation
3
+ import kotlinx.coroutines.experimental.*
4
+ import reactor.core.Disposable
9
5
import reactor.core.scheduler.Scheduler
10
- import reactor.core.scheduler.TimedScheduler
11
6
import java.util.concurrent.TimeUnit
12
7
import kotlin.coroutines.experimental.CoroutineContext
13
8
@@ -16,45 +11,31 @@ import kotlin.coroutines.experimental.CoroutineContext
16
11
*/
17
12
fun Scheduler.asCoroutineDispatcher () = SchedulerCoroutineDispatcher (this )
18
13
19
- /* *
20
- * Converts an instance of [TimedScheduler] to an implementation of [CoroutineDispatcher]
21
- * and provides native [delay][Delay.delay] support.
22
- */
23
- fun TimedScheduler.asCoroutineDispatcher () = TimedSchedulerCoroutineDispatcher (this )
24
-
25
14
/* *
26
15
* Implements [CoroutineDispatcher] on top of an arbitrary [Scheduler].
27
16
* @param scheduler a scheduler.
28
17
*/
29
- open class SchedulerCoroutineDispatcher (private val scheduler : Scheduler ) : CoroutineDispatcher() {
18
+ open class SchedulerCoroutineDispatcher (private val scheduler : Scheduler ) : CoroutineDispatcher(), Delay {
30
19
override fun dispatch (context : CoroutineContext , block : Runnable ) {
31
20
scheduler.schedule(block)
32
21
}
33
22
34
- override fun toString (): String = scheduler.toString()
35
- override fun equals (other : Any? ): Boolean = other is SchedulerCoroutineDispatcher && other.scheduler == = scheduler
36
- override fun hashCode (): Int = System .identityHashCode(scheduler)
37
- }
38
-
39
- /* *
40
- * Implements [CoroutineDispatcher] on top of an arbitrary [TimedScheduler].
41
- * @param scheduler a timed scheduler.
42
- */
43
- open class TimedSchedulerCoroutineDispatcher (private val scheduler : TimedScheduler ) : SchedulerCoroutineDispatcher(scheduler), Delay {
44
-
45
23
override fun scheduleResumeAfterDelay (time : Long , unit : TimeUnit , continuation : CancellableContinuation <Unit >) {
46
24
val disposable = scheduler.schedule({
47
25
with (continuation) { resumeUndispatched(Unit ) }
48
26
}, time, unit)
49
-
50
27
continuation.disposeOnCompletion(disposable.asDisposableHandle())
51
28
}
52
29
53
30
override fun invokeOnTimeout (time : Long , unit : TimeUnit , block : Runnable ): DisposableHandle =
54
31
scheduler.schedule(block, time, unit).asDisposableHandle()
32
+
33
+ override fun toString (): String = scheduler.toString()
34
+ override fun equals (other : Any? ): Boolean = other is SchedulerCoroutineDispatcher && other.scheduler == = scheduler
35
+ override fun hashCode (): Int = System .identityHashCode(scheduler)
55
36
}
56
37
57
- private fun Cancellation .asDisposableHandle (): DisposableHandle =
58
- object : DisposableHandle {
59
- override fun dispose () = this @asDisposableHandle.dispose()
60
- }
38
+ private fun Disposable .asDisposableHandle (): DisposableHandle =
39
+ object : DisposableHandle {
40
+ override fun dispose () = this @asDisposableHandle.dispose()
41
+ }
0 commit comments