Skip to content

Commit

Permalink
Make FlowCollector fun interface, remove redundant extensions (Kotlin…
Browse files Browse the repository at this point in the history
…#3047)

* It also saves us from the copy-without-imports problem

Addresses Kotlin#3037
  • Loading branch information
qwwdfsad authored and yorickhenning committed Jan 28, 2022
1 parent 7160e01 commit d46e84b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 40 deletions.
2 changes: 0 additions & 2 deletions kotlinx-coroutines-core/api/kotlinx-coroutines-core.api
Expand Up @@ -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;
Expand Down
19 changes: 17 additions & 2 deletions kotlinx-coroutines-core/common/src/flow/FlowCollector.kt
Expand Up @@ -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<in T> {
public fun interface FlowCollector<in T> {

/**
* Collects the value emitted by the upstream.
Expand Down
36 changes: 0 additions & 36 deletions kotlinx-coroutines-core/common/src/flow/terminal/Collect.kt
Expand Up @@ -50,42 +50,6 @@ public fun <T> Flow<T>.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 <T> Flow<T>.collect(crossinline action: suspend (value: T) -> Unit): Unit =
collect(object : FlowCollector<T> {
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 <T> SharedFlow<T>.collect(crossinline action: suspend (value: T) -> Unit): Nothing {
collect(object : FlowCollector<T> {
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.
Expand Down

0 comments on commit d46e84b

Please sign in to comment.