pull/32/head
Administrator 5 years ago
parent a9059b9ce2
commit f98b1f9730
  1. 47
      app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt

@ -17,9 +17,18 @@ class Coroutine<T>() {
} }
} }
private var coroutine: Coroutine<T>? = null private var interceptor: Coroutine<T>? = null
private var job: Job? = null private var job: Job? = null
private var start: (() -> Unit)? = null
private var success: ((T?) -> Unit)? = null
private var error: ((Throwable) -> Unit)? = null
private var finally: (() -> Unit)? = null
private var timeMillis: Long? = null
private var errorReturn: Result<T>? = null
private constructor( private constructor(
scope: CoroutineScope? = null, scope: CoroutineScope? = null,
block: (suspend CoroutineScope.() -> T)? = null block: (suspend CoroutineScope.() -> T)? = null
@ -30,23 +39,13 @@ class Coroutine<T>() {
} }
private constructor(coroutine: Coroutine<T>) : this() { private constructor(coroutine: Coroutine<T>) : this() {
this.coroutine = coroutine this.interceptor = coroutine
this.job = coroutine.job this.job = coroutine.job
} }
private var start: (() -> Unit)? = null
private var success: ((T?) -> Unit)? = null
private var error: ((Throwable) -> Unit)? = null
private var finally: (() -> Unit)? = null
private var timeMillis: Long? = null
private var errorReturn: Result<T>? = null
fun timeout(timeMillis: () -> Long): Coroutine<T> { fun timeout(timeMillis: () -> Long): Coroutine<T> {
if (this.coroutine != null) { if (this.interceptor != null) {
this.coroutine!!.timeMillis = timeMillis() this.interceptor!!.timeMillis = timeMillis()
} else { } else {
this.timeMillis = timeMillis() this.timeMillis = timeMillis()
} }
@ -54,8 +53,8 @@ class Coroutine<T>() {
} }
fun onErrorReturn(value: () -> T?): Coroutine<T> { fun onErrorReturn(value: () -> T?): Coroutine<T> {
if (this.coroutine != null) { if (this.interceptor != null) {
this.coroutine!!.errorReturn = Result(value()) this.interceptor!!.errorReturn = Result(value())
} else { } else {
errorReturn = Result(value()) errorReturn = Result(value())
} }
@ -63,8 +62,8 @@ class Coroutine<T>() {
} }
fun onStart(start: (() -> Unit)): Coroutine<T> { fun onStart(start: (() -> Unit)): Coroutine<T> {
if (this.coroutine != null) { if (this.interceptor != null) {
this.coroutine!!.start = start this.interceptor!!.start = start
} else { } else {
this.start = start this.start = start
} }
@ -72,8 +71,8 @@ class Coroutine<T>() {
} }
fun onSuccess(success: (T?) -> Unit): Coroutine<T> { fun onSuccess(success: (T?) -> Unit): Coroutine<T> {
if (this.coroutine != null) { if (this.interceptor != null) {
this.coroutine!!.success = success this.interceptor!!.success = success
} else { } else {
this.success = success this.success = success
} }
@ -81,8 +80,8 @@ class Coroutine<T>() {
} }
fun onError(error: (Throwable) -> Unit): Coroutine<T> { fun onError(error: (Throwable) -> Unit): Coroutine<T> {
if (this.coroutine != null) { if (this.interceptor != null) {
this.coroutine!!.error = error this.interceptor!!.error = error
} else { } else {
this.error = error this.error = error
} }
@ -90,8 +89,8 @@ class Coroutine<T>() {
} }
fun onFinally(finally: () -> Unit): Coroutine<T> { fun onFinally(finally: () -> Unit): Coroutine<T> {
if (this.coroutine != null) { if (this.interceptor != null) {
this.coroutine!!.finally = finally this.interceptor!!.finally = finally
} else { } else {
this.finally = finally this.finally = finally
} }

Loading…
Cancel
Save