Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save vprabhu/adf153e7f3cbe6f5c39a4efe2c5d3400 to your computer and use it in GitHub Desktop.
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch
suspend fun main(): Unit = coroutineScope {
val sharedFLow = MutableSharedFlow<Long>(
replay = 1,
extraBufferCapacity = 2
)
launch {
sendUpdates(sharedFLow)
}
launch {
sharedFLow.collect {
println("Collector 1 Current Time : $it")
}
}
launch {
delay(4000)
sharedFLow.collect {
println("Collector 2 Current Time : $it")
}
}
}
suspend fun sendUpdates(sharedFlow: MutableSharedFlow<Long>) {
repeat(15) {
delay(200)
val cTime = System.currentTimeMillis()
sharedFlow.emit(cTime)
if(it == 12){
delay(4000)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment