Colton Idle
09/18/2024, 2:04 PMfun startCameraPreview(): SharedFlow<Frame> {
val events = MutableSharedFlow<PreviewFrame>()
cameraClient.addPreviewDataCallBack(
object : IPreviewDataCallBack {
override fun onPreviewData(...) {
scope.launch { events.emit(PreviewFrame(data, width, height)) }
}
})
return events.asSharedFlow()
}
This seems to work and I thought I wanted to treat this as a hot flow, but now I think a cold flow would work fine. Is there anything bad about having this setup this way? My biggest issue that I'm trying to prevent is for this flow to never be cleaned up
I initially didn't go with a callbackFlow because (from my limited time with them) they seem to be suited for cases where there's a terminal callback event where you can "shutdown" the flow. I don't see such an event from this 3rd party lib.Joffrey
09/18/2024, 2:06 PMcallbackFlow { ... }
that unregisters the callback on completion/cancellation, and then you can use shareIn
if you want to share this flow between consumers.Colton Idle
09/18/2024, 3:46 PMJoffrey
09/18/2024, 6:29 PMaddPreviewDataCallback
I was assuming there is also the "remove" equivalent?Joffrey
09/18/2024, 6:30 PMawaitClose { ... }
function in callbackFlow
to do thatColton Idle
09/18/2024, 6:35 PM