Skip to content

Instantly share code, notes, and snippets.

@pencelab
Last active October 20, 2020 09:05
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/d03e5e319ec309c3bc56f8ebbc38c759 to your computer and use it in GitHub Desktop.
Save pencelab/d03e5e319ec309c3bc56f8ebbc38c759 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.*
fun log(message: String) {
println("[${Thread.currentThread().name}] : $message")
}
fun main() {
log("Start")
val myFlow = MutableSharedFlow<String>(1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
myFlow.tryEmit("Initial Value")
runBlocking {
val job = launch(Dispatchers.Default) {
myFlow
.distinctUntilChanged()
.collect {
log("Collected => $it")
}
}
launch {
listOf(1, 2, 3, 3, 3, 4, 5, 6).forEach {
log("Updating => $it")
myFlow.emit("$it")
delay(300)
}
delay(1500)
job.cancel()
}
}
log("End")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment