Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Use Kotlin's Duration #48

Merged
merged 3 commits into from
Mar 8, 2022
Merged

Use Kotlin's Duration #48

merged 3 commits into from
Mar 8, 2022

Conversation

Goooler
Copy link
Owner

@Goooler Goooler commented Mar 8, 2022

No description provided.

Comment on lines -14 to -26
inline val Int.nanoseconds: Duration get() = Duration.ofNanos(toLong())

inline val Int.microseconds: Duration get() = Duration.ofNanos(toLong() * 1000L)

inline val Int.milliseconds: Duration get() = Duration.ofMillis(toLong())

inline val Int.seconds: Duration get() = Duration.ofSeconds(toLong())

inline val Int.minutes: Duration get() = Duration.ofMinutes(toLong())

inline val Int.hours: Duration get() = Duration.ofHours(toLong())

inline val Int.days: Period get() = Period.ofDays(this)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +48 to +53
timeout: Duration = 30.seconds,
callback: (CountDownState) -> Unit = {}
) {
val timeEnd = Second(0)
countdownJob = viewModelScope.launch {
flow {
(countDownTime.value downTo timeEnd.value).forEach {
(timeout.inWholeSeconds downTo Duration.ZERO.inWholeSeconds).forEach {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Goooler
Copy link
Owner Author

Goooler commented Mar 8, 2022

public suspend fun <T> withTimeout(timeMillis: Long, block: suspend CoroutineScope.() -> T): T {
    contract {
        callsInPlace(block, InvocationKind.EXACTLY_ONCE)
    }
    if (timeMillis <= 0L) throw TimeoutCancellationException("Timed out immediately")
    return suspendCoroutineUninterceptedOrReturn { uCont ->
        setupTimeout(TimeoutCoroutine(timeMillis, uCont), block)
    }
}

@Goooler Goooler merged commit d2496df into trunk Mar 8, 2022
@Goooler Goooler deleted the time_unit branch March 8, 2022 03:25
@Goooler Goooler added the enhancement New feature or request label Mar 8, 2022
@Goooler Goooler added this to the 1.6.0 milestone Mar 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant