Pacane
03/31/2020, 12:26 PMsuspend fun fastSubscribe(path: String): Value =
suspendCancellableCoroutine { cont: CancellableContinuation<Value> ->
val handler = object : Handler<SubscriptionValue> {
override fun handle(event: SubscriptionValue) {
requester.unsubscribe(path, this)
cont.resume(event.value)
}
}
requester.subscribe(path, handler)
}
Here I want to finish the execution with the value in the callback when I have one, but I'd like to add a timeout for if I never get a value (ie: when handle
is never called)
I think I should use a withTimeout
in there but I don't know where exactly. On timeout I need to call requester.unsubscribe(handler)
, so having the withTimeout
on the call site of fastSubscribe
isn't really a solution. Is there anything I can do with this?tseisel
03/31/2020, 12:34 PMwithTimeout
will try to cancel the running coroutine after the time limit has been reached, you could register a listener with cont.invokeOnCancellation
to dispose the subscription if its result has not been received.streetsofboston
03/31/2020, 12:36 PMstreetsofboston
03/31/2020, 12:39 PMPacane
03/31/2020, 12:39 PMPacane
03/31/2020, 12:45 PM