Skip to content

Instantly share code, notes, and snippets.

@tateisu
Created February 2, 2021 21:57
Show Gist options
  • Save tateisu/f1cfd32a28c672e88009f7bd16669159 to your computer and use it in GitHub Desktop.
Save tateisu/f1cfd32a28c672e88009f7bd16669159 to your computer and use it in GitHub Desktop.
scratch
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