Skip to content

Commit a5e5052

Browse files
Add playground files for collectLatest and mapLatest
1 parent 3667ea0 commit a5e5052

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.flow.concurrency
2+
3+
import kotlinx.coroutines.coroutineScope
4+
import kotlinx.coroutines.delay
5+
import kotlinx.coroutines.flow.collectLatest
6+
import kotlinx.coroutines.flow.flow
7+
8+
suspend fun main() = coroutineScope {
9+
10+
val flow = flow {
11+
repeat(5) {
12+
val pancakeIndex = it + 1
13+
println("Emitter: Start Cooking Pancake $pancakeIndex")
14+
delay(100)
15+
println("Emitter: Pancake $pancakeIndex ready!")
16+
emit(pancakeIndex)
17+
}
18+
}
19+
20+
flow.collectLatest {
21+
println("Collector: Start eating pancake $it")
22+
delay(300)
23+
println("Collector: Finished eating pancake $it")
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.flow.concurrency
2+
3+
import kotlinx.coroutines.coroutineScope
4+
import kotlinx.coroutines.delay
5+
import kotlinx.coroutines.flow.flow
6+
import kotlinx.coroutines.flow.mapLatest
7+
8+
suspend fun main() = coroutineScope {
9+
10+
val flow = flow {
11+
repeat(5) {
12+
val pancakeIndex = it + 1
13+
println("Emitter: Start Cooking Pancake $pancakeIndex")
14+
delay(100)
15+
println("Emitter: Pancake $pancakeIndex ready!")
16+
emit(pancakeIndex)
17+
}
18+
}.mapLatest {
19+
println("Add topping onto the pancake $it")
20+
delay(200)
21+
it
22+
}
23+
24+
flow.collect {
25+
println("Collector: Start eating pancake $it")
26+
delay(300)
27+
println("Collector: Finished eating pancake $it")
28+
}
29+
}

0 commit comments

Comments
 (0)