From 2d53b0f771565cb83819dc008e5a2259316b4fce Mon Sep 17 00:00:00 2001 From: Kevin Cianfarini Date: Sun, 10 Dec 2023 23:48:18 -0500 Subject: [PATCH] Update to Kotlin 1.9.21, coroutines 1.8.0-RC, and datetime 0.5.0 This resolves a bug in kotlinx-coroutines that this project exposed. See https://github.com/Kotlin/kotlinx.coroutines/issues/3920 for more details. --- gradle/libs.versions.toml | 6 +++--- .../kotlin/tech/kraken/cardiologist/flow.kt | 2 +- .../kotlin/tech/kraken/cardiologist/suspend.kt | 18 +++--------------- .../tech/kraken/cardiologist/FlowTests.kt | 10 ---------- 4 files changed, 7 insertions(+), 29 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6f086b8..2289b22 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,7 +1,7 @@ [versions] -kotlin = "1.9.20" -kotlinx-coroutines = "1.7.3" -kotlinx-datetime = "0.4.1" +kotlin = "1.9.21" +kotlinx-coroutines = "1.8.0-RC" +kotlinx-datetime = "0.5.0" turbine = "1.0.0" [libraries] diff --git a/src/commonMain/kotlin/tech/kraken/cardiologist/flow.kt b/src/commonMain/kotlin/tech/kraken/cardiologist/flow.kt index e2d1e87..4dd8bb2 100644 --- a/src/commonMain/kotlin/tech/kraken/cardiologist/flow.kt +++ b/src/commonMain/kotlin/tech/kraken/cardiologist/flow.kt @@ -18,7 +18,7 @@ import kotlin.time.Duration public fun Clock.intervalPulse(interval: Duration): Flow = flow { while (true) { emit(Pulse(now())) - delay(interval.roundToMillis()) + delay(interval) } } diff --git a/src/commonMain/kotlin/tech/kraken/cardiologist/suspend.kt b/src/commonMain/kotlin/tech/kraken/cardiologist/suspend.kt index d2311bb..b4f9579 100644 --- a/src/commonMain/kotlin/tech/kraken/cardiologist/suspend.kt +++ b/src/commonMain/kotlin/tech/kraken/cardiologist/suspend.kt @@ -2,24 +2,12 @@ package tech.kraken.cardiologist import kotlinx.coroutines.delay import kotlinx.datetime.* -import kotlin.time.Duration public suspend fun Clock.delayUntil(instant: Instant) { val duration = instant - now() - delay(duration.roundToMillis()) + delay(duration) } -/** - * We don't use duration based [delay] from kotlinx.coroutines because it has a bug - * that loses nanosecond granularity when rounding to milliseconds. - * - * See: https://github.com/Kotlin/kotlinx.coroutines/issues/3920 - */ -internal fun Duration.roundToMillis(): Long = if (this > Duration.ZERO) { - val millis = inWholeMilliseconds - if (millis * 1_000_000 < inWholeNanoseconds) millis + 1 else millis -} else 0 - public suspend fun Clock.delayUntil(dateTime: LocalDateTime, timeZone: TimeZone) { delayUntil(instant = dateTime.toInstant(timeZone)) } @@ -28,7 +16,7 @@ public suspend fun Clock.delayFor(period: DateTimePeriod, timeZone: TimeZone) { val now = now() val futureInstant = now.plus(period, timeZone) val duration = futureInstant - now - delay(duration.roundToMillis()) + delay(duration) } public suspend fun Clock.delayUntilNext(time: LocalTime, timeZone: TimeZone) { @@ -41,5 +29,5 @@ public suspend fun Clock.delayUntilNext(time: LocalTime, timeZone: TimeZone) { } val duration = futureInstant - now - delay(duration.roundToMillis()) + delay(duration) } \ No newline at end of file diff --git a/src/commonTest/kotlin/tech/kraken/cardiologist/FlowTests.kt b/src/commonTest/kotlin/tech/kraken/cardiologist/FlowTests.kt index 14f2823..388e476 100644 --- a/src/commonTest/kotlin/tech/kraken/cardiologist/FlowTests.kt +++ b/src/commonTest/kotlin/tech/kraken/cardiologist/FlowTests.kt @@ -59,14 +59,4 @@ class FlowTests { ) } } - - @Test fun foo() = runTest(timeout = 1.days) { - withContext(Dispatchers.Default) { - val tz = TimeZone.of("Europe/London") - val flow = Clock.System.schedulePulse(tz) - flow.beat(mode = RecurringJobMode.Concurrent) { instant -> - println(instant) - } - } - } } \ No newline at end of file