Skip to content

Commit 1dcf0eb

Browse files
committed
~ Workaround JS BE bugs and more tests
1 parent 4545bf0 commit 1dcf0eb

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

kotlinx-coroutines-core/common/src/flow/operators/Limit.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public fun <T> Flow<T>.take(count: Int): Flow<T> {
5151
require(count > 0) { "Requested element count $count should be positive" }
5252
return flow {
5353
var consumed = 0
54-
collectWhile { value ->
54+
// This return is needed to work around a bug in JS BE
55+
return@flow collectWhile { value ->
5556
emit(value)
5657
++consumed < count
5758
}
@@ -65,7 +66,8 @@ public fun <T> Flow<T>.take(count: Int): Flow<T> {
6566
* See [transformWhile] for a more flexible operator.
6667
*/
6768
public fun <T> Flow<T>.takeWhile(predicate: suspend (T) -> Boolean): Flow<T> = flow {
68-
collectWhile { value ->
69+
// This return is needed to work around a bug in JS BE
70+
return@flow collectWhile { value ->
6971
if (predicate(value)) {
7072
emit(value)
7173
true

kotlinx-coroutines-core/common/test/flow/operators/TransformWhileTest.kt

+21
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,27 @@ class TransformWhileTest : TestBase() {
2424
assertEquals(expected, actual)
2525
}
2626

27+
@Test
28+
fun testCancelUpstream() = runTest {
29+
var cancelled = false
30+
val flow = flow {
31+
coroutineScope {
32+
launch(start = CoroutineStart.ATOMIC) {
33+
hang { cancelled = true }
34+
}
35+
emit(1)
36+
emit(2)
37+
emit(3)
38+
}
39+
}
40+
val transformed = flow.transformWhile {
41+
emit(it)
42+
it < 2
43+
}
44+
assertEquals(listOf(1, 2), transformed.toList())
45+
assertTrue(cancelled)
46+
}
47+
2748
@Test
2849
fun testExample() = runTest {
2950
val source = listOf(

0 commit comments

Comments
 (0)