@@ -47,7 +47,6 @@ internal sealed class SetTimeoutBasedDispatcher: CoroutineDispatcher(), Delay {
47
47
48
48
override fun scheduleResumeAfterDelay (timeMillis : Long , continuation : CancellableContinuation <Unit >) {
49
49
val handle = setTimeout({ with (continuation) { resumeUndispatched(Unit ) } }, delayToInt(timeMillis))
50
- // Actually on cancellation, but clearTimeout is idempotent
51
50
continuation.invokeOnCancellation(handler = ClearTimeout (handle).asHandler)
52
51
}
53
52
}
@@ -64,7 +63,7 @@ internal object SetTimeoutDispatcher : SetTimeoutBasedDispatcher() {
64
63
}
65
64
}
66
65
67
- private class ClearTimeout (private val handle : Int ) : CancelHandler(), DisposableHandle {
66
+ private open class ClearTimeout (protected val handle : Int ) : CancelHandler(), DisposableHandle {
68
67
69
68
override fun dispose () {
70
69
clearTimeout(handle)
@@ -83,15 +82,18 @@ internal class WindowDispatcher(private val window: Window) : CoroutineDispatche
83
82
override fun dispatch (context : CoroutineContext , block : Runnable ) = queue.enqueue(block)
84
83
85
84
override fun scheduleResumeAfterDelay (timeMillis : Long , continuation : CancellableContinuation <Unit >) {
86
- window.setTimeout({ with (continuation) { resumeUndispatched(Unit ) } }, delayToInt(timeMillis))
85
+ val handle = window.setTimeout({ with (continuation) { resumeUndispatched(Unit ) } }, delayToInt(timeMillis))
86
+ continuation.invokeOnCancellation(handler = WindowClearTimeout (handle).asHandler)
87
87
}
88
88
89
89
override fun invokeOnTimeout (timeMillis : Long , block : Runnable , context : CoroutineContext ): DisposableHandle {
90
90
val handle = window.setTimeout({ block.run () }, delayToInt(timeMillis))
91
- return object : DisposableHandle {
92
- override fun dispose () {
93
- window.clearTimeout(handle)
94
- }
91
+ return WindowClearTimeout (handle)
92
+ }
93
+
94
+ private inner class WindowClearTimeout (handle : Int ) : ClearTimeout(handle) {
95
+ override fun dispose () {
96
+ window.clearTimeout(handle)
95
97
}
96
98
}
97
99
}
0 commit comments