![JAR search and dependency download from the Maven repository](/logo.png)
commonMain.ovh.plrapps.mapcompose.core.Throttle.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mapcompose-mp-desktop Show documentation
Show all versions of mapcompose-mp-desktop Show documentation
A Compose Multiplatform library to display tiled maps, with support for markers, paths, and rotation
package ovh.plrapps.mapcompose.core
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
/**
* Limit the rate at which a [block] is called.
* The [block] execution is triggered upon reception of [Unit] from the returned [SendChannel].
*
* @param wait The time in ms between each [block] call.
*
* @author P.Laurence
*/
fun CoroutineScope.throttle(wait: Long, block: suspend () -> Unit): SendChannel {
val channel = Channel(capacity = Channel.CONFLATED)
val flow = channel.receiveAsFlow()
launch {
flow.collect {
block()
delay(wait)
}
}
return channel
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy