5
5
package kotlinx.coroutines
6
6
7
7
import kotlinx.coroutines.internal.*
8
- import org.w3c.dom.*
9
8
import kotlin.coroutines.*
10
9
10
+ public expect abstract class W3CWindow
11
+ internal expect fun w3cSetTimeout (window : W3CWindow , handler : () -> Unit , timeout : Int ): Int
12
+ internal expect fun w3cSetTimeout (handler : () -> Unit , timeout : Int ): Int
13
+ internal expect fun w3cClearTimeout (handle : Int )
14
+ internal expect fun w3cClearTimeout (window : W3CWindow , handle : Int )
15
+
16
+ internal expect class ScheduledMessageQueue (dispatcher : SetTimeoutBasedDispatcher ) : MessageQueue {
17
+ override fun schedule ()
18
+ override fun reschedule ()
19
+ internal fun setTimeout (timeout : Int )
20
+ }
21
+
22
+ internal expect class WindowMessageQueue (window : W3CWindow ) : MessageQueue {
23
+ override fun schedule ()
24
+ override fun reschedule ()
25
+ }
26
+
11
27
private const val MAX_DELAY = Int .MAX_VALUE .toLong()
12
28
13
29
private fun delayToInt (timeMillis : Long ): Int =
14
30
timeMillis.coerceIn(0 , MAX_DELAY ).toInt()
15
31
16
- internal sealed class SetTimeoutBasedDispatcher : CoroutineDispatcher (), Delay {
17
- val messageQueue = ScheduledMessageQueue (this )
32
+ internal abstract class SetTimeoutBasedDispatcher : CoroutineDispatcher (), Delay {
33
+ internal val messageQueue = ScheduledMessageQueue (this )
18
34
19
35
abstract fun scheduleQueueProcessing ()
20
36
@@ -28,48 +44,47 @@ internal sealed class SetTimeoutBasedDispatcher: CoroutineDispatcher(), Delay {
28
44
}
29
45
30
46
override fun invokeOnTimeout (timeMillis : Long , block : Runnable , context : CoroutineContext ): DisposableHandle {
31
- val handle = setTimeout ({ block.run () }, delayToInt(timeMillis))
47
+ val handle = w3cSetTimeout ({ block.run () }, delayToInt(timeMillis))
32
48
return ClearTimeout (handle)
33
49
}
34
50
35
51
override fun scheduleResumeAfterDelay (timeMillis : Long , continuation : CancellableContinuation <Unit >) {
36
- val handle = setTimeout ({ with (continuation) { resumeUndispatched(Unit ) } }, delayToInt(timeMillis))
52
+ val handle = w3cSetTimeout ({ with (continuation) { resumeUndispatched(Unit ) } }, delayToInt(timeMillis))
37
53
continuation.invokeOnCancellation(handler = ClearTimeout (handle).asHandler)
38
54
}
39
55
}
40
56
41
- internal class WindowDispatcher (private val window : Window ) : CoroutineDispatcher(), Delay {
57
+ internal class WindowDispatcher (private val window : W3CWindow ) : CoroutineDispatcher(), Delay {
42
58
private val queue = WindowMessageQueue (window)
43
59
44
60
override fun dispatch (context : CoroutineContext , block : Runnable ) = queue.enqueue(block)
45
61
46
62
override fun scheduleResumeAfterDelay (timeMillis : Long , continuation : CancellableContinuation <Unit >) {
47
- val handle = setTimeout (window, { with (continuation) { resumeUndispatched(Unit ) } }, delayToInt(timeMillis))
63
+ val handle = w3cSetTimeout (window, { with (continuation) { resumeUndispatched(Unit ) } }, delayToInt(timeMillis))
48
64
continuation.invokeOnCancellation(handler = WindowClearTimeout (handle).asHandler)
49
65
}
50
66
51
67
override fun invokeOnTimeout (timeMillis : Long , block : Runnable , context : CoroutineContext ): DisposableHandle {
52
- val handle = setTimeout (window, block::run, delayToInt(timeMillis))
68
+ val handle = w3cSetTimeout (window, block::run, delayToInt(timeMillis))
53
69
return WindowClearTimeout (handle)
54
70
}
55
71
56
72
private inner class WindowClearTimeout (handle : Int ) : ClearTimeout(handle) {
57
73
override fun dispose () {
58
- window.clearTimeout( handle)
74
+ w3cClearTimeout(window, handle)
59
75
}
60
76
}
61
77
}
62
78
63
79
internal object SetTimeoutDispatcher : SetTimeoutBasedDispatcher() {
64
80
override fun scheduleQueueProcessing () {
65
- setTimeout( messageQueue.processQueue, 0 )
81
+ messageQueue.setTimeout( 0 )
66
82
}
67
83
}
68
84
69
85
private open class ClearTimeout (protected val handle : Int ) : CancelHandler(), DisposableHandle {
70
-
71
86
override fun dispose () {
72
- clearTimeout (handle)
87
+ w3cClearTimeout (handle)
73
88
}
74
89
75
90
override fun invoke (cause : Throwable ? ) {
0 commit comments