|
|
@ -2,35 +2,38 @@ package io.legado.app.help.coroutine |
|
|
|
|
|
|
|
|
|
|
|
import kotlinx.coroutines.* |
|
|
|
import kotlinx.coroutines.* |
|
|
|
|
|
|
|
|
|
|
|
class Coroutine<T>(private val scope: CoroutineScope, private val block: suspend CoroutineScope.() -> T) { |
|
|
|
class Coroutine<T>() { |
|
|
|
|
|
|
|
|
|
|
|
companion object { |
|
|
|
companion object { |
|
|
|
|
|
|
|
|
|
|
|
private val DEFAULT = MainScope() |
|
|
|
private val DEFAULT = MainScope() |
|
|
|
|
|
|
|
|
|
|
|
fun <T> launch(scope: CoroutineScope = DEFAULT, block: suspend CoroutineScope.() -> T): Coroutine<T> { |
|
|
|
fun <T> async(scope: CoroutineScope = DEFAULT, block: suspend CoroutineScope.() -> T): Coroutine<T> { |
|
|
|
return Coroutine(scope, block) |
|
|
|
return Coroutine(scope, block) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun <T> plus(coroutine: Coroutine<T>): Coroutine<T>{ |
|
|
|
fun <T> plus(coroutine: Coroutine<T>): Coroutine<T> { |
|
|
|
return Coroutine(coroutine.scope, coroutine.block) |
|
|
|
return Coroutine(coroutine) |
|
|
|
.timeout{ coroutine.timeMillis?:0 } |
|
|
|
|
|
|
|
.onErrorReturn { coroutine.errorReturn?.value } |
|
|
|
|
|
|
|
.onStart { coroutine.start } |
|
|
|
|
|
|
|
.onSuccess { coroutine.success } |
|
|
|
|
|
|
|
.onError { coroutine.error } |
|
|
|
|
|
|
|
.onFinally { coroutine.finally } |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private val job: Job |
|
|
|
private var coroutine: Coroutine<T>? = null |
|
|
|
|
|
|
|
private var job: Job? = null |
|
|
|
|
|
|
|
|
|
|
|
init { |
|
|
|
private constructor( |
|
|
|
job = scope.launch { |
|
|
|
scope: CoroutineScope? = null, |
|
|
|
executeInternal(block) |
|
|
|
block: (suspend CoroutineScope.() -> T)? = null |
|
|
|
|
|
|
|
) : this() { |
|
|
|
|
|
|
|
this.job = scope?.launch { |
|
|
|
|
|
|
|
block?.let { executeInternal(it) } |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private constructor(coroutine: Coroutine<T>) : this() { |
|
|
|
|
|
|
|
this.coroutine = coroutine |
|
|
|
|
|
|
|
this.job = coroutine.job |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private var start: (() -> Unit)? = null |
|
|
|
private var start: (() -> Unit)? = null |
|
|
|
private var success: ((T?) -> Unit)? = null |
|
|
|
private var success: ((T?) -> Unit)? = null |
|
|
|
private var error: ((Throwable) -> Unit)? = null |
|
|
|
private var error: ((Throwable) -> Unit)? = null |
|
|
@ -42,38 +45,62 @@ class Coroutine<T>(private val scope: CoroutineScope, private val block: suspend |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun timeout(timeMillis: () -> Long): Coroutine<T> { |
|
|
|
fun timeout(timeMillis: () -> Long): Coroutine<T> { |
|
|
|
|
|
|
|
if (this.coroutine != null) { |
|
|
|
|
|
|
|
this.coroutine!!.timeMillis = timeMillis() |
|
|
|
|
|
|
|
} else { |
|
|
|
this.timeMillis = timeMillis() |
|
|
|
this.timeMillis = timeMillis() |
|
|
|
|
|
|
|
} |
|
|
|
return this@Coroutine |
|
|
|
return this@Coroutine |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun onErrorReturn(value: () -> T?): Coroutine<T> { |
|
|
|
fun onErrorReturn(value: () -> T?): Coroutine<T> { |
|
|
|
|
|
|
|
if (this.coroutine != null) { |
|
|
|
|
|
|
|
this.coroutine!!.errorReturn = Result(value()) |
|
|
|
|
|
|
|
} else { |
|
|
|
errorReturn = Result(value()) |
|
|
|
errorReturn = Result(value()) |
|
|
|
|
|
|
|
} |
|
|
|
return this@Coroutine |
|
|
|
return this@Coroutine |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun onStart(start: (() -> Unit)): Coroutine<T> { |
|
|
|
fun onStart(start: (() -> Unit)): Coroutine<T> { |
|
|
|
|
|
|
|
if (this.coroutine != null) { |
|
|
|
|
|
|
|
this.coroutine!!.start = start |
|
|
|
|
|
|
|
} else { |
|
|
|
this.start = start |
|
|
|
this.start = start |
|
|
|
|
|
|
|
} |
|
|
|
return this@Coroutine |
|
|
|
return this@Coroutine |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun onSuccess(success: (T?) -> Unit): Coroutine<T> { |
|
|
|
fun onSuccess(success: (T?) -> Unit): Coroutine<T> { |
|
|
|
|
|
|
|
if (this.coroutine != null) { |
|
|
|
|
|
|
|
this.coroutine!!.success = success |
|
|
|
|
|
|
|
} else { |
|
|
|
this.success = success |
|
|
|
this.success = success |
|
|
|
|
|
|
|
} |
|
|
|
return this@Coroutine |
|
|
|
return this@Coroutine |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun onError(error: (Throwable) -> Unit): Coroutine<T> { |
|
|
|
fun onError(error: (Throwable) -> Unit): Coroutine<T> { |
|
|
|
|
|
|
|
if (this.coroutine != null) { |
|
|
|
|
|
|
|
this.coroutine!!.error = error |
|
|
|
|
|
|
|
} else { |
|
|
|
this.error = error |
|
|
|
this.error = error |
|
|
|
|
|
|
|
} |
|
|
|
return this@Coroutine |
|
|
|
return this@Coroutine |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun onFinally(finally: () -> Unit): Coroutine<T> { |
|
|
|
fun onFinally(finally: () -> Unit): Coroutine<T> { |
|
|
|
|
|
|
|
if (this.coroutine != null) { |
|
|
|
|
|
|
|
this.coroutine!!.finally = finally |
|
|
|
|
|
|
|
} else { |
|
|
|
this.finally = finally |
|
|
|
this.finally = finally |
|
|
|
|
|
|
|
} |
|
|
|
return this@Coroutine |
|
|
|
return this@Coroutine |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//取消当前任务 |
|
|
|
//取消当前任务 |
|
|
|
fun cancel(cause: CancellationException? = null) { |
|
|
|
fun cancel(cause: CancellationException? = null) { |
|
|
|
job.cancel(cause) |
|
|
|
job?.cancel(cause) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private suspend fun executeInternal(block: suspend CoroutineScope.() -> T) { |
|
|
|
private suspend fun executeInternal(block: suspend CoroutineScope.() -> T) { |
|
|
|