commonMain.ovh.plrapps.mapcompose.utils.Flow.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
The newest version!
package ovh.plrapps.mapcompose.utils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import ovh.plrapps.mapcompose.utils.map
fun Flow.throttle(wait: Long) = channelFlow {
val channel = Channel(capacity = Channel.CONFLATED)
coroutineScope {
launch {
collect {
channel.send(it)
}
}
launch {
for (e in channel) {
send(e)
delay(wait)
}
}
}
}
fun StateFlow.map(
coroutineScope : CoroutineScope,
mapper : (value : T) -> M
) : StateFlow = map { mapper(it) }.stateIn(
coroutineScope,
SharingStarted.Eagerly,
mapper(value)
)