Skip to content

Commit 2596414

Browse files
committed
Add flowOf(value), use unsafeFlow in trivial flow builders
1 parent 641d671 commit 2596414

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

binary-compatibility-validator/reference-public-api/kotlinx-coroutines-core.txt

+1
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ public final class kotlinx/coroutines/flow/FlowKt {
813813
public static final fun flattenMerge (Lkotlinx/coroutines/flow/Flow;II)Lkotlinx/coroutines/flow/Flow;
814814
public static synthetic fun flattenMerge$default (Lkotlinx/coroutines/flow/Flow;IIILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
815815
public static final fun flow (Lkotlin/jvm/functions/Function2;)Lkotlinx/coroutines/flow/Flow;
816+
public static final fun flowOf (Ljava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
816817
public static final fun flowOf ([Ljava/lang/Object;)Lkotlinx/coroutines/flow/Flow;
817818
public static final fun flowOn (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;I)Lkotlinx/coroutines/flow/Flow;
818819
public static synthetic fun flowOn$default (Lkotlinx/coroutines/flow/Flow;Lkotlin/coroutines/CoroutineContext;IILjava/lang/Object;)Lkotlinx/coroutines/flow/Flow;

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

+16-4
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@ public fun <T> flowOf(vararg elements: T): Flow<T> = unsafeFlow {
127127
}
128128
}
129129

130+
/**
131+
* Creates flow that produces a given [value].
132+
*/
133+
@FlowPreview
134+
public fun <T> flowOf(value: T): Flow<T> = unsafeFlow {
135+
/*
136+
* Implementation note: this is just an "optimized" overload of flowOf(vararg)
137+
* which significantly reduce the footprint of widespread single-value flows.
138+
*/
139+
emit(value)
140+
}
141+
130142
/**
131143
* Returns an empty flow.
132144
*/
@@ -141,7 +153,7 @@ private object EmptyFlow : Flow<Nothing> {
141153
* Creates a flow that produces values from the given array.
142154
*/
143155
@FlowPreview
144-
public fun <T> Array<T>.asFlow(): Flow<T> = flow {
156+
public fun <T> Array<T>.asFlow(): Flow<T> = unsafeFlow {
145157
forEach { value ->
146158
emit(value)
147159
}
@@ -151,7 +163,7 @@ public fun <T> Array<T>.asFlow(): Flow<T> = flow {
151163
* Creates flow that produces values from the given array.
152164
*/
153165
@FlowPreview
154-
public fun IntArray.asFlow(): Flow<Int> = flow {
166+
public fun IntArray.asFlow(): Flow<Int> = unsafeFlow {
155167
forEach { value ->
156168
emit(value)
157169
}
@@ -161,7 +173,7 @@ public fun IntArray.asFlow(): Flow<Int> = flow {
161173
* Creates flow that produces values from the given array.
162174
*/
163175
@FlowPreview
164-
public fun LongArray.asFlow(): Flow<Long> = flow {
176+
public fun LongArray.asFlow(): Flow<Long> = unsafeFlow {
165177
forEach { value ->
166178
emit(value)
167179
}
@@ -171,7 +183,7 @@ public fun LongArray.asFlow(): Flow<Long> = flow {
171183
* Creates flow that produces values from the given range.
172184
*/
173185
@FlowPreview
174-
public fun IntRange.asFlow(): Flow<Int> = flow {
186+
public fun IntRange.asFlow(): Flow<Int> = unsafeFlow {
175187
forEach { value ->
176188
emit(value)
177189
}

0 commit comments

Comments
 (0)