From d46e84b2f9ff1d4764d8ca71f6f5d51a6456a04c Mon Sep 17 00:00:00 2001 From: Vsevolod Tolstopyatov Date: Thu, 25 Nov 2021 16:54:44 +0300 Subject: [PATCH] Make FlowCollector fun interface, remove redundant extensions (#3047) * It also saves us from the copy-without-imports problem Addresses #3037 --- .../api/kotlinx-coroutines-core.api | 2 -- .../common/src/flow/FlowCollector.kt | 19 ++++++++-- .../common/src/flow/terminal/Collect.kt | 36 ------------------- 3 files changed, 17 insertions(+), 40 deletions(-) diff --git a/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api b/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api index ee4d8bfc09..7b63e7bd70 100644 --- a/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api +++ b/kotlinx-coroutines-core/api/kotlinx-coroutines-core.api @@ -919,8 +919,6 @@ public final class kotlinx/coroutines/flow/FlowKt { public static final fun catch (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function3;)Lkotlinx/coroutines/flow/Flow; public static final fun channelFlow (Lkotlin/jvm/functions/Function2;)Lkotlinx/coroutines/flow/Flow; public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final fun collect (Lkotlinx/coroutines/flow/SharedFlow;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final fun collectIndexed (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final fun collectLatest (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static final fun combine (Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function3;)Lkotlinx/coroutines/flow/Flow; diff --git a/kotlinx-coroutines-core/common/src/flow/FlowCollector.kt b/kotlinx-coroutines-core/common/src/flow/FlowCollector.kt index d1c1565cb0..2877fe55e7 100644 --- a/kotlinx-coroutines-core/common/src/flow/FlowCollector.kt +++ b/kotlinx-coroutines-core/common/src/flow/FlowCollector.kt @@ -8,10 +8,25 @@ package kotlinx.coroutines.flow * [FlowCollector] is used as an intermediate or a terminal collector of the flow and represents * an entity that accepts values emitted by the [Flow]. * - * This interface should usually not be implemented directly, but rather used as a receiver in a [flow] builder when implementing a custom operator. + * This interface should usually not be implemented directly, but rather used as a receiver in a [flow] builder when implementing a custom operator, + * or with SAM-conversion. * Implementations of this interface are not thread-safe. + * + * Example of usage: + * + * ``` + * val flow = getMyEvents() + * try { + * flow.collect { value -> + * println("Received $value") + * } + * println("My events are consumed successfully") + * } catch (e: Throwable) { + * println("Exception from the flow: $e") + * } + * ``` */ -public interface FlowCollector { +public fun interface FlowCollector { /** * Collects the value emitted by the upstream. diff --git a/kotlinx-coroutines-core/common/src/flow/terminal/Collect.kt b/kotlinx-coroutines-core/common/src/flow/terminal/Collect.kt index 91d410b9a2..df0c5fd4b3 100644 --- a/kotlinx-coroutines-core/common/src/flow/terminal/Collect.kt +++ b/kotlinx-coroutines-core/common/src/flow/terminal/Collect.kt @@ -50,42 +50,6 @@ public fun Flow.launchIn(scope: CoroutineScope): Job = scope.launch { collect() // tail-call } -/** - * Terminal flow operator that collects the given flow with a provided [action]. - * If any exception occurs during collect or in the provided flow, this exception is rethrown from this method. - * - * Example of use: - * - * ``` - * val flow = getMyEvents() - * try { - * flow.collect { value -> - * println("Received $value") - * } - * println("My events are consumed successfully") - * } catch (e: Throwable) { - * println("Exception from the flow: $e") - * } - * ``` - */ -public suspend inline fun Flow.collect(crossinline action: suspend (value: T) -> Unit): Unit = - collect(object : FlowCollector { - override suspend fun emit(value: T) = action(value) - }) - -/** - * Terminal flow operator that collects the given [SharedFlow] with the provided [action]. - * If any exception occurs during `collect` or in the provided flow, this exception is rethrown from this method. - * - * This is a counterpart of a regular [Flow.collect] extension, only different in the return type - * so that any code below `collect` produces a compilation warning. - */ -public suspend inline fun SharedFlow.collect(crossinline action: suspend (value: T) -> Unit): Nothing { - collect(object : FlowCollector { - override suspend fun emit(value: T) = action(value) - }) -} - /** * Terminal flow operator that collects the given flow with a provided [action] that takes the index of an element (zero-based) and the element. * If any exception occurs during collect or in the provided flow, this exception is rethrown from this method.