|
|
@ -1,6 +1,8 @@ |
|
|
|
package io.legado.app.help.coroutine |
|
|
|
package io.legado.app.help.coroutine |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.util.Log |
|
|
|
import kotlinx.coroutines.* |
|
|
|
import kotlinx.coroutines.* |
|
|
|
|
|
|
|
import kotlin.coroutines.CoroutineContext |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Coroutine<T>(scope: CoroutineScope, block: suspend CoroutineScope.() -> T) { |
|
|
|
class Coroutine<T>(scope: CoroutineScope, block: suspend CoroutineScope.() -> T) { |
|
|
@ -17,14 +19,12 @@ class Coroutine<T>(scope: CoroutineScope, block: suspend CoroutineScope.() -> T) |
|
|
|
|
|
|
|
|
|
|
|
private val job: Job |
|
|
|
private val job: Job |
|
|
|
|
|
|
|
|
|
|
|
private var start: (suspend CoroutineScope.() -> Unit)? = null |
|
|
|
private var start: VoidCallback? = null |
|
|
|
private var execute: (suspend CoroutineScope.(T?) -> Unit)? = null |
|
|
|
private var success: Callback<T?>? = null |
|
|
|
private var success: (suspend CoroutineScope.(T?) -> Unit)? = null |
|
|
|
private var error: Callback<Throwable>? = null |
|
|
|
private var error: (suspend CoroutineScope.(Throwable) -> Unit)? = null |
|
|
|
private var finally: VoidCallback? = null |
|
|
|
private var finally: (suspend CoroutineScope.() -> Unit)? = null |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private var timeMillis: Long? = null |
|
|
|
private var timeMillis: Long? = null |
|
|
|
|
|
|
|
|
|
|
|
private var errorReturn: Result<T>? = null |
|
|
|
private var errorReturn: Result<T>? = null |
|
|
|
|
|
|
|
|
|
|
|
val isCancelled: Boolean |
|
|
|
val isCancelled: Boolean |
|
|
@ -37,9 +37,7 @@ class Coroutine<T>(scope: CoroutineScope, block: suspend CoroutineScope.() -> T) |
|
|
|
get() = job.isCompleted |
|
|
|
get() = job.isCompleted |
|
|
|
|
|
|
|
|
|
|
|
init { |
|
|
|
init { |
|
|
|
this.job = scope.plus(Dispatchers.Main).launch { |
|
|
|
this.job = executeInternal(scope, block) |
|
|
|
executeInternal(this@launch, block) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun timeout(timeMillis: () -> Long): Coroutine<T> { |
|
|
|
fun timeout(timeMillis: () -> Long): Coroutine<T> { |
|
|
@ -62,28 +60,35 @@ class Coroutine<T>(scope: CoroutineScope, block: suspend CoroutineScope.() -> T) |
|
|
|
return this@Coroutine |
|
|
|
return this@Coroutine |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun onStart(start: (suspend CoroutineScope.() -> Unit)): Coroutine<T> { |
|
|
|
fun onStart( |
|
|
|
this.start = start |
|
|
|
context: CoroutineContext? = null, |
|
|
|
return this@Coroutine |
|
|
|
block: (suspend CoroutineScope.() -> Unit) |
|
|
|
} |
|
|
|
): Coroutine<T> { |
|
|
|
|
|
|
|
this.start = VoidCallback(context, block) |
|
|
|
fun onExecute(execute: suspend CoroutineScope.(T?) -> Unit): Coroutine<T> { |
|
|
|
|
|
|
|
this.execute = execute |
|
|
|
|
|
|
|
return this@Coroutine |
|
|
|
return this@Coroutine |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun onSuccess(success: suspend CoroutineScope.(T?) -> Unit): Coroutine<T> { |
|
|
|
fun onSuccess( |
|
|
|
this.success = success |
|
|
|
context: CoroutineContext? = null, |
|
|
|
|
|
|
|
block: suspend CoroutineScope.(T?) -> Unit |
|
|
|
|
|
|
|
): Coroutine<T> { |
|
|
|
|
|
|
|
this.success = Callback(context, block) |
|
|
|
return this@Coroutine |
|
|
|
return this@Coroutine |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun onError(error: suspend CoroutineScope.(Throwable) -> Unit): Coroutine<T> { |
|
|
|
fun onError( |
|
|
|
this.error = error |
|
|
|
context: CoroutineContext? = null, |
|
|
|
|
|
|
|
block: suspend CoroutineScope.(Throwable) -> Unit |
|
|
|
|
|
|
|
): Coroutine<T> { |
|
|
|
|
|
|
|
this.error = Callback(context, block) |
|
|
|
return this@Coroutine |
|
|
|
return this@Coroutine |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun onFinally(finally: suspend CoroutineScope.() -> Unit): Coroutine<T> { |
|
|
|
fun onFinally( |
|
|
|
this.finally = finally |
|
|
|
context: CoroutineContext? = null, |
|
|
|
|
|
|
|
block: suspend CoroutineScope.() -> Unit |
|
|
|
|
|
|
|
): Coroutine<T> { |
|
|
|
|
|
|
|
this.finally = VoidCallback(context, block) |
|
|
|
return this@Coroutine |
|
|
|
return this@Coroutine |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -96,50 +101,72 @@ class Coroutine<T>(scope: CoroutineScope, block: suspend CoroutineScope.() -> T) |
|
|
|
return job.invokeOnCompletion(handler) |
|
|
|
return job.invokeOnCompletion(handler) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private suspend fun executeInternal(scope: CoroutineScope, block: suspend CoroutineScope.() -> T) { |
|
|
|
private fun executeInternal(scope: CoroutineScope, block: suspend CoroutineScope.() -> T): Job { |
|
|
|
tryCatch( |
|
|
|
return scope.plus(Dispatchers.Main).launch { |
|
|
|
{ |
|
|
|
try { |
|
|
|
start?.invoke(scope) |
|
|
|
start?.let { dispatchVoidCallback(this, it) } |
|
|
|
val result = executeBlockIO(block, timeMillis ?: 0L) |
|
|
|
val value = executeBlock(scope, timeMillis ?: 0L, block) |
|
|
|
success?.invoke(scope, result) |
|
|
|
success?.let { dispatchCallback(this, value, it) } |
|
|
|
}, |
|
|
|
} catch (e: Throwable) { |
|
|
|
{ e -> |
|
|
|
|
|
|
|
val consume: Boolean = errorReturn?.value?.let { value -> |
|
|
|
val consume: Boolean = errorReturn?.value?.let { value -> |
|
|
|
success?.invoke(scope, value) |
|
|
|
success?.let { dispatchCallback(this, value, it) } |
|
|
|
true |
|
|
|
true |
|
|
|
} ?: false |
|
|
|
} ?: false |
|
|
|
|
|
|
|
|
|
|
|
if (!consume) { |
|
|
|
if (!consume) { |
|
|
|
error?.invoke(scope, e) |
|
|
|
error?.let { dispatchCallback(this, e, it) } |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
} finally { |
|
|
|
{ |
|
|
|
finally?.let { dispatchVoidCallback(this, it) } |
|
|
|
finally?.invoke(scope) |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private suspend fun executeBlockIO(block: suspend CoroutineScope.() -> T, timeMillis: Long): T? { |
|
|
|
private suspend inline fun dispatchVoidCallback(scope: CoroutineScope, callback: VoidCallback) { |
|
|
|
val execution = withContext(Dispatchers.IO) { |
|
|
|
if (null == callback.context) { |
|
|
|
val result = block() |
|
|
|
callback.block.invoke(scope) |
|
|
|
execute?.invoke(this, result) |
|
|
|
} else { |
|
|
|
result |
|
|
|
withContext(scope.coroutineContext.plus(callback.context)) { |
|
|
|
|
|
|
|
callback.block.invoke(this) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return if (timeMillis > 0L) withTimeout(timeMillis) { execution } else execution |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private suspend fun tryCatch( |
|
|
|
private suspend inline fun <R> dispatchCallback( |
|
|
|
tryBlock: suspend () -> Unit, |
|
|
|
scope: CoroutineScope, |
|
|
|
errorBlock: (suspend (Throwable) -> Unit)? = null, |
|
|
|
value: R, |
|
|
|
finallyBlock: (suspend () -> Unit)? = null |
|
|
|
callback: Callback<R> |
|
|
|
) { |
|
|
|
) { |
|
|
|
try { |
|
|
|
if (null == callback.context) { |
|
|
|
tryBlock() |
|
|
|
callback.block.invoke(scope, value) |
|
|
|
} catch (e: Throwable) { |
|
|
|
} else { |
|
|
|
errorBlock?.invoke(e) |
|
|
|
withContext(scope.coroutineContext.plus(callback.context)) { |
|
|
|
} finally { |
|
|
|
callback.block.invoke(this, value) |
|
|
|
finallyBlock?.invoke() |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private suspend inline fun executeBlock( |
|
|
|
|
|
|
|
scope: CoroutineScope, |
|
|
|
|
|
|
|
timeMillis: Long, |
|
|
|
|
|
|
|
noinline block: suspend CoroutineScope.() -> T |
|
|
|
|
|
|
|
): T? { |
|
|
|
|
|
|
|
return withContext(scope.coroutineContext.plus(Dispatchers.IO)) { |
|
|
|
|
|
|
|
if (timeMillis > 0L) withTimeout(timeMillis) { |
|
|
|
|
|
|
|
block() |
|
|
|
|
|
|
|
} else block() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private data class Result<out T>(val value: T?) |
|
|
|
private data class Result<out T>(val value: T?) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private inner class VoidCallback( |
|
|
|
|
|
|
|
val context: CoroutineContext?, |
|
|
|
|
|
|
|
val block: suspend CoroutineScope.() -> Unit |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private inner class Callback<VALUE>( |
|
|
|
|
|
|
|
val context: CoroutineContext?, |
|
|
|
|
|
|
|
val block: suspend CoroutineScope.(VALUE) -> Unit |
|
|
|
|
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|