File tree Expand file tree Collapse file tree 4 files changed +104
-0
lines changed
app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/playground/flow/concurrency Expand file tree Collapse file tree 4 files changed +104
-0
lines changed Original file line number Diff line number Diff line change 1+ package com.lukaslechner.coroutineusecasesonandroid.playground.flow.concurrency
2+
3+ import kotlinx.coroutines.channels.BufferOverflow
4+ import kotlinx.coroutines.coroutineScope
5+ import kotlinx.coroutines.delay
6+ import kotlinx.coroutines.flow.buffer
7+ import kotlinx.coroutines.flow.flow
8+
9+ suspend fun main () = coroutineScope {
10+
11+ val flow = flow {
12+ repeat(5 ) {
13+ val pancakeIndex = it + 1
14+ println (" Emitter: Start Cooking Pancake $pancakeIndex " )
15+ delay(100 )
16+ println (" Emitter: Pancake $pancakeIndex ready!" )
17+ emit(pancakeIndex)
18+ }
19+ }.buffer(capacity = 1 , onBufferOverflow = BufferOverflow .SUSPEND )
20+
21+ flow.collect {
22+ println (" Collector: Start eating pancake $it " )
23+ delay(300 )
24+ println (" Collector: Finished eating pancake $it " )
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package com.lukaslechner.coroutineusecasesonandroid.playground.flow.concurrency
2+
3+ import kotlinx.coroutines.channels.BufferOverflow
4+ import kotlinx.coroutines.coroutineScope
5+ import kotlinx.coroutines.delay
6+ import kotlinx.coroutines.flow.buffer
7+ import kotlinx.coroutines.flow.flow
8+
9+ suspend fun main () = coroutineScope {
10+
11+ val flow = flow {
12+ repeat(5 ) {
13+ val pancakeIndex = it + 1
14+ println (" Emitter: Start Cooking Pancake $pancakeIndex " )
15+ delay(100 )
16+ println (" Emitter: Pancake $pancakeIndex ready!" )
17+ emit(pancakeIndex)
18+ }
19+ }.buffer(capacity = 1 , onBufferOverflow = BufferOverflow .DROP_OLDEST )
20+
21+ flow.collect {
22+ println (" Collector: Start eating pancake $it " )
23+ delay(300 )
24+ println (" Collector: Finished eating pancake $it " )
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package com.lukaslechner.coroutineusecasesonandroid.playground.flow.concurrency
2+
3+ import kotlinx.coroutines.channels.BufferOverflow
4+ import kotlinx.coroutines.coroutineScope
5+ import kotlinx.coroutines.delay
6+ import kotlinx.coroutines.flow.buffer
7+ import kotlinx.coroutines.flow.flow
8+
9+ suspend fun main () = coroutineScope {
10+
11+ val flow = flow {
12+ repeat(5 ) {
13+ val pancakeIndex = it + 1
14+ println (" Emitter: Start Cooking Pancake $pancakeIndex " )
15+ delay(100 )
16+ println (" Emitter: Pancake $pancakeIndex ready!" )
17+ emit(pancakeIndex)
18+ }
19+ }.buffer(capacity = 1 , onBufferOverflow = BufferOverflow .DROP_LATEST )
20+
21+ flow.collect {
22+ println (" Collector: Start eating pancake $it " )
23+ delay(300 )
24+ println (" Collector: Finished eating pancake $it " )
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package com.lukaslechner.coroutineusecasesonandroid.playground.flow.concurrency
2+
3+ import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
4+ import kotlinx.coroutines.coroutineScope
5+ import kotlinx.coroutines.delay
6+ import kotlinx.coroutines.flow.buffer
7+ import kotlinx.coroutines.flow.flow
8+
9+ suspend fun main () = coroutineScope {
10+
11+ val flow = flow {
12+ repeat(5 ) {
13+ val pancakeIndex = it + 1
14+ println (" Emitter: Start Cooking Pancake $pancakeIndex " )
15+ delay(100 )
16+ println (" Emitter: Pancake $pancakeIndex ready!" )
17+ emit(pancakeIndex)
18+ }
19+ }.buffer(capacity = UNLIMITED )
20+
21+ flow.collect {
22+ println (" Collector: Start eating pancake $it " )
23+ delay(300 )
24+ println (" Collector: Finished eating pancake $it " )
25+ }
26+ }
You can’t perform that action at this time.
0 commit comments