George
07/22/2022, 1:15 PMGeorge
07/22/2022, 1:52 PMGeorge
07/22/2022, 2:26 PMprivate val sender: MutableSharedFlow<Message> = MutableSharedFlow()
override fun stream(client: Client): Flow<Message> {
return sender
.onSubscription { emitAll(latest(client)) }
.filter { getClient(it.client) == client }
}
1️⃣
Con: the filter is applied also on the elements from subscription which i already make sure they are the right ones.
private val messageCache: ConcurrentMap<Client, MutableSharedFlow<Message>> =
ConcurrentHashMap<Client, MutableSharedFlow<Message>>().apply {
put(Csp1, MutableSharedFlow())
put(Csp2, MutableSharedFlow())
}
override fun stream(client: Client): Flow<Message> {
return messageCache[client]!!.onSubscription {
latest(client)
}
}
2️⃣
Con: Not sure if it really is a con but i need a new MutableSharedFlow for every new client (which they are not that many tbh.)
Pro: the filter operator is no longer needed at all.
ps: i really dont like the !! 😛Tijl
07/22/2022, 3:00 PMFlow
from your filter
is cold not hot. Not sure why you would want to avoid shareIn
if you need a SharedFlow
after your filter. Especially since you don’t see to use any replay or buffer.George
07/22/2022, 3:02 PMTijl
07/22/2022, 3:12 PMfilter
doesn’t care or know whether the upstream flow is cold or hot, it will only collect the upstream flow when it is collected.
e.g. see this example: https://pl.kotl.in/gja9cjw0ZGeorge
07/22/2022, 3:16 PMGeorge
07/22/2022, 3:24 PMTijl
07/22/2022, 3:25 PMTijl
07/22/2022, 3:26 PMTijl
07/22/2022, 3:26 PMTijl
07/22/2022, 3:27 PMfilter
and that is it TBH (no need to turn it back into a SharedFlow after), but of course I don’t know your exact use caseGeorge
07/22/2022, 3:30 PMonSubscription
after the filter
so i was looking for a way to turn it back. But i think the code is a bit cleaner like that.
If you dont mind one more question: Do you prefer 1 or 2 choice? I think i prefer the 2nd one.Tijl
07/22/2022, 3:45 PMlatest
does.Francesc
07/22/2022, 4:48 PMGeorge
07/22/2022, 5:20 PM