groostav
10/31/2021, 10:54 PMFlow()
and somebody called first()
, we should not cancel the remaining elements. The remaining elements should instead be produced and simply left un-consumed). You do have to kick it off. If you forget about it in most cases it'l complete normally but it some it will hang open, like a resource that needs to be disposed.
Should I make this thing into a ReceiveChannel
or a Flow
?Joffrey
10/31/2021, 10:55 PMgroostav
10/31/2021, 11:02 PMgroostav
10/31/2021, 11:04 PMJoffrey
10/31/2021, 11:31 PMReceiveChannel
, each element goes to a single consumer. If you have 2 concurrent consumers, each of them will receive approximatively half of the elements. So that might not be suitable for you.
With a ShareFlow
, you can sort of make it terminate by sending a sentinel value, and using takeWhile
after sharing the flow. Collectors will then stop. To support cancellation of upstream, you could use SharingStarted.WhileSubscribed
when starting your shared flow, which cancels the upstream when there are 0 subscribers.andylamax
11/01/2021, 5:45 AMI accidentally hit enter before I finish typing
That is slack experience at its finest
andylamax
11/01/2021, 5:49 AMgroostav
11/01/2021, 6:21 AMandylamax
11/01/2021, 6:30 AMgroostav
11/01/2021, 6:39 AMfirst()
to kill the producer. The producer is actually a process, so killing it would end up as kill -9 subprocess
, which just, even if your not interested in its output, doesn't seem great.Tijl
11/01/2021, 7:15 AMfirst()
and then later again (or collect
or another terminator) then you have more than one consumergroostav
11/03/2021, 7:09 PMconsumeAsFlow()
vs receiveAsFlow()
, where first()
was throwing FlowAborted
at the flow and the channel implementing it was marked as consumed.groostav
11/03/2021, 7:13 PMsuspend fun next(): T
and have access to all the fancy combinators one would expect, like map
and filter
. From this angle, the concept of "is this flow finished" or "do you have multiple consumers" or "which is the start element" are fairly elegantly answered by the Iterator
interface:
• "is this flow finished", => ask hasNext()
• "do you have multiple consumers" => I dont know, but if you want a fan-out build it yourself
• "which is the start element" => I don't know, if you wanted the start element you should've buffered it yourself.groostav
11/03/2021, 7:14 PMTijl
11/03/2021, 7:14 PMSharedFlow
groostav
11/03/2021, 7:14 PMgroostav
11/03/2021, 7:15 PM