Skip to content

Instantly share code, notes, and snippets.

@vprabhu
Created October 15, 2024 22:54
Show Gist options
  • Select an option

  • Save vprabhu/3473f8c69375ba6271b57d426fcfacfd to your computer and use it in GitHub Desktop.

Select an option

Save vprabhu/3473f8c69375ba6271b57d426fcfacfd to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
suspend fun main(): Unit = coroutineScope {
println("State Flows")
val _value = MutableStateFlow(Long.MIN_VALUE)
val value = _value.asStateFlow()
launch {
update(_value)
}
launch {
delay(1000)
repeat(2){
println("Collect 1 ${value.value}")
delay(5000)
}
}
launch {
delay(1250)
println("Collect 2 ${value.value}")
}
launch {
delay(3500)
println("Collect 3 ${value.value}")
}
}
suspend fun update(_value: MutableStateFlow<Long>) {
repeat(5) {
_value.update {
delay(500)
System.currentTimeMillis()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment