|
|
@ -35,7 +35,7 @@ class Coroutine<T>() { |
|
|
|
get() = job?.isCancelled ?: false |
|
|
|
get() = job?.isCancelled ?: false |
|
|
|
|
|
|
|
|
|
|
|
val isActive: Boolean |
|
|
|
val isActive: Boolean |
|
|
|
get() = job?.isActive ?: false |
|
|
|
get() = job?.isActive ?: true |
|
|
|
|
|
|
|
|
|
|
|
val isCompleted: Boolean |
|
|
|
val isCompleted: Boolean |
|
|
|
get() = job?.isCompleted ?: false |
|
|
|
get() = job?.isCompleted ?: false |
|
|
@ -44,8 +44,8 @@ class Coroutine<T>() { |
|
|
|
scope: CoroutineScope, |
|
|
|
scope: CoroutineScope, |
|
|
|
block: suspend CoroutineScope.() -> T |
|
|
|
block: suspend CoroutineScope.() -> T |
|
|
|
) : this() { |
|
|
|
) : this() { |
|
|
|
this.job = scope.launch { |
|
|
|
this.job = scope.plus(Dispatchers.Main).launch { |
|
|
|
executeInternal(block) |
|
|
|
executeInternal(this@launch, block) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -122,52 +122,47 @@ class Coroutine<T>() { |
|
|
|
job?.cancel(cause) |
|
|
|
job?.cancel(cause) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private suspend fun executeInternal(block: suspend CoroutineScope.() -> T) { |
|
|
|
private suspend fun executeInternal(scope: CoroutineScope, block: suspend CoroutineScope.() -> T) { |
|
|
|
tryCatch( |
|
|
|
tryCatch( |
|
|
|
{ |
|
|
|
{ |
|
|
|
start?.invoke(this) |
|
|
|
start?.invoke(scope) |
|
|
|
|
|
|
|
|
|
|
|
val result = executeBlockIO(block, timeMillis ?: 0L) |
|
|
|
val result = executeBlockIO(block, timeMillis ?: 0L) |
|
|
|
|
|
|
|
success?.invoke(scope, result) |
|
|
|
success?.invoke(this, result) |
|
|
|
|
|
|
|
}, |
|
|
|
}, |
|
|
|
{ e -> |
|
|
|
{ e -> |
|
|
|
val consume: Boolean = errorReturn?.value?.let { value -> |
|
|
|
val consume: Boolean = errorReturn?.value?.let { value -> |
|
|
|
success?.invoke(this, value) |
|
|
|
success?.invoke(scope, value) |
|
|
|
true |
|
|
|
true |
|
|
|
} ?: false |
|
|
|
} ?: false |
|
|
|
|
|
|
|
|
|
|
|
if (!consume) { |
|
|
|
if (!consume) { |
|
|
|
error?.invoke(this, e) |
|
|
|
error?.invoke(scope, e) |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
}, |
|
|
|
{ |
|
|
|
{ |
|
|
|
finally?.invoke(this) |
|
|
|
finally?.invoke(scope) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private suspend fun executeBlockIO(block: suspend CoroutineScope.() -> T, timeMillis: Long): T? { |
|
|
|
private suspend fun executeBlockIO(block: suspend CoroutineScope.() -> T, timeMillis: Long): T? { |
|
|
|
val result: T = withContext(Dispatchers.IO) { |
|
|
|
val execution = withContext(Dispatchers.IO) { |
|
|
|
val b = block() |
|
|
|
val result = block() |
|
|
|
|
|
|
|
execute?.invoke(this, result) |
|
|
|
execute?.invoke(this, b) |
|
|
|
result |
|
|
|
|
|
|
|
|
|
|
|
b |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return if (timeMillis > 0L) withTimeout(timeMillis) { result } else result |
|
|
|
return if (timeMillis > 0L) withTimeout(timeMillis) { execution } else execution |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private suspend fun tryCatch( |
|
|
|
private suspend fun tryCatch( |
|
|
|
tryBlock: suspend CoroutineScope.() -> Unit, |
|
|
|
tryBlock: suspend () -> Unit, |
|
|
|
errorBlock: (suspend CoroutineScope.(Throwable) -> Unit)? = null, |
|
|
|
errorBlock: (suspend (Throwable) -> Unit)? = null, |
|
|
|
finallyBlock: (suspend CoroutineScope.() -> Unit)? = null |
|
|
|
finallyBlock: (suspend () -> Unit)? = null |
|
|
|
) { |
|
|
|
) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
coroutineScope { tryBlock() } |
|
|
|
tryBlock() |
|
|
|
} catch (e: Throwable) { |
|
|
|
} catch (e: Throwable) { |
|
|
|
coroutineScope { errorBlock?.invoke(this, e) } |
|
|
|
errorBlock?.invoke(e) |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
coroutineScope { finallyBlock?.invoke(this) } |
|
|
|
finallyBlock?.invoke() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|