Skip to content

Commit 1d7a4a0

Browse files
authored
add flowFromNonSuspend (hoc081098#196)
1 parent ab4a7ba commit 1d7a4a0

File tree

7 files changed

+121
-19
lines changed

7 files changed

+121
-19
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Petrus Nguyễn Thái Học
3+
Copyright (c) 2021-2023 Petrus Nguyễn Thái Học
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,7 @@ snapshot](https://hoc081098.github.io/FlowExt/docs/latest).
13351335
```License
13361336
MIT License
13371337
1338-
Copyright (c) 2021-2022 Petrus Nguyễn Thái Học
1338+
Copyright (c) 2021-2023 Petrus Nguyễn Thái Học
13391339
```
13401340

13411341
[badge-android]: http://img.shields.io/badge/android-6EDB8D.svg?style=flat

api/FlowExt.api

+4
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ public final class com/hoc081098/flowext/FlatMapFirstKt {
134134
public abstract interface annotation class com/hoc081098/flowext/FlowExtPreview : java/lang/annotation/Annotation {
135135
}
136136

137+
public final class com/hoc081098/flowext/FlowFromNonSuspendKt {
138+
public static final fun flowFromNonSuspend (Lkotlin/jvm/functions/Function0;)Lkotlinx/coroutines/flow/Flow;
139+
}
140+
137141
public final class com/hoc081098/flowext/FlowFromSuspendKt {
138142
public static final fun flowFromSuspend (Lkotlin/jvm/functions/Function1;)Lkotlinx/coroutines/flow/Flow;
139143
}

src/commonMain/kotlin/com/hoc081098/flowext/concat.kt

+16-16
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,22 @@ public fun <T> Flow<T>.concatWith(others: Sequence<Flow<T>>): Flow<T> {
169169
}
170170
}
171171

172+
/**
173+
* This function is an alias to [concatWith] operator.
174+
*
175+
* Returns a [Flow] that emits the items emitted from this [Flow], then the next, one after the other, without interleaving them.
176+
*
177+
* Example:
178+
* ``` kotlin
179+
* val flow1 = flowOf(1, 2, 3)
180+
* val flow2 = flowOf(4, 5, 6)
181+
* val result = flow1 + flow2 // produces the following emissions 1, 2, 3, 4, 5, 6
182+
* ```
183+
*
184+
* @see concatWith
185+
*/
186+
public operator fun <T> Flow<T>.plus(other: Flow<T>): Flow<T> = concat(this, other)
187+
172188
//
173189
// startWith
174190
//
@@ -267,19 +283,3 @@ public fun <T> Flow<T>.startWith(others: Sequence<T>): Flow<T> = concat(others.a
267283
* Returns a [Flow] that emits the items in a specified [Flow] before it begins to emit items emitted by the current [Flow].
268284
*/
269285
public fun <T> Flow<T>.startWith(other: Flow<T>): Flow<T> = concat(other, this)
270-
271-
/**
272-
* This function is an alias to [concatWith] operator.
273-
*
274-
* Returns a [Flow] that emits the items emitted from this [Flow], then the next, one after the other, without interleaving them.
275-
*
276-
* @see concatWith
277-
*
278-
* Example:
279-
* ``` kotlin
280-
* val flow1 = flowOf(1, 2, 3)
281-
* val flow2 = flowOf(4, 5, 6)
282-
* val result = flow1 + flow2 // produces the following emissions 1, 2, 3, 4, 5, 6
283-
* ```
284-
*/
285-
public operator fun <T, R : T> Flow<T>.plus(other: Flow<R>): Flow<T> = concat(this, other)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2021-2023 Petrus Nguyễn Thái Học
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package com.hoc081098.flowext
26+
27+
import kotlinx.coroutines.flow.Flow
28+
import kotlinx.coroutines.flow.flow
29+
30+
/**
31+
* Creates a _cold_ flow that produces a single value from the given [function].
32+
*
33+
* Example of usage:
34+
*
35+
* ```
36+
* fun call(): R = ...
37+
* fun callAsFlow(): Flow<R> = flowFromNonSuspend(::call)
38+
* ```
39+
*/
40+
public fun <T> flowFromNonSuspend(function: () -> T): Flow<T> = flow { return@flow emit(function()) }

src/commonMain/kotlin/com/hoc081098/flowext/flowFromSuspend.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,5 @@ import kotlinx.coroutines.flow.flow
3737
* fun remoteCallFlow(): Flow<R> = flowFromSuspend(::remoteCall)
3838
* ```
3939
*/
40-
public fun <T> flowFromSuspend(function: suspend () -> T): Flow<T> = flow { emit(function()) }
40+
public fun <T> flowFromSuspend(function: suspend () -> T): Flow<T> =
41+
flow { return@flow emit(function()) }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2021-2023 Petrus Nguyễn Thái Học
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package com.hoc081098.flowext
26+
27+
import com.hoc081098.flowext.utils.BaseTest
28+
import com.hoc081098.flowext.utils.TestException
29+
import com.hoc081098.flowext.utils.test
30+
import kotlin.test.Test
31+
import kotlinx.coroutines.ExperimentalCoroutinesApi
32+
33+
@ExperimentalCoroutinesApi
34+
class FlowFromNonSuspendTest : BaseTest() {
35+
@Test
36+
fun flowFromNonSuspendEmitsValues() = runTest {
37+
var count = 0L
38+
val flow = flowFromNonSuspend {
39+
count
40+
}
41+
42+
flow.test(listOf(Event.Value(0L), Event.Complete))
43+
44+
++count
45+
flow.test(listOf(Event.Value(1L), Event.Complete))
46+
47+
++count
48+
flow.test(listOf(Event.Value(2L), Event.Complete))
49+
}
50+
51+
@Test
52+
fun deferFactoryThrows() = runTest {
53+
val testException = TestException()
54+
55+
flowFromNonSuspend<Int> { throw testException }.test(listOf(Event.Error(testException)))
56+
}
57+
}

0 commit comments

Comments
 (0)