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()
}
Scanner
elizarov
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 PMcancel
doesn't do anythinguseCancellably
, it throws an exception and aborts the connection attemptURL
streamsisActive
is only useful for computational workelizarov
10/18/2018, 5:01 PMSUPERCILEX
10/18/2018, 5:02 PMelizarov
10/18/2018, 5:09 PMSUPERCILEX
10/18/2018, 5:14 PM