commonMain.dev.bluefalcon.NativeFlow.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of blue-falcon Show documentation
Show all versions of blue-falcon Show documentation
Bluetooth Multiplatform Library
package dev.bluefalcon
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.plus
internal fun Flow.toNativeType(scope: CoroutineScope): NativeFlow = NativeFlow(this, scope)
class NativeFlow(private val origin: Flow, private val scope: CoroutineScope) : Flow by origin {
fun collect(block: (T) -> Unit) = onEach { block(it) }.launchIn(scope)
fun collectOnMain(block: (T) -> Unit) = onEach { block(it) }.launchIn(scope + Dispatchers.Main)
}