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

jsAndWasmSharedMain.flow.internal.SafeCollector.kt Maven / Gradle / Ivy

package kotlinx.coroutines.flow.internal

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlin.coroutines.*

internal actual class SafeCollector actual constructor(
    internal actual val collector: FlowCollector,
    internal actual val collectContext: CoroutineContext
) : FlowCollector {

    // Note, it is non-capturing lambda, so no extra allocation during init of SafeCollector
    internal actual val collectContextSize = collectContext.fold(0) { count, _ -> count + 1 }
    private var lastEmissionContext: CoroutineContext? = null

    actual override suspend fun emit(value: T) {
        val currentContext = currentCoroutineContext()
        currentContext.ensureActive()
        if (lastEmissionContext !== currentContext) {
            checkContext(currentContext)
            lastEmissionContext = currentContext
        }
        collector.emit(value)
    }

    public actual fun releaseIntercepted() {
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy