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