Skip to content

Instantly share code, notes, and snippets.

Avatar
☯️

Glenn pencelab

☯️
View GitHub Profile
View coroutines-con-kotlin.updates.sharedflow-06-output.txt
[main] : Start
[DefaultDispatcher-worker-1] : Emitting => 1
[DefaultDispatcher-worker-1] : Emitting => 2
[DefaultDispatcher-worker-1] : Emitting => 3
[DefaultDispatcher-worker-1] : Emitting => 4
[DefaultDispatcher-worker-1] : Emitting => 5
[main] : Collector started collecting...
[main] : Collected => 4
[DefaultDispatcher-worker-1] : Emitting => 6
[DefaultDispatcher-worker-1] : Emitting => 7
View coroutines-con-kotlin.updates.sharedflow-06-collapsed.kt
val myRepository = MyRepository()
val myComponent = MyComponent(myRepository)
...
val job = launch {
delay(1250)
myComponent.mySharedFlow
.onStart {
log("Collector started collecting...")
}
.collect {
View coroutines-con-kotlin.updates.sharedflow-06.kt
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.*
fun log(message: String) {
println("[${Thread.currentThread().name}] : $message")
}
class MyRepository {
fun getList(): List<Int> = (1..20).toList()
View coroutines-con-kotlin.updates.sharedflow-06-mycomponent.kt
class MyComponent(private val myRepository: MyRepository): CoroutineScope by CoroutineScope(Dispatchers.IO) {
private val _mySharedFlow = MutableSharedFlow<Int>(2, 3, BufferOverflow.DROP_OLDEST)
val mySharedFlow: SharedFlow<Int>
get() = _mySharedFlow
fun fetchAndEmitList() = launch {
myRepository.getList().forEach {
log("Emitting => $it")
_mySharedFlow.emit(it)
View coroutines-con-kotlin.updates.sharedflow-05-output.txt
[main] : Start
[DefaultDispatcher-worker-1] : Collector started collecting...
[main] : Emitting => 1
[main] : Emitting => 2
[main] : Emitting => 3
[main] : Emitting => 4
[main] : Emitting => 5
[main] : Emitting => 6
[main] : Emitting => 7
[main] : Emitting => 8
View coroutines-con-kotlin.updates.sharedflow-05-collapsed.kt
val myFlow = MutableSharedFlow<MyElement<Int>>(replay = 2)
...
launch(Dispatchers.Default) {
val lastValue = myFlow
.onStart {
log("Collector started collecting...")
}
.filter { it is MyElement.MyFinalToken }
.map { (myFlow.replayCache[0] as MyElement.MyToken).token }
.first()
View coroutines-con-kotlin.updates.sharedflow-05.kt
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
fun log(message: String) {
println("[${Thread.currentThread().name}] : $message")
}
sealed class MyElement<T> {
class MyToken<T>(val token: T): MyElement<T>()
class MyFinalToken<T>: MyElement<T>()
View coroutines-con-kotlin.updates.sharedflow-05-myelement.kt
sealed class MyElement<T> {
class MyToken<T>(val token: T): MyElement<T>()
class MyFinalToken<T>: MyElement<T>()
}
View coroutines-con-kotlin.updates.sharedflow-04-output.txt
[main] : Start
[main] : Emitting => 1
[main] : Emitting => 2
[main] : Emitting => 3
[main] : Emitting => 4
[main] : Emitting => 5
[main] : Emitting => 6
[main] : Emitting => 7
[main] : Emitting => 8
[DefaultDispatcher-worker-1] : Collector started collecting...
You can’t perform that action at this time.