William Reed
07/21/2021, 12:51 PMdata class ApiResponse(
val someId: Long,
val success: Boolean,
)
suspend fun ApiResponse.poll(interval: Long) {
var result = success
while (!result) {
result = sendSomeApiRequest(someId)
delay(interval)
}
}
A requirement I need to support is allowing users of this function to change the interval
value while polling occurs based on outside information. would the most idiomatic way to do this be using a StateFlow
? so my above function would become
suspend fun ApiResponse.poll(interval: StateFlow<Long>)
Dominaezzz
07/21/2021, 2:07 PMWilliam Reed
07/21/2021, 2:08 PMBox
like AtomicLong
may work wellephemient
07/21/2021, 3:56 PMsuspend fun ApiResponse.poll(onRetry: suspend (ApiResponse) -> Unit) {
personally I'd probably do this - let the caller set up the delay, or whatever else is desired (print, counter, etc.)