Make cronetRequest.execute(..., () -> R): R
This commit is contained in:
1 parent
5d4f544b94
commit
b28fc3ffc4
3 files changed
+12
-14
No files matched your search
@@ -133,8 +133,7 @@ private fun rethrowExactly(code: Int, headers: Headers, body: String, e: Throwab
|
||||
val httpContentPool = DirectByteBufferPool(16, 16384)
|
||||
|
||||
suspend inline fun <T> fetchCompat(url: String, referer: String? = null, crossinline parser: (ByteBuffer) -> T): T {
|
||||
val ret: T
|
||||
if (isCronetSupported) {
|
||||
return if (isCronetSupported) {
|
||||
cronetRequest(url, referer).execute {
|
||||
val buffer = httpContentPool.borrow()
|
||||
try {
|
||||
@@ -142,7 +141,7 @@ suspend inline fun <T> fetchCompat(url: String, referer: String? = null, crossin
|
||||
buffer.put(it)
|
||||
}
|
||||
buffer.flip()
|
||||
ret = parser(buffer)
|
||||
parser(buffer)
|
||||
} finally {
|
||||
httpContentPool.recycle(buffer)
|
||||
}
|
||||
@@ -155,13 +154,12 @@ suspend inline fun <T> fetchCompat(url: String, referer: String? = null, crossin
|
||||
if (body.source().read(buffer) == -1) break
|
||||
}
|
||||
buffer.flip()
|
||||
ret = parser(buffer)
|
||||
parser(buffer)
|
||||
} finally {
|
||||
httpContentPool.recycle(buffer)
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
|
||||
|
||||
@@ -26,16 +26,16 @@ class CronetHttpUriFetcher(private val data: String, private val options: Option
|
||||
val diskCacheKey = options.diskCacheKey ?: data
|
||||
val diskCache = requireNotNull(imageLoader.diskCache)
|
||||
val snapshot = diskCache.openSnapshot(diskCacheKey) ?: run {
|
||||
cronetRequest(data) {
|
||||
val success = cronetRequest(data) {
|
||||
disableCache()
|
||||
}.execute {
|
||||
val success = diskCache.suspendEdit(diskCacheKey) {
|
||||
diskCache.suspendEdit(diskCacheKey) {
|
||||
RandomAccessFile(data.toFile(), "rw").use {
|
||||
copyToChannel(it.channel)
|
||||
}
|
||||
}
|
||||
check(success)
|
||||
}
|
||||
check(success)
|
||||
// diskcache snapshot MUST exist here
|
||||
requireNotNull(diskCache.openSnapshot(diskCacheKey))
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ class CronetRequest {
|
||||
lateinit var consumer: (ByteBuffer) -> Unit
|
||||
lateinit var onResponse: CronetRequest.(UrlResponseInfo) -> Unit
|
||||
lateinit var request: UrlRequest
|
||||
lateinit var daemonCont: Continuation<Boolean>
|
||||
lateinit var onError: (Throwable) -> Unit
|
||||
lateinit var readerCont: Continuation<Unit>
|
||||
val callback = object : UrlRequest.Callback() {
|
||||
override fun onRedirectReceived(req: UrlRequest, info: UrlResponseInfo, url: String) {
|
||||
@@ -64,11 +64,10 @@ class CronetRequest {
|
||||
|
||||
override fun onSucceeded(req: UrlRequest, info: UrlResponseInfo) {
|
||||
readerCont.resume(Unit)
|
||||
daemonCont.resume(true)
|
||||
}
|
||||
|
||||
override fun onFailed(req: UrlRequest, info: UrlResponseInfo?, e: CronetException) {
|
||||
daemonCont.resumeWithException(e)
|
||||
onError(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,6 +91,7 @@ suspend inline fun CronetRequest.awaitBodyFully(crossinline callback: (ByteBuffe
|
||||
buffer.clear()
|
||||
request.read(buffer)
|
||||
}
|
||||
onError = { readerCont.resumeWithException(it) }
|
||||
readerCont = cont
|
||||
request.read(buffer)
|
||||
}
|
||||
@@ -105,15 +105,15 @@ suspend inline fun CronetRequest.copyToChannel(chan: FileChannel, crossinline li
|
||||
listener(bytes)
|
||||
}
|
||||
|
||||
suspend inline fun CronetRequest.execute(crossinline callback: suspend CronetRequest.(UrlResponseInfo) -> Unit): Boolean {
|
||||
suspend inline fun <R> CronetRequest.execute(crossinline callback: suspend CronetRequest.(UrlResponseInfo) -> R): R {
|
||||
contract {
|
||||
callsInPlace(callback, InvocationKind.EXACTLY_ONCE)
|
||||
}
|
||||
return coroutineScope {
|
||||
onResponse = { launch { callback(it) } }
|
||||
suspendCancellableCoroutine { cont ->
|
||||
onResponse = { launch { cont.resume(callback(it)) } }
|
||||
cont.invokeOnCancellation { request.cancel() }
|
||||
daemonCont = cont
|
||||
onError = { cont.resumeWithException(it) }
|
||||
request.start()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user