Skip to content

Commit

Permalink
Update to Kotlin 1.9.21, coroutines 1.8.0-RC, and datetime 0.5.0
Browse files Browse the repository at this point in the history
This resolves a bug in kotlinx-coroutines that this
project exposed. See Kotlin/kotlinx.coroutines#3920
for more details.
  • Loading branch information
kevincianfarini committed Dec 11, 2023
1 parent ab6e16b commit 2d53b0f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 29 deletions.
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/tech/kraken/cardiologist/flow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import kotlin.time.Duration
public fun Clock.intervalPulse(interval: Duration): Flow<Pulse> = flow {
while (true) {
emit(Pulse(now()))
delay(interval.roundToMillis())
delay(interval)
}
}

Expand Down
18 changes: 3 additions & 15 deletions src/commonMain/kotlin/tech/kraken/cardiologist/suspend.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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) {
Expand All @@ -41,5 +29,5 @@ public suspend fun Clock.delayUntilNext(time: LocalTime, timeZone: TimeZone) {
}

val duration = futureInstant - now
delay(duration.roundToMillis())
delay(duration)
}
10 changes: 0 additions & 10 deletions src/commonTest/kotlin/tech/kraken/cardiologist/FlowTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}

0 comments on commit 2d53b0f

Please sign in to comment.