Hello everyone, is it a bad practice to expose flo...
# coroutines
k
Hello everyone, is it a bad practice to expose flow inner channel like this?
Copy code
fun scan() = callbackFlow<Device> {
    deviceChannel = channel
    hardwareChecker.scanDevices()
}
The goal is to offer from multiple places data to the same flow.
b
If you call
scan()
multiple times - only last subscription will receive updates
k
@bezrukov that absolutelly fine
@bezrukov by the way, there maybe be a lot of forever suspended channels, will be created🤔
d
Unless the scandevices method suspends, all the channels will be closed immediately. Actually an error will be thrown since you don't call awaitClose.
💪 1
☝️ 1
z
I think this is probably dangerous – like Kirill said, a new channel is going to get created every time someone collects this Flow, and they’re never going to be cleaned up. If you want to just launch a single job and publish the scan results as a Flow, you could create a channel yourself and use
receiveAsFlow
to turn it into a flow. However this will just round-robin the results to each collector, which is also probably not what you want. You probably want to create a
BroadcastChannel
to send results on and use
asFlow()
to expose it to your consumers.
❤️ 1