File tree 2 files changed +25
-2
lines changed
kotlinx-coroutines-core/common
2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -51,7 +51,8 @@ public fun <T> Flow<T>.take(count: Int): Flow<T> {
51
51
require(count > 0 ) { " Requested element count $count should be positive" }
52
52
return flow {
53
53
var consumed = 0
54
- collectWhile { value ->
54
+ // This return is needed to work around a bug in JS BE
55
+ return @flow collectWhile { value ->
55
56
emit(value)
56
57
++ consumed < count
57
58
}
@@ -65,7 +66,8 @@ public fun <T> Flow<T>.take(count: Int): Flow<T> {
65
66
* See [transformWhile] for a more flexible operator.
66
67
*/
67
68
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 ->
69
71
if (predicate(value)) {
70
72
emit(value)
71
73
true
Original file line number Diff line number Diff line change @@ -24,6 +24,27 @@ class TransformWhileTest : TestBase() {
24
24
assertEquals(expected, actual)
25
25
}
26
26
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
+
27
48
@Test
28
49
fun testExample () = runTest {
29
50
val source = listOf (
You can’t perform that action at this time.
0 commit comments