Skip to content

Instantly share code, notes, and snippets.

Avatar
☯️

Glenn pencelab

☯️
View GitHub Profile
View coroutines-con-kotlin.updates.sharedflow-04-collapsed.kt
val myFlow = MutableSharedFlow<String>(replay = 10)
...
val job = launch(Dispatchers.Default) {
delay(750)
myFlow
.onStart {
log("Collector started collecting...")
}
.collect {
log("Collected => $it")
View coroutines-con-kotlin.updates.sharedflow-04.kt
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun log(message: String) {
println("[${Thread.currentThread().name}] : $message")
}
fun main() {
log("Start")
View coroutines-con-kotlin.updates.sharedflow-03-collapsed.kt
val myFlow = MutableSharedFlow<String>(extraBufferCapacity = 3)
...
val job = launch(Dispatchers.Default) {
delay(700)
myFlow
.onStart {
log("Collector started collecting...")
}
.collect {
log("Collected => $it")
View coroutines-con-kotlin.updates.sharedflow-03-output.txt
[main] : Start
[main] : Emitting => 1
[main] : Emitting => 2
[main] : Emitting => 3
[main] : Emitting => 4
[main] : Emitting => 5
[DefaultDispatcher-worker-1] : Collector started collecting...
[main] : Emitting => 6
[DefaultDispatcher-worker-1] : Collected => 6
[main] : Emitting => 7
View coroutines-con-kotlin.updates.sharedflow-03.kt
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun log(message: String) {
println("[${Thread.currentThread().name}] : $message")
}
fun main() {
log("Start")
View coroutines-con-kotlin.updates.sharedflow-02-collapsed.kt
val myFlow = MutableSharedFlow<String>(1, onBufferOverflow = BufferOverflow.DROP_OLDEST)
myFlow.tryEmit("Initial Value")
...
val job = launch(Dispatchers.Default) {
myFlow
.distinctUntilChanged()
.collect {
log("Collected => $it")
}
}
View coroutines-con-kotlin.updates.sharedflow-01-collapsed.kt
val myFlow = MutableStateFlow("Initial Value")
...
val job = launch(Dispatchers.Default) {
myFlow.collect {
log("Collected => $it")
}
}
...
launch {
listOf(1, 2, 3, 3, 3, 4, 5, 6).forEach {
View coroutines-con-kotlin.updates.sharedflow-02-output.txt
[main] : Start
[DefaultDispatcher-worker-1] : Collected => Initial Value
[main] : Updating => 1
[DefaultDispatcher-worker-1] : Collected => 1
[main] : Updating => 2
[DefaultDispatcher-worker-1] : Collected => 2
[main] : Updating => 3
[DefaultDispatcher-worker-1] : Collected => 3
[main] : Updating => 3
[main] : Updating => 3
View coroutines-con-kotlin.updates.sharedflow-02.kt
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")
You can’t perform that action at this time.