So, here's a task. A background thread may or may not call all of it's listeners some time after sendRequest()
was called (in other words sendRequest()
schedules a possible event). The required function must:
- Subscribe to those events
- Call
sendRequest()
periodically until the function returns anything - Return the first suitable result (filtering is done in the listener and is omitted in this snippet) or
null
after a timeout - Unsubscribe from those events regardless of the result
suspend fun findServer(): Server? {
var listener: ((Server) -> Unit)? = null
return try {
withTimeoutOrNull(10000) {
suspendCancellableCoroutine<Server> { continuation ->
listener = { server: Server ->
continuation.resume(server)
}.also { discoveryService.addListener(it) }
launch {
while (continuation.isActive) {
discoveryService.sendRequest()
delay(1000)
}
}
}
}
} finally {
listener?.let { discoveryService.removeListener(it) }
}
}
The current implementation seems to work fine, but the nullable listener
variable feels kind of clunky and requires using also
to keep the listener reference, which in turn breaks type inference and makes me specify listener's parameter type (no big deal, but...). I could use a lateinit var here, but it probably can lead to exceptions when withTimeoutOrNull()
stops before the listener was initialized.
Any recommendations on improving this snippet? Or am I, in fact, doing everything totally wrong here?