Skip to content

Commit e9eb0ef

Browse files
authored
Make FlowCollector fun interface, remove redundant extensions (#3047)
* It also saves us from the copy-without-imports problem Addresses #3037
1 parent ff9359a commit e9eb0ef

File tree

3 files changed

+17
-40
lines changed

3 files changed

+17
-40
lines changed

kotlinx-coroutines-core/api/kotlinx-coroutines-core.api

-2
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,6 @@ public final class kotlinx/coroutines/flow/FlowKt {
919919
public static final fun catch (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function3;)Lkotlinx/coroutines/flow/Flow;
920920
public static final fun channelFlow (Lkotlin/jvm/functions/Function2;)Lkotlinx/coroutines/flow/Flow;
921921
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
922-
public static final fun collect (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
923-
public static final fun collect (Lkotlinx/coroutines/flow/SharedFlow;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
924922
public static final fun collectIndexed (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function3;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
925923
public static final fun collectLatest (Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
926924
public static final fun combine (Lkotlinx/coroutines/flow/Flow;Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function3;)Lkotlinx/coroutines/flow/Flow;

kotlinx-coroutines-core/common/src/flow/FlowCollector.kt

+17-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,25 @@ package kotlinx.coroutines.flow
88
* [FlowCollector] is used as an intermediate or a terminal collector of the flow and represents
99
* an entity that accepts values emitted by the [Flow].
1010
*
11-
* This interface should usually not be implemented directly, but rather used as a receiver in a [flow] builder when implementing a custom operator.
11+
* This interface should usually not be implemented directly, but rather used as a receiver in a [flow] builder when implementing a custom operator,
12+
* or with SAM-conversion.
1213
* Implementations of this interface are not thread-safe.
14+
*
15+
* Example of usage:
16+
*
17+
* ```
18+
* val flow = getMyEvents()
19+
* try {
20+
* flow.collect { value ->
21+
* println("Received $value")
22+
* }
23+
* println("My events are consumed successfully")
24+
* } catch (e: Throwable) {
25+
* println("Exception from the flow: $e")
26+
* }
27+
* ```
1328
*/
14-
public interface FlowCollector<in T> {
29+
public fun interface FlowCollector<in T> {
1530

1631
/**
1732
* Collects the value emitted by the upstream.

kotlinx-coroutines-core/common/src/flow/terminal/Collect.kt

-36
Original file line numberDiff line numberDiff line change
@@ -50,42 +50,6 @@ public fun <T> Flow<T>.launchIn(scope: CoroutineScope): Job = scope.launch {
5050
collect() // tail-call
5151
}
5252

53-
/**
54-
* Terminal flow operator that collects the given flow with a provided [action].
55-
* If any exception occurs during collect or in the provided flow, this exception is rethrown from this method.
56-
*
57-
* Example of use:
58-
*
59-
* ```
60-
* val flow = getMyEvents()
61-
* try {
62-
* flow.collect { value ->
63-
* println("Received $value")
64-
* }
65-
* println("My events are consumed successfully")
66-
* } catch (e: Throwable) {
67-
* println("Exception from the flow: $e")
68-
* }
69-
* ```
70-
*/
71-
public suspend inline fun <T> Flow<T>.collect(crossinline action: suspend (value: T) -> Unit): Unit =
72-
collect(object : FlowCollector<T> {
73-
override suspend fun emit(value: T) = action(value)
74-
})
75-
76-
/**
77-
* Terminal flow operator that collects the given [SharedFlow] with the provided [action].
78-
* If any exception occurs during `collect` or in the provided flow, this exception is rethrown from this method.
79-
*
80-
* This is a counterpart of a regular [Flow.collect] extension, only different in the return type
81-
* so that any code below `collect` produces a compilation warning.
82-
*/
83-
public suspend inline fun <T> SharedFlow<T>.collect(crossinline action: suspend (value: T) -> Unit): Nothing {
84-
collect(object : FlowCollector<T> {
85-
override suspend fun emit(value: T) = action(value)
86-
})
87-
}
88-
8953
/**
9054
* Terminal flow operator that collects the given flow with a provided [action] that takes the index of an element (zero-based) and the element.
9155
* If any exception occurs during collect or in the provided flow, this exception is rethrown from this method.

0 commit comments

Comments
 (0)