Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pencelab/a19458085e017f7f771f01f3eca2e0fe to your computer and use it in GitHub Desktop.
Save pencelab/a19458085e017f7f771f01f3eca2e0fe to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun log(message: String) {
println("[${Thread.currentThread().name}] : $message")
}
fun main() {
log("Start")
val myFlow = MutableSharedFlow<String>(extraBufferCapacity = 3)
runBlocking {
val job = launch(Dispatchers.Default) {
delay(700)
myFlow
.onStart {
log("Collector started collecting...")
}
.collect {
log("Collected => $it")
delay(500)
}
}
launch {
listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14).forEach {
log("Emitting => $it")
myFlow.emit("$it")
delay(150)
}
delay(2000)
job.cancel()
}
}
log("End")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment