All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.ovh.plrapps.mapcompose.core.Throttle.kt Maven / Gradle / Ivy

Go to download

A Compose Multiplatform library to display tiled maps, with support for markers, paths, and rotation

There is a newer version: 0.9.4
Show newest version
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