Lilly
09/08/2021, 11:56 AMsuspend fun readPacketFlow(): Flow<Packet> = flow {
api.readByteFlow().collect { bytes ->
// parse the bytes and build packets from it
...
val packet: Packet = someOperation()
emit(packet)
}
I'm observing strange behavior. On the caller site I'm collecting the flow like:
fun requestPacket() {
coroutineScope {
launch {
dataSource.readPacketFlow()
.onStart { Log.d("PacketFlow", "starting packet flow.") } // logged
.onCompletion { Log.d("PacketFlow", "completing packet flow.") } // logged
.collect {
val packet = it as ResponsePacket
Log.i("Received in Flow", "bytes: [${packet.bytes.toHexString()}]") // not logged
}
Log.i("Request", "after readPacketFlow.") // not logged
}
}
Log.i("Request", "after scope.") // not logged
}
None of the logs (except the first two) are logged. I don't get what's going on here. Any ideas?Dominaezzz
09/08/2021, 11:59 AMLilly
09/08/2021, 12:02 PMDominaezzz
09/08/2021, 12:03 PMLilly
09/08/2021, 12:03 PMval readPacketFlow(): Flow<Packet>
get() = api.readByteFlow().map { // do all the stuff here }
Dominaezzz
09/08/2021, 12:05 PMLilly
09/08/2021, 12:07 PMDominaezzz
09/08/2021, 12:12 PMLilly
09/08/2021, 12:19 PMLilly
09/08/2021, 2:29 PMpublic fun <T, R> Flow<T>.runningFold(initial: R, @BuilderInference operation: suspend (accumulator: R, value: T) -> R): Flow<R> = flow {
var accumulator: R = initial
emit(accumulator)
collect { value ->
accumulator = operation(accumulator, value)
emit(accumulator)
}
}
This is a general question, not specific to the case above. So to be clear, it's not a no go to do this?Dominaezzz
09/08/2021, 2:47 PM