Is there a specific way to mark a `MutableSharedFl...
# coroutines
r
Is there a specific way to mark a
MutableSharedFlow
that it won't get any new messages, so `collect`s terminate?
s
Nope, shared flows can never terminate.
c
No. From the docs, “Shared flow never completes.” This is by design, as SharedFlows are meant for sharing values to multiple subscribers, and are not designed as a general-purpose communication mechanism with a “lifecycle”. If you want the collectors to respond to some “close” event, you’ll need to pass a value as a “close token” into the SharedFlow and collectors must use
takeWhile
to respond to it themselves. Alternatively, use a Channel, which can be explicitly closed.
r
With a
Channel
, can I expose a
Flow
where all consumers get the same data?
receiveAsFlow()
seems to spread the elements.
c
channel.receiveAsFlow().shareIn()
should do the trick
1