4
4
package kotlinx.coroutines.time
5
5
6
6
import kotlinx.coroutines.CoroutineScope
7
+ import kotlinx.coroutines.coroutineScope
7
8
import kotlinx.coroutines.selects.SelectBuilder
8
9
import java.time.Duration
9
- import java.util.concurrent.TimeUnit
10
+ import kotlin.coroutines.suspendCoroutine
10
11
11
12
/* *
12
13
* "java.time" adapter method for [kotlinx.coroutines.delay]
13
14
*/
14
- public suspend fun delay (duration : Duration ) =
15
- kotlinx.coroutines.delay(duration.toMillis())
15
+ public suspend fun delay (duration : Duration ) {
16
+ if (duration.seconds < Long .MAX_VALUE / 1_000L ) kotlinx.coroutines.delay(duration.toMillis())
17
+ else suspendCoroutine<Unit > { }
18
+ }
16
19
17
20
/* *
18
21
* "java.time" adapter method for [SelectBuilder.onTimeout]
19
22
*/
20
- public fun <R > SelectBuilder<R>.onTimeout (duration : Duration , block : suspend () -> R ) =
21
- onTimeout(duration.toMillis(), block)
23
+ public fun <R > SelectBuilder<R>.onTimeout (duration : Duration , block : suspend () -> R ) {
24
+ if (duration.seconds < Long .MAX_VALUE / 1_000L ) onTimeout(duration.toMillis(), block)
25
+ }
22
26
23
27
/* *
24
28
* "java.time" adapter method for [kotlinx.coroutines.withTimeout]
25
29
*/
26
30
public suspend fun <T > withTimeout (duration : Duration , block : suspend CoroutineScope .() -> T ): T =
27
- kotlinx.coroutines.withTimeout(duration.toMillis(), block)
31
+ if (duration.seconds < Long .MAX_VALUE / 1_000L ) kotlinx.coroutines.withTimeout(duration.toMillis(), block)
32
+ else coroutineScope { block() }
28
33
29
34
/* *
30
35
* "java.time" adapter method for [kotlinx.coroutines.withTimeoutOrNull]
31
36
*/
32
37
public suspend fun <T > withTimeoutOrNull (duration : Duration , block : suspend CoroutineScope .() -> T ): T ? =
33
- kotlinx.coroutines.withTimeoutOrNull(duration.toMillis(), block)
38
+ if (duration.seconds < Long .MAX_VALUE / 1_000L ) kotlinx.coroutines.withTimeoutOrNull(duration.toMillis(), block)
39
+ else coroutineScope { block() }
0 commit comments