https://kotlinlang.org logo
Title
y

yschimke

12/20/2021, 9:56 AM
Are there nice patterns in Kotlin Coroutines/Flows for having a state machine based on the inflight requests? On each request start/completion - have it evaluate whether to change some app state.
This is for android with multiple networks (BLE, Wifi, LTE). Say I have different requests (suspending functions) that have different network QoS, and I want to activate certain networks when media requests are made. This should request a particular network (I have this code already). Image requests are lower priority, but should use higher bandwidth network if already open. When all high priority requests are complete, I'll free the network and close existing connections.
a

Alex Vanyo

12/22/2021, 6:23 PM
`suspend fun`s are state machines 😄 . If you have some object that can intercept all of the requests, you can keep track of some state as requests start and end. If you need to guard that state with concurrent requests happening, you might be able to use a
Mutex
to control that.
y

yschimke

12/23/2021, 10:41 AM
Yep - I like this. I suspect every bodies case is subtely different, but once I've implemented it I'll use it a few times. So I'll probably but it behind some abstraction, so the network code isn't dealing with mutexes (the Single Level of Abstraction (SLA) principle).
val requestGate = RequestGate<R, S>(onRequestChange: <S, List<R>> -> S)
or similar. Thanks!