Slide 1

Slide 1 text

Copenhagen Denmark ASYNCHRONOUS DATA STREAMS WITH KOTLIN FLOW ROMAN ELIZAROV @relizarov Copenhagen Denmark

Slide 2

Slide 2 text

RECAP ON KOTLIN COROUTINES Kotlin Coroutines FTW

Slide 3

Slide 3 text

Callback hell before fun requestTokenAsync(): Promise { … } fun createPostAsync(token: Token, item: Item): Promise … fun processPost(post: Post) { … } fun postItem(item: Item) { requestTokenAsync() .thenCompose { token -> createPostAsync(token, item) } .thenAccept { post -> processPost(post) } }

Slide 4

Slide 4 text

Direct style with Kotlin Corou6nes suspend fun requestToken(): Token { … } suspend fun createPost(token: Token, item: Item): Post { … } fun processPost(post: Post) { … } suspend fun postItem(item: Item) { val token = requestToken() val post = createPost(token, item) processPost(post) } Like regular code

Slide 5

Slide 5 text

Direct style with Kotlin Coroutines suspend fun requestToken(): Token { … } suspend fun createPost(token: Token, item: Item): Post { … } fun processPost(post: Post) { … } suspend fun postItem(item: Item) { val token = requestToken() val post = createPost(token, item) processPost(post) }

Slide 6

Slide 6 text

Asynchronous yet sequen6al suspend fun requestToken(): Token { … } suspend fun createPost(token: Token, item: Item): Post { … } fun processPost(post: Post) { … } suspend fun postItem(item: Item) { val token = requestToken() val post = createPost(token, item) processPost(post) }

Slide 7

Slide 7 text

Asynchronous yet sequential suspend fun requestToken(): Token { … } suspend fun createPost(token: Token, item: Item): Post { … } fun processPost(post: Post) { … } suspend fun postItem(item: Item) { val token = requestToken() val post = createPost(token, item) processPost(post) }

Slide 8

Slide 8 text

Asynchronous yet sequential suspend fun requestToken(): Token { … } suspend fun createPost(token: Token, item: Item): Post { … } fun processPost(post: Post) { … } suspend fun postItem(item: Item) { val token = requestToken() val post = createPost(token, item) processPost(post) }

Slide 9

Slide 9 text

suspend fun foo(): Response One response suspend fun foo(): List Many responses

Slide 10

Slide 10 text

suspend fun foo(): List

Slide 11

Slide 11 text

suspend fun foo(): List = buildList { … }

Slide 12

Slide 12 text

suspend fun foo(): List = buildList { add(compute("A")) add(compute("B")) add(compute("C")) }

Slide 13

Slide 13 text

suspend fun foo(): List = buildList { add(compute("A")) add(compute("B")) add(compute("C")) } fun main() = runBlocking { val list = foo() for (x in list) println(x) }

Slide 14

Slide 14 text

suspend fun foo(): List = buildList { add(compute("A")) add(compute("B")) add(compute("C")) } fun main() = runBlocking { val list = foo() for (x in list) println(x) }

Slide 15

Slide 15 text

suspend fun foo(): List = buildList { add(compute("A")) add(compute("B")) add(compute("C")) } fun main() = runBlocking { val list = foo() for (x in list) println(x) }

Slide 16

Slide 16 text

main() foo() suspend fun foo(): List = buildList { add(compute("A")) add(compute("B")) add(compute("C")) } fun main() = runBlocking { val list = foo() for (x in list) println(x) }

Slide 17

Slide 17 text

main() foo() suspend fun foo(): List = buildList { add(compute("A")) add(compute("B")) add(compute("C")) } fun main() = runBlocking { val list = foo() for (x in list) println(x) }

Slide 18

Slide 18 text

main() foo() A suspend fun foo(): List = buildList { add(compute("A")) add(compute("B")) add(compute("C")) } fun main() = runBlocking { val list = foo() for (x in list) println(x) }

Slide 19

Slide 19 text

main() foo() suspend fun foo(): List = buildList { add(compute("A")) add(compute("B")) add(compute("C")) } fun main() = runBlocking { val list = foo() for (x in list) println(x) } B A

Slide 20

Slide 20 text

main() foo() suspend fun foo(): List = buildList { add(compute("A")) add(compute("B")) add(compute("C")) } fun main() = runBlocking { val list = foo() for (x in list) println(x) } C B A

Slide 21

Slide 21 text

main() foo() suspend fun foo(): List = buildList { add(compute("A")) add(compute("B")) add(compute("C")) } fun main() = runBlocking { val list = foo() for (x in list) println(x) } List C B A

Slide 22

Slide 22 text

main() foo() suspend fun foo(): List = buildList { add(compute("A")) add(compute("B")) add(compute("C")) } fun main() = runBlocking { val list = foo() for (x in list) println(x) } List C B A

Slide 23

Slide 23 text

main() foo() List A B C B C A suspend fun foo(): List = buildList { add(compute("A")) add(compute("B")) add(compute("C")) } fun main() = runBlocking { val list = foo() for (x in list) println(x) }

Slide 24

Slide 24 text

main() foo() List A B C B C A suspend fun foo(): List = buildList { add(compute("A")) add(compute("B")) add(compute("C")) } fun main() = runBlocking { val list = foo() for (x in list) println(x) }

Slide 25

Slide 25 text

Channel receive() send()

Slide 26

Slide 26 text

fun CoroutineScope.foo(): ReceiveChannel = produce { … }

Slide 27

Slide 27 text

fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) }

Slide 28

Slide 28 text

fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 29

Slide 29 text

fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 30

Slide 30 text

fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 31

Slide 31 text

main() foo() fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 32

Slide 32 text

main() foo() fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 33

Slide 33 text

main() foo() Channel fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 34

Slide 34 text

main() foo() Channel fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 35

Slide 35 text

main() foo() Channel fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 36

Slide 36 text

main() foo() send Channel A fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 37

Slide 37 text

main() foo() send Channel A A fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 38

Slide 38 text

main() foo() send send Channel A A B fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 39

Slide 39 text

main() foo() send send Channel A B A B fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 40

Slide 40 text

main() foo() send send send Channel A B A B C fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 41

Slide 41 text

main() foo() send send send Channel A B C A B C fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 42

Slide 42 text

main() foo() send send send Channel A B C A B C fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 43

Slide 43 text

main() foo() send send send Channel A B C A B C fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 44

Slide 44 text

main() foo() send send send Channel A B C A B C ❌ fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() for (x in channel) println(x) }

Slide 45

Slide 45 text

Channel is hot

Slide 46

Slide 46 text

fun main() = runBlocking { val channel = foo() for (x in channel) println(x) } Channel is hot

Slide 47

Slide 47 text

Channel is hot fun main() = runBlocking { val channel = foo() // for (x in channel) println(x) }

Slide 48

Slide 48 text

main() foo() Channel fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() // for (x in channel) println(x) }

Slide 49

Slide 49 text

main() foo() send Channel A fun CoroutineScope.foo(): ReceiveChannel = produce { send(compute("A")) send(compute("B")) send(compute("C")) } fun main() = runBlocking { val channel = foo() // for (x in channel) println(x) }

Slide 50

Slide 50 text

fun CoroutineScope.foo(): ReceiveChannel

Slide 51

Slide 51 text

KOTLIN FLOW Image: Markus Trienke, Sunset over dri6 ice

Slide 52

Slide 52 text

fun foo(): Flow = flow { … }

Slide 53

Slide 53 text

fun foo(): Flow = flow { … }

Slide 54

Slide 54 text

fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 55

Slide 55 text

fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 56

Slide 56 text

fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 57

Slide 57 text

fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 58

Slide 58 text

main() foo() fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 59

Slide 59 text

main() foo() fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 60

Slide 60 text

main() foo() Flow fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 61

Slide 61 text

main() foo() Flow fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 62

Slide 62 text

main() foo() collect Flow fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 63

Slide 63 text

main() foo() emit collect A Flow fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 64

Slide 64 text

main() foo() emit A collect A Flow fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 65

Slide 65 text

main() foo() emit A collect A Flow fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 66

Slide 66 text

main() foo() emit emit A collect A B Flow fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 67

Slide 67 text

main() foo() emit emit A B collect A B Flow fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 68

Slide 68 text

main() foo() emit emit A B collect A B Flow fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 69

Slide 69 text

main() foo() emit emit emit A B collect A B C Flow fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 70

Slide 70 text

main() foo() emit emit emit A B C collect A B C Flow fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 71

Slide 71 text

main() foo() emit emit emit A B C collect A B C Flow fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 72

Slide 72 text

main() foo() emit emit emit A B C collect A B C ❌ Flow fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 73

Slide 73 text

main() foo() emit emit emit A B C collect A B C ❌ Flow fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) }

Slide 74

Slide 74 text

fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } Flow is cold ❄

Slide 75

Slide 75 text

fun main() = runBlocking { val flow = foo() // flow.collect { x -> println(x) } } Flow is cold ❄

Slide 76

Slide 76 text

Flow is declara6ve

Slide 77

Slide 77 text

Flow is declarative fun foo(): Flow = flow { emit(compute("A")) emit(compute("B")) emit(compute("C")) } Declaration

Slide 78

Slide 78 text

fun strings(): Flow = flow { emit("A") emit("B") emit(”C") }

Slide 79

Slide 79 text

fun strings(): Flow = flow { … } fun foo(): Flow = strings().map { name -> compute(name) }

Slide 80

Slide 80 text

fun strings(): Flow = flow { … } fun foo(): Flow = strings().map { name -> compute(name) }

Slide 81

Slide 81 text

fun strings(): Flow = flow { … } fun foo(): Flow = strings().map { name -> compute(name) }

Slide 82

Slide 82 text

fun strings(): Flow = flow { … } fun foo(): Flow = strings().map { name -> compute(name) } Operators

Slide 83

Slide 83 text

fun foo(): Flow = flowOf("A", "B", "C").map { name -> compute(name) } Operators

Slide 84

Slide 84 text

Flow vs List fun foo(): Flow = flowOf("A", "B", "C").map { name -> compute(name) } suspend fun foo(): List = listOf("A", "B", "C").map { name -> compute(name) }

Slide 85

Slide 85 text

fun foo(): Flow = flowOf("A", "B", "C").map { name -> compute(name) } suspend fun foo(): List = listOf("A", "B", "C").map { name -> compute(name) } Defined – Declarative Runs – Impera:ve suspend fun Flow.collect(…) Runs the flow Flow vs List

Slide 86

Slide 86 text

fun foo(): Flow = flowOf("A", "B", "C").map { name -> compute(name) } suspend fun foo(): List = listOf("A", "B", "C").map { name -> compute(name) } Defined – Declara:ve Runs – Imperative suspend fun Flow.toList(): List Runs the flow Flow vs List

Slide 87

Slide 87 text

fun foo(): Flow = flowOf("A", "B", "C").map { name -> compute(name) } suspend fun foo(): List = listOf("A", "B", "C").map { name -> compute(name) } Flow vs List Execu6on order

Slide 88

Slide 88 text

fun foo(): Flow = flowOf("A", "B", "C").map { name -> compute(name) } suspend fun foo(): List = listOf("A", "B", "C").map { name -> compute(name) } B C A Flow vs List

Slide 89

Slide 89 text

fun foo(): Flow = flowOf("A", "B", "C").map { name -> compute(name) } suspend fun foo(): List = listOf("A", "B", "C").map { name -> compute(name) } B C A map Flow vs List

Slide 90

Slide 90 text

fun foo(): Flow = flowOf("A", "B", "C").map { name -> compute(name) } suspend fun foo(): List = listOf("A", "B", "C").map { name -> compute(name) } B C A B’ C’ A’ map Flow vs List

Slide 91

Slide 91 text

fun foo(): Flow = flowOf("A", "B", "C").map { name -> compute(name) } suspend fun foo(): List = listOf("A", "B", "C").map { name -> compute(name) } B C A B’ C’ A’ map A A’ Flow vs List

Slide 92

Slide 92 text

fun foo(): Flow = flowOf("A", "B", "C").map { name -> compute(name) } suspend fun foo(): List = listOf("A", "B", "C").map { name -> compute(name) } B C A B’ C’ A’ map A B’ A’ B Flow vs List

Slide 93

Slide 93 text

fun foo(): Flow = flowOf("A", "B", "C").map { name -> compute(name) } suspend fun foo(): List = listOf("A", "B", "C").map { name -> compute(name) } B C A B’ C’ A’ map A B’ C’ A’ B C Flow vs List React on emi2ed values

Slide 94

Slide 94 text

Flow is reac6ve RxJava Project Reactor Kotlin Flow Reactive Streams Specification

Slide 95

Slide 95 text

Publisher org.reactivestreams Publisher

Slide 96

Slide 96 text

fun Publisher.asFlow(): Flow org.reactivestreams Publisher kotlinx.coroutines.flow Flow

Slide 97

Slide 97 text

fun Flow.asPublisher(): Publisher fun Publisher.asFlow(): Flow org.reactivestreams Publisher kotlinx.coroutines.flow Flow

Slide 98

Slide 98 text

WHY FLOW? What’s the difference?

Slide 99

Slide 99 text

A A’ mapper fun map(mapper: (T) -> R): Flowable fun flatMapSingle(mapper: (T) -> SingleSource): Flowable Synchronous Asynchronous Flowable

Slide 100

Slide 100 text

A A’ mapper fun map(mapper: (T) -> R): Flowable fun flatMapSingle(mapper: (T) -> SingleSource): Flowable fun filter(predicate: (T) -> Boolean): Flowable A A predicate Synchronous Asynchronous Synchronous Asynchronous Flowable

Slide 101

Slide 101 text

A A’ Flow fun map(transform: suspend (T) -> R): Flow transform

Slide 102

Slide 102 text

A A’ transform Flow fun map(transform: suspend (T) -> R): Flow

Slide 103

Slide 103 text

A A’ transform fun map(transform: suspend (T) -> R): Flow Flow fun filter(predicate: suspend (T) -> Boolean): Flow A predicate A

Slide 104

Slide 104 text

Operator avoidance startWith(value) onStart { emit(value) } delaySubscription(time) onStart { delay(time) } startWith(flow) onStart { emitAll(flow) } delayEach(time) onEach { delay(time) } onErrorReturn(value) catch { emit(value) } onErrorResume(flow) catch { emitAll(flow) } generate(…) flow { … } Composable

Slide 105

Slide 105 text

Reactive + = ❤

Slide 106

Slide 106 text

FLOW UNDER THE HOOD Image: Flow by Grant Tarrant

Slide 107

Slide 107 text

interface Flow { suspend fun collect(collector: FlowCollector) } interface FlowCollector { suspend fun emit(value: T) }

Slide 108

Slide 108 text

flow.collect { value -> println(value) } collect 1

Slide 109

Slide 109 text

flow.collect { value -> println(value) } val flow = flow { emit("A") } collect 1

Slide 110

Slide 110 text

flow.collect { value -> println(value) } val flow = flow { emit("A") } collect 1

Slide 111

Slide 111 text

flow.collect { value -> println(value) } val flow = flow { emit("A") } collect 1 λ

Slide 112

Slide 112 text

flow.collect { value -> println(value) } val flow = flow { emit("A") } collect 1 λ

Slide 113

Slide 113 text

flow.collect { value -> println(value) } val flow = flow { emit("A") } collect emit λ 1 2 λ

Slide 114

Slide 114 text

flow.collect { value -> println(value) } val flow = flow { emit("A") } collect emit λ println 1 2 3 λ

Slide 115

Slide 115 text

flow.collect { value -> println(value) } val flow = flow { emit("A") } collect λ emit λ println 1 2 3 ^ 4 ^ 5

Slide 116

Slide 116 text

flow.collect { value -> println(value) } val flow = flow { emit("A") emit("B") } collect λ emit λ println emit println

Slide 117

Slide 117 text

flow.collect { value -> println(value) } val flow = flow { emit("A") delay(100) emit("B") } collect λ emit λ println emit println Asynchronous emi@er

Slide 118

Slide 118 text

flow.collect { value -> delay(100) println(value) } val flow = flow { emit("A") delay(100) emit("B") } collect λ emit λ println emit println Backpressure

Slide 119

Slide 119 text

Simple design

Slide 120

Slide 120 text

Simple design ⇒ performance

Slide 121

Slide 121 text

Kotlin Flow Plays Scrabble • Benchmark originally developed by José Paumard • Implemented for RxJava by David Karnok

Slide 122

Slide 122 text

Kotlin Flow Plays Scrabble • Benchmark originally developed by José Paumard • Implemented for RxJava by David Karnok SequencePlaysScrabble 9.824 ± 0.190 ms/op https://github.com/Kotlin/kotlinx.coroutines/tree/develop/benchmarks/src/jmh/kotlin/benchmarks/flow/scrabble/README.md

Slide 123

Slide 123 text

Kotlin Flow Plays Scrabble • Benchmark originally developed by José Paumard • Implemented for RxJava by David Karnok SequencePlaysScrabble 9.824 ± 0.190 ms/op RxJava2PlaysScrabbleOpt 23.653 ± 0.379 ms/op https://github.com/Kotlin/kotlinx.coroutines/tree/develop/benchmarks/src/jmh/kotlin/benchmarks/flow/scrabble/README.md

Slide 124

Slide 124 text

Kotlin Flow Plays Scrabble • Benchmark originally developed by José Paumard • Implemented for RxJava by David Karnok SequencePlaysScrabble 9.824 ± 0.190 ms/op RxJava2PlaysScrabbleOpt 23.653 ± 0.379 ms/op FlowPlaysScrabbleOpt 13.958 ± 0.278 ms/op https://github.com/Kotlin/kotlinx.coroutines/tree/develop/benchmarks/src/jmh/kotlin/benchmarks/flow/scrabble/README.md

Slide 125

Slide 125 text

FLOW IS ASYNCHRONOUS

Slide 126

Slide 126 text

FLOW IS ASYNCHRONOUS YET SEQUENTIAL

Slide 127

Slide 127 text

main() emit emit emit A B C collect A B C ❌ 100 ms 100 ms 100 ms 100 ms 100 ms 100 ms 100 ms 700 ms Single corouDne

Slide 128

Slide 128 text

GOING CONCURRENT WITH A FLOW Image: Channels by Tom Doel

Slide 129

Slide 129 text

flow.buffer().collect { … }

Slide 130

Slide 130 text

flow.buffer().collect { … } main() emit emit emit A B C collect A B C ❌ 100 ms 100 ms 100 ms 100 ms 400 ms 100 ms 100 ms 100 ms

Slide 131

Slide 131 text

flow.buffer().collect { … } main() emit emit emit A B C collect A B C Separate corouDne

Slide 132

Slide 132 text

flow.buffer().collect { … } main() send send send A B C collect A B C ❌ Channel Declarative & safe

Slide 133

Slide 133 text

FLOW EXECUTION CONTEXT Image: Context by Bart Everson

Slide 134

Slide 134 text

fun foo(): Flow = flowOf("A", "B", "C").map { name -> compute(name) }

Slide 135

Slide 135 text

fun foo(): Flow = flowOf("A", "B", "C").map { name -> compute(name) } fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } Where does it execute?

Slide 136

Slide 136 text

fun foo(): Flow = flowOf("A", "B", "C").map { name -> compute(name) } .flowOn(Dispatchers.Default) fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } Executes in background

Slide 137

Slide 137 text

fun foo(): Flow = flowOf("A", "B", "C").map { name -> compute(name) } .flowOn(Dispatchers.Default) Executes in collector’s context fun main() = runBlocking { val flow = foo() flow.collect { x -> println(x) } } Context preservation

Slide 138

Slide 138 text

FLOW IN REACTIVE UI

Slide 139

Slide 139 text

fun events(): Flow

Slide 140

Slide 140 text

fun events(): Flow scope.launch { … }

Slide 141

Slide 141 text

fun events(): Flow scope.launch { events().collect { event -> … } }

Slide 142

Slide 142 text

fun events(): Flow scope.launch { events().collect { event -> updateUI(event) } }

Slide 143

Slide 143 text

fun events(): Flow scope.launch { events().collect { event -> updateUI(event) } } “Subscribe” to events

Slide 144

Slide 144 text

fun events(): Flow events() .onEach { event -> updateUI(event) } .launchIn(scope) “Subscribe” to events

Slide 145

Slide 145 text

fun events(): Flow events() .onEach { event -> updateUI(event) } .launchIn(scope) “Subscribe” to events

Slide 146

Slide 146 text

MANAGING LIFETIME

Slide 147

Slide 147 text

Observable • ………. • ….. • ……..

Slide 148

Slide 148 text

Observable • ………. • ….. • …….. subscribe observable.subscribe { event -> updateUI(event) }

Slide 149

Slide 149 text

Observable • ………. • ….. • …….. subscribe SubscripIon observable.subscribe { event -> updateUI(event) }

Slide 150

Slide 150 text

Observable • ………. • ….. • …….. subscribe SubscripIon val composite = CompositeDisposable() composite.add(observable.subscribe { event -> updateUI(event) })

Slide 151

Slide 151 text

Observable • ………. • ….. • …….. subscribe SubscripIon val composite = CompositeDisposable() composite.add(observable.subscribe { event -> updateUI(event) }) composite.clear()

Slide 152

Slide 152 text

Flow • ………. • ….. • …….. launch Job CorouIneScope Structured Concurrency

Slide 153

Slide 153 text

Flow • ………. • ….. • …….. launch Job events() .onEach { event -> updateUI(event) } .launchIn(scope)

Slide 154

Slide 154 text

Flow • ………. • ….. • …….. launch Job val scope = MainScope() events() .onEach { event -> updateUI(event) } .launchIn(scope)

Slide 155

Slide 155 text

Flow • ………. • ….. • …….. launch Job val scope = MainScope() events() .onEach { event -> updateUI(event) } .launchIn(scope) scope.cancel()

Slide 156

Slide 156 text

Flow • ………. • ….. • …….. launch Job val scope = MainScope() events() .onEach { event -> updateUI(event) } .launchIn(scope) scope.cancel()

Slide 157

Slide 157 text

LiveData • ………. • ….. • …….. observe data.observe(livecycleOwner) { event -> updateUI(event) }

Slide 158

Slide 158 text

STATUS & ROADMAP What’s next?

Slide 159

Slide 159 text

Status & Roadmap Flow is stable in kotlinx.corouInes version 1.3.0 Future improvements ØOut-of-the box support for UI models (StateFlow / EventFlow) ØSharing / caching flows ØConcurrency / parallelism operators ØChunking / windowing operators Want more? Give us your feedback h2ps://github.com/Kotlin/kotlinx.corouInes/issues

Slide 160

Slide 160 text

Learn more vKotlin Flow by example guide https://kotlinlang.org/docs/reference/coroutines/flow.html vAPI docs https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/ vStories in my blog https://medium.com/@elizarov

Slide 161

Slide 161 text

#KotlinConf THANK YOU AND REMEMBER TO VOTE Roman Elizarov @relizarov #KotlinConf