Justin Breitfeller
04/10/2024, 1:31 PMreturn channelFlow {
launch(start = CoroutineStart.UNDISPATCHED) {
messageSourceFlow.collect { send(it)
}
try {
turnOnMessages()
awaitCancellation()
} finally {
turnOffMessages()
}
}
The key element is the launch(start = CoroutineStart.UNDISPATCHED) {
because I want to make sure I'm ready to receive messages before I call the turnOnMessages
method. Does this make sense? Is there an easier way to achieve this goal?ephemient
04/10/2024, 4:18 PMmessageSourceFlow
.onStart { turnOnMessages() }
.onCompletion { turnOffMessages() }
Justin Breitfeller
04/10/2024, 7:07 PMSam
04/11/2024, 6:50 AMmessageSourceFlow
comes from? How/why did it come to be a Flow
? I ask because if there's something behind it that's capable of sending messages while the flow isn't being collected, there's almost certainly a buffer in there too. For example, if it's a callbackFlow
, it has a buffer.Sam
04/11/2024, 6:52 AMchannelFlow
is redundant.Justin Breitfeller
04/11/2024, 5:57 PMmessageSourceFlow
is a SharedFlow
that is being populated from a background thread that's connected to some external service that sends me messages. I could setup a buffer on that shared flow but then I'd also potentially receive old messages since I can have multiple flows turning on / off messages (I basically reference count the calls to turn on/off to really control the server side flow).
I think for my question I'm really wondering if the launch with an UNDISPATCHED start is the best way to ensure I'm collecting before calling turnOnMessagsSam
04/11/2024, 6:00 PMonSubscription
instead of onStart
and that should solve your issueJustin Breitfeller
04/11/2024, 6:18 PMJustin Breitfeller
04/11/2024, 6:47 PMSam
04/12/2024, 10:26 AM