Created
October 15, 2024 22:54
-
-
Save vprabhu/3473f8c69375ba6271b57d426fcfacfd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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