-
-
Save tateisu/f1cfd32a28c672e88009f7bd16669159 to your computer and use it in GitHub Desktop.
scratch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.* | |
import kotlinx.coroutines.flow.MutableSharedFlow | |
import kotlinx.coroutines.flow.collect | |
println("scratch start") | |
runBlocking{ | |
println("test start") | |
val receivedValues = mutableListOf<Int>() | |
val sharedFlow = MutableSharedFlow<Int>() | |
val job = GlobalScope.launch{ | |
println("before collect") | |
sharedFlow.collect { | |
println("collect: $it") | |
synchronized(receivedValues) { | |
receivedValues.add(it) | |
if(receivedValues.size >= 2) cancel() | |
} | |
} | |
} | |
delay(100L) | |
println("before emits") | |
sharedFlow.emit(1) | |
sharedFlow.emit(2) | |
println("wait end..") | |
job.join() | |
println("receivedValues=$receivedValues") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment