SUPERCILEX
10/18/2018, 4:51 PMpublic suspend inline fun <T : Closeable?, R> T.useCancellably(
crossinline block: (T) -> R
): R = suspendCancellableCoroutine { cont ->
cont.invokeOnCancellation { this?.close() }
use(block)
}
elizarov
10/18/2018, 4:55 PMSUPERCILEX
10/18/2018, 4:55 PMelizarov
10/18/2018, 4:56 PMSUPERCILEX
10/18/2018, 4:56 PMrunBlocking {
val foo = launch(<http://Dispatchers.IO|Dispatchers.IO>) {
ServerSocket(PORT).useCancellably {
it.accept()
// ...
}
}
delay(100)
print("Cancelling")
foo.cancel()
}
SUPERCILEX
10/18/2018, 4:57 PMScanner
elizarov
10/18/2018, 4:57 PMelizarov
10/18/2018, 4:57 PMuse
inside a coroutine? I don’t see why you need a special function.SUPERCILEX
10/18/2018, 4:57 PMSUPERCILEX
10/18/2018, 4:57 PMcancel
doesn't do anythingSUPERCILEX
10/18/2018, 4:58 PMuseCancellably
, it throws an exception and aborts the connection attemptSUPERCILEX
10/18/2018, 4:58 PMURL
streamsSUPERCILEX
10/18/2018, 4:58 PMSUPERCILEX
10/18/2018, 4:59 PMSUPERCILEX
10/18/2018, 4:59 PMisActive
is only useful for computational workelizarov
10/18/2018, 5:01 PMelizarov
10/18/2018, 5:01 PMSUPERCILEX
10/18/2018, 5:02 PMSUPERCILEX
10/18/2018, 5:02 PMelizarov
10/18/2018, 5:09 PMSUPERCILEX
10/18/2018, 5:14 PMSUPERCILEX
10/18/2018, 5:14 PM