Hugh Edwards
02/18/2023, 4:06 AMHugh Edwards
02/19/2023, 12:48 AMHugh Edwards
02/19/2023, 12:48 AMGlobalScope(<http://Dispatchers.IO|Dispatchers.IO>)
coroutine that initiates the socket connection, returning a flow (via callback flow) of transformed socket messages as domain objects
I popped a .flowOn(<http://dispatchers.io|dispatchers.io>)
on that callback flow (though looking again now I think that flow should already exist in the io scope..?)Hugh Edwards
02/19/2023, 12:48 AMprivate val _messageFlow = MutableSharedFlow<DomainMessage>()
GlobalScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) {
// what ive just described above
val flow = initiateSocketConnection()
launch(Dispatchers.Default) {
flow.collect { message ->
_messageFlow.emit(message)
}
}
}
Hugh Edwards
02/19/2023, 12:48 AMs3rius
02/20/2023, 10:11 PMinitiateSocketConnection
to be called a bunch of times. In that case it seems sensible to use a shared flow.
The pattern you are after (collect everything from a flow and put it in a shared flow) you can use shareIn. The example even has something pretty close to what you're building.
So it could roughly look like this
public val messageFlow = initiateSocketConnection()
.shareIn(GlobalScope, SharingStarted.Eagerly, 0)
s3rius
02/20/2023, 10:11 PMs3rius
02/20/2023, 10:15 PMSharingStated.WhileSubscribed()
Hugh Edwards
02/21/2023, 12:04 AMHugh Edwards
02/21/2023, 12:05 AM