Lilly
03/23/2021, 4:54 PMwhile (!packetChannel.isEmpty) {
packetChannel.receive()
}
in flows when I convert the channel to a flow:
packetChannel.receiveAsFlow()
Edit: I would like to consume the channel as a flow, but instead of collect all values then parse I would like to collect one value, parse it, wait for next one then emit final resultDominaezzz
03/23/2021, 4:56 PMpacketChannel.receiveAsFlow().toList()
Lilly
03/23/2021, 5:06 PMephemient
03/23/2021, 5:07 PMpacketFlow.collect {
// it: Packet
}
Lilly
03/23/2021, 5:29 PMfun reutnResult() = flow {
val destination = mutableMapOf()
subscribeToPacketChannel().collect { value ->
// do some heavy work
// put result of work into destination
// when done close this -> how to notice?
}
...
emit(destination) // result
}
Erik
03/23/2021, 5:30 PMLilly
03/23/2021, 5:31 PMErik
03/23/2021, 5:32 PMLilly
03/23/2021, 5:32 PMpacketChannel.isEmpty
in flow?Erik
03/23/2021, 5:34 PMLilly
03/23/2021, 5:40 PMSend a token that indicates that you've received enough items to be emitted?The problem here I currently noticed is that I don't collect prmitves but instead a
ResponsePacket
so I would have to fake one to indicate last valueErik
03/23/2021, 5:42 PMLilly
03/23/2021, 5:57 PMephemient
03/23/2021, 5:58 PMLilly
03/23/2021, 6:06 PMinternal suspend fun sendPacketWithBatchResponse(
packet: RequestPacket
onResponse: suspend (List<ResponsePacket>) -> Unit
) {
sendPacketWithConfirmation(packet)
onResponse(
mutableListOf<ResponsePacket>().apply {
while (!packetChannel.isEmpty) {
add(packetChannel.receive())
}
}
)
}
TBH I don't even know why this while
is working when I think about it now. But it works and it returns a group. Then I do my processing. But this performs synchronous. You have to know that I make a request to the device and the device could reponse with 1, 2,3 or 30 packets depending on the request I send. I would like to shift to an asynchronously approach, that says I collect one packet and while others are on the way to arrive I can process the arrived packetsErik
03/23/2021, 6:14 PMLilly
03/23/2021, 6:18 PMephemient
03/23/2021, 6:26 PMLilly
03/23/2021, 6:31 PMNatsuki(开元米粉实力代购)
03/24/2021, 6:09 AMfold
fits your needs?
val result = flowOf(1,2,3,4).fold(init) { acc,ele ->
// do what ever with ele and acc, return the desired acc if you want to use it next iteration
}
println(result) // flow has finished, the last result you returned will print here
Lilly
03/24/2021, 11:37 AM