Skip to content

Commit 13b83e1

Browse files
Add playground files for cancellation
1 parent c290d3c commit 13b83e1

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/playground/flow/cancellation/1_cancellation_basics.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.lukaslechner.coroutineusecasesonandroid.playground.flow.cancellation
22

33
import kotlinx.coroutines.CancellationException
44
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.cancel
56
import kotlinx.coroutines.flow.flow
67
import kotlinx.coroutines.flow.onCompletion
78
import kotlinx.coroutines.launch
@@ -19,6 +20,10 @@ suspend fun main() {
1920
}
2021
.collect {
2122
println("Collected $it")
23+
24+
if (it == 2) {
25+
cancel()
26+
}
2227
}
2328
}.join()
2429
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.flow.cancellation
2+
3+
import kotlinx.coroutines.*
4+
import kotlinx.coroutines.flow.flow
5+
import kotlinx.coroutines.flow.onCompletion
6+
import java.math.BigInteger
7+
import kotlin.coroutines.EmptyCoroutineContext
8+
9+
suspend fun main() {
10+
val scope = CoroutineScope(EmptyCoroutineContext)
11+
12+
scope.launch {
13+
intFlow()
14+
.onCompletion { throwable ->
15+
if (throwable is CancellationException) {
16+
println("Flow got cancelled.")
17+
}
18+
}
19+
.collect {
20+
println("Collected $it")
21+
22+
if (it == 2) {
23+
cancel()
24+
}
25+
}
26+
}.join()
27+
}
28+
29+
private fun intFlow() = flow {
30+
emit(1)
31+
emit(2)
32+
33+
println("Start calculation")
34+
calculateFactorialOf(1_000)
35+
println("Calculation finished!")
36+
37+
emit(3)
38+
}
39+
40+
private suspend fun calculateFactorialOf(number: Int): BigInteger = coroutineScope {
41+
var factorial = BigInteger.ONE
42+
for (i in 1..number) {
43+
factorial = factorial.multiply(BigInteger.valueOf(i.toLong()))
44+
ensureActive()
45+
}
46+
factorial
47+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.lukaslechner.coroutineusecasesonandroid.playground.flow.cancellation
2+
3+
import kotlinx.coroutines.CancellationException
4+
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.cancel
6+
import kotlinx.coroutines.flow.cancellable
7+
import kotlinx.coroutines.flow.flowOf
8+
import kotlinx.coroutines.flow.onCompletion
9+
import kotlinx.coroutines.launch
10+
import kotlin.coroutines.EmptyCoroutineContext
11+
12+
suspend fun main() {
13+
val scope = CoroutineScope(EmptyCoroutineContext)
14+
15+
scope.launch {
16+
flowOf(1, 2, 3)
17+
.onCompletion { throwable ->
18+
if (throwable is CancellationException) {
19+
println("Flow got cancelled.")
20+
}
21+
}.cancellable()
22+
.collect {
23+
println("Collected $it")
24+
25+
if (it == 2) {
26+
cancel()
27+
}
28+
}
29+
}.join()
30+
}

0 commit comments

Comments
 (0)