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

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

Go to download

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

The newest version!
package ovh.plrapps.mapcompose.core

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch

/**
 * So long as the returned [SendChannel] receives [T] elements, the provided [block] function isn't
 * executed until a time-span of [timeoutMillis] elapses.
 * When [block] is executed, it's provided with the last [T] value sent to the channel.
 */
fun  CoroutineScope.debounce(
    timeoutMillis: Long,
    block: suspend (T) -> Unit
): SendChannel {
    val channel = Channel(capacity = Channel.CONFLATED)
    val flow = channel.receiveAsFlow().debounce(timeoutMillis)
    launch {
        flow.collect {
            block(it)
        }
    }

    return channel
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy