Hi, I'm trying to find the best primitive to use f...
# coroutines
p
Hi, I'm trying to find the best primitive to use for my use case, maybe in coroutines? maybe in rx? something else perhaps..? I have some state that I'd like to represent as some kind of Flow. I'd like to have multiple subscribers, a default value when subscribing (the default value would be computed at the subscribe time). I need to be able to add more data to that flow at runtime (like you can do with a
ConflatedBroadcastChannel
) and ideally, one subscriber could unsubscribe without disrupting the other subscribers.
b
so use
ConflatedBroadcastChannel
🙂 to consume it as flow just use
channel.asFlow()
if you subscribe to cbc after initial data comes to the channel, you will receive the last element. Or do you want to receive every element sent to channel before you subscribed?
p
I want to receive the initial data (that is different for each subscribers) only
but you saying this makes me realise something... so I'm using this pattern of
SomeSortOfFlow<T>
to represent initial data + changes over time
b
e.g.
Copy code
val channel = ConflatedBroadcastChannel()
channel.offer(1)
channel.offer(2)

channel.asFlow().collect {
// what are you expecting there? 1? 2? 1,2?
}
p
when maybe I could use
SomeSortOfFlow<List<T>>
to represent the complete state over time
b
yes, CBC is usually used for holding the whole state
p
to answer your question, in
collect
I want to receive 1, 2 but also everything that will eventually be coming after
channel.asFlow().collect
In reality, what I'm looking for is more something like when doing
subscribe()
or whatever method it is, I need to query database to get the whole state, and then over time I want to get the changes individually to do something somewhere else with those changes
Not sure if this is some kind of antipattern or something like that though
u
is there a flow.first() flow.startWith?
I guess flow.flattenConcat() would do.