https://kotlinlang.org logo
Title
d

diesieben07

11/17/2020, 6:21 PM
I can share ("fan-out") a Flow between multiple collectors while only collecting the original flow once using
shareIn
. However this does not support a finite flow, as
shareIn
makes the flow endless ("Shared flow never completes"). How can the collectors of the shared flow ever complete? Even if the original flow completes, they will continue to "hang", expecting more elements, because the shared flow never completes.
z

Zach Klippenstein (he/him) [MOD]

11/17/2020, 6:26 PM
You could use a sentinel and takeWhile downstream of your sharing operator
l

louiscad

11/17/2020, 6:30 PM
Easiest if you have a non nullable type is to send null, and takeWhile not null, plus use
filterNotNull
to get back the non null type.
d

diesieben07

11/17/2020, 6:31 PM
val shared = data.onCompletion<ByteBuffer?> { emit(null) }
                .shareIn(this, sharingStartedAfter(delegates.size))
                .takeWhile { it != null }
                .filterNotNull()
Thanks. I hate it. 😄
😂 5
But it does work.