ursus
03/06/2022, 9:59 PMsuspend fun <T> ???(millis: Long, action: suspend () -> T): T {
val start = System.currentMillis()
val value = action()
val end = System.currentMillis()
val took = end - start
if (took < millis) {
delay(millis - took)
}
return value
}
does this have a name already? it makes sure the suspend function takes atleast N millis, so its not too fast
its not debounce since its calcualtes the delay …so I guess I need my own nameRichard Gomez
03/06/2022, 10:03 PMrunForAtLeast(1000) { }
ursus
03/06/2022, 10:20 PMephemient
03/06/2022, 10:52 PMcoroutineScope {
launch {
delay(millis)
}
action()
}
ursus
03/06/2022, 11:04 PMRichard Gomez
03/06/2022, 11:09 PMcoroutineScope
will wait for all child coroutines to finish before it returns, so if action()
completes but delay()
is still running, it will wait.
This function returns as soon as the given block and all its children coroutines are completedhttps://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/coroutine-scope.html
ursus
03/06/2022, 11:10 PMMichael Marshall
03/09/2022, 5:01 AMaction()
outside the launch
block, any side effects from action
will take place before the delay.ephemient
03/09/2022, 5:58 AM