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

commonMain.flow.internal.Merge.kt Maven / Gradle / Ivy

There is a newer version: 1.3.8
Show newest version
/*
 * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.coroutines.flow.internal

import kotlinx.coroutines.*
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.sync.*
import kotlin.coroutines.*

internal class ChannelFlowTransformLatest(
    private val transform: suspend FlowCollector.(value: T) -> Unit,
    flow: Flow,
    context: CoroutineContext = EmptyCoroutineContext,
    capacity: Int = Channel.BUFFERED
) : ChannelFlowOperator(flow, context, capacity) {
    override fun create(context: CoroutineContext, capacity: Int): ChannelFlow =
        ChannelFlowTransformLatest(transform, flow, context, capacity)

    override suspend fun flowCollect(collector: FlowCollector) {
        assert { collector is SendingCollector } // So cancellation behaviour is not leaking into the downstream
        flowScope {
            var previousFlow: Job? = null
            flow.collect { value ->
                previousFlow?.apply {
                    cancel(ChildCancelledException())
                    join()
                }
                // Do not pay for dispatch here, it's never necessary
                previousFlow = launch(start = CoroutineStart.UNDISPATCHED) {
                    collector.transform(value)
                }
            }
        }
    }
}

internal class ChannelFlowMerge(
    private val flow: Flow>,
    private val concurrency: Int,
    context: CoroutineContext = EmptyCoroutineContext,
    capacity: Int = Channel.BUFFERED
) : ChannelFlow(context, capacity) {
    override fun create(context: CoroutineContext, capacity: Int): ChannelFlow =
        ChannelFlowMerge(flow, concurrency, context, capacity)

    override fun produceImpl(scope: CoroutineScope): ReceiveChannel {
        return scope.flowProduce(context, capacity, block = collectToFun)
    }

    override suspend fun collectTo(scope: ProducerScope) {
        val semaphore = Semaphore(concurrency)
        val collector = SendingCollector(scope)
        val job: Job? = coroutineContext[Job]
        flow.collect { inner ->
            /*
             * We launch a coroutine on each emitted element and the only potential
             * suspension point in this collector is `semaphore.acquire` that rarely suspends,
             * so we manually check for cancellation to propagate it to the upstream in time.
             */
            job?.ensureActive()
            semaphore.acquire()
            scope.launch {
                try {
                    inner.collect(collector)
                } finally {
                    semaphore.release() // Release concurrency permit
                }
            }
        }
    }

    override fun additionalToStringProps(): String =
        "concurrency=$concurrency, "
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy