https://kotlinlang.org logo
Title
n

natario1

04/11/2021, 10:47 AM
Is it possible to have something like
SharingStarted.WhileSubscribed()
with the extra feature that if the flow has never emitted, wait for first emission before stopping? In other words, if someone subscribes but then leaves before receiving the first value, wait for it before stop. Looking at source code it seems quite easy to modify
WhileSubscribed
to do this (just add sharedFlow.first()). But we don't have the SharedFlow when creating
SharingStarted
, we only have the upstream flow which means that values in the replay cache are ignored.
d

Dominaezzz

04/11/2021, 11:04 AM
I feel like this could be done with a custom Flow operator on top of the sharedFlow.
n

natario1

04/11/2021, 11:08 AM
Yeah I was just thinking about this, using standard
WhileSubscribed
then launching a coroutine that collects the subscription count and calls first() on first subscriber. Basically add a fake subscription. Not sure if it's the best way
d

Dominaezzz

04/11/2021, 11:17 AM
Could also make a custom
SharingStarted
where you set the
SharedFlow
later. Not the prettiest though.
👍 2