Skip to content

Commit

Permalink
release version 1.3.7
Browse files Browse the repository at this point in the history
with kotlin 1.5.31
coroutines-android 1.5.2
fix gradle.properties
add java 11 support
launchOnUI, launchOnUiAsyncBy, launchOnUiBy return a Job
  • Loading branch information
Rasalexman committed Oct 15, 2021
1 parent 8d9b9af commit f346a96
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 82 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ android {

dependencies {
implementation(fileTree(mapOf("include" to listOf("*.jar"), "dir" to "libs")))
implementation(kotlin("stdlib", Versions.kotlin))
implementation(kotlin("stdlib-jdk8", Versions.kotlin))

implementation(Libs.Core.appcompat)
implementation(Libs.Core.coreKtx)
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/appdependencies/Builds.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object Builds {
}

object Manager {
const val VERSION_NAME = "1.3.6"
const val VERSION_NAME = "1.3.7"
}
}

2 changes: 0 additions & 2 deletions buildSrc/src/main/kotlin/appdependencies/ClassPath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ object ClassPath {

const val gradle = "com.android.tools.build:gradle:${Versions.gradle}"
const val kotlingradle = "org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlin}"
//const val mavenplugin = "com.github.dcendents:android-maven-gradle-plugin:${Versions.maven}"
//const val dokkaplugin = "org.jetbrains.dokka:dokka-gradle-plugin:${Versions.dokka}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ open class AsyncTasksManager : IAsyncTasksManager {
/**
* Cancelation handlers local store
*/
override val cancelationHandlers: MutableSet<CancelationHandler> by lazy { mutableSetOf<CancelationHandler>() }
override val cancelationHandlers: MutableSet<CancelationHandler> by lazy { mutableSetOf() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

@file:Suppress("SuspendFunctionOnCoroutineScope")

package com.rasalexman.coroutinesmanager

import kotlinx.coroutines.*
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext

/**
Expand Down Expand Up @@ -48,20 +53,18 @@ typealias SuspendFinal<T> = suspend CoroutineScope.(Throwable?) -> T
* @param catchBlock - block where throwable will be handled.
* @param handleCancellationExceptionManually - does we need to handle cancelation manually
*/
suspend fun <T> tryCatch(
suspend fun <T> CoroutineScope.tryCatch(
tryBlock: SuspendTry<T>,
catchBlock: SuspendCatch<T>,
handleCancellationExceptionManually: Boolean = false
): T {
return coroutineScope {
try {
tryBlock()
} catch (e: Throwable) {
if (e !is CancellationException || handleCancellationExceptionManually) {
catchBlock(e)
} else {
throw e
}
return try {
tryBlock()
} catch (e: Throwable) {
if (e !is CancellationException || handleCancellationExceptionManually) {
catchBlock(e)
} else {
throw e
}
}
}
Expand All @@ -74,26 +77,24 @@ suspend fun <T> tryCatch(
* @param finallyBlock
* @param handleCancellationExceptionManually - can we handle cancellation manually
*/
suspend fun <T> tryCatchFinally(
suspend fun <T> CoroutineScope.tryCatchFinally(
tryBlock: SuspendTry<T>,
catchBlock: SuspendCatch<T>,
finallyBlock: SuspendFinal<T>,
handleCancellationExceptionManually: Boolean = false
): T {
return coroutineScope {
var caughtThrowable: Throwable? = null
try {
tryBlock()
} catch (e: Throwable) {
caughtThrowable = e
if (e !is CancellationException || handleCancellationExceptionManually) {
catchBlock(e)
} else {
throw e
}
} finally {
finallyBlock(caughtThrowable)
var caughtThrowable: Throwable? = null
return try {
tryBlock()
} catch (e: Throwable) {
caughtThrowable = e
if (e !is CancellationException || handleCancellationExceptionManually) {
catchBlock(e)
} else {
throw e
}
} finally {
finallyBlock(caughtThrowable)
}
}

Expand All @@ -103,21 +104,20 @@ suspend fun <T> tryCatchFinally(
* @param tryBlock
* @param finallyBlock
*/
suspend fun <T> tryFinally(
suspend fun <T> CoroutineScope.tryFinally(
tryBlock: SuspendTry<T>,
finallyBlock: SuspendFinal<T>
): T {
return coroutineScope {
var caughtThrowable: Throwable? = null
try {
tryBlock()
} catch (e: Throwable) {
caughtThrowable = e
throw e
} finally {
finallyBlock(caughtThrowable)
}
var caughtThrowable: Throwable? = null
return try {
tryBlock()
} catch (e: Throwable) {
caughtThrowable = e
throw e
} finally {
finallyBlock(caughtThrowable)
}

}

/**
Expand All @@ -129,24 +129,22 @@ suspend fun <T> tryFinally(
* @param catchContext - context for invoke [catchBlock]. Current Scope Context by default
* @param handleCancellationExceptionManually - does we need to handle cancelation manually
*/
suspend fun <T> tryCatchWithContext(
suspend fun <T> CoroutineScope.tryCatchWithContext(
tryBlock: SuspendTry<T>,
catchBlock: SuspendCatch<T>,
tryContext: CoroutineContext = Dispatchers.Main,
catchContext: CoroutineContext = Dispatchers.Main,
handleCancellationExceptionManually: Boolean = false
): T {
return coroutineScope {
try {
withContext(tryContext, tryBlock)
} catch (e: Throwable) {
if (e !is CancellationException || handleCancellationExceptionManually) {
withContext(catchContext) {
catchBlock(e)
}
} else {
throw e
return try {
withContext(tryContext, tryBlock)
} catch (e: Throwable) {
if (e !is CancellationException || handleCancellationExceptionManually) {
withContext(catchContext) {
catchBlock(e)
}
} else {
throw e
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

@file:Suppress("SuspendFunctionOnCoroutineScope")

package com.rasalexman.coroutinesmanager

import kotlinx.coroutines.*
Expand All @@ -24,7 +26,7 @@ import kotlin.coroutines.CoroutineContext
/**
* IAsyncTasksManager
*/
interface IAsyncTasksManager {
interface IAsyncTasksManager : CoroutineScope {
/**
* Async Job
* You better to override this val in implementing classes,
Expand All @@ -41,17 +43,17 @@ interface IAsyncTasksManager {
val cancelationHandlers: MutableSet<CancelationHandler>?
get() = CoroutinesProvider.cancelationHandlersSet

/**
* CoroutineContext to use in this manager. It's IO
*/
val asyncCoroutineContext: CoroutineContext
get() = CoroutinesProvider.IO + asyncJob

/**
* CoroutineContext to use in this manager. It's Default
*/
val defaultCoroutineContext: CoroutineContext
get() = CoroutinesProvider.COMMON + asyncJob

/**
* CoroutineContext to use in this manager. It's IO
*/
override val coroutineContext: CoroutineContext
get() = CoroutinesProvider.IO + asyncJob
}

/**
Expand All @@ -64,27 +66,23 @@ suspend fun <T> IAsyncTasksManager.doAsync(
start: CoroutineStart = CoroutineStart.DEFAULT,
block: SuspendTry<T>
): Deferred<T> {
return coroutineScope {
async(
asyncCoroutineContext,
return async(
coroutineContext,
start,
block
).also { job -> job.invokeOnCompletion { job.cancel() } }
}
}

@Suppress("DeferredIsResult")
suspend fun <T> IAsyncTasksManager.doDefault(
start: CoroutineStart = CoroutineStart.DEFAULT,
block: SuspendTry<T>
): Deferred<T> {
return coroutineScope {
async(
return async(
defaultCoroutineContext,
start,
block
).also { job -> job.invokeOnCompletion { job.cancel() } }
}
}

/**
Expand Down Expand Up @@ -145,15 +143,15 @@ suspend fun <T> IAsyncTasksManager.doAsyncAwaitBy(
}

/**
* Do some async work withContext([com.rasalexman.coroutinesmanager.IAsyncTasksManager.asyncCoroutineContext])
* Do some async work withContext([com.rasalexman.coroutinesmanager.IAsyncTasksManager.coroutineContext])
*
* @param block
* The worker block to invoke
*/
suspend fun <T> IAsyncTasksManager.doWithAsync(
block: SuspendTry<T>
): T {
return withContext(asyncCoroutineContext, block)
return withContext(coroutineContext, block)
}

/**
Expand Down Expand Up @@ -204,7 +202,7 @@ suspend fun <T> IAsyncTasksManager.doTryCatchAsync(

/**
* Doing some async work with tryCatch block without create new coroutineScope
* and using withContext([com.rasalexman.coroutinesmanager.IAsyncTasksManager.asyncCoroutineContext])
* and using withContext([com.rasalexman.coroutinesmanager.IAsyncTasksManager.coroutineContext])
*
* @param tryBlock - main working block
* @param catchBlock - block where throwable will be handled.
Expand All @@ -215,7 +213,7 @@ suspend fun <T> IAsyncTasksManager.doWithTryCatchAsync(
tryBlock: SuspendTry<T>,
catchBlock: SuspendCatch<T>,
handleCancellationExceptionManually: Boolean = false
): T = withContext(asyncCoroutineContext) {
): T = withContext(coroutineContext) {
tryCatch(
tryBlock = tryBlock,
catchBlock = catchBlock,
Expand Down Expand Up @@ -250,7 +248,7 @@ suspend fun <T> IAsyncTasksManager.doTryCatchFinallyAsync(

/**
* Doing some async work with tryCatchFinally block without create new coroutineScope
* and using withContext([com.rasalexman.coroutinesmanager.IAsyncTasksManager.asyncCoroutineContext])
* and using withContext([com.rasalexman.coroutinesmanager.IAsyncTasksManager.coroutineContext])
*
* @param tryBlock - main working block
* @param catchBlock - block where throwable will be handled
Expand All @@ -263,7 +261,7 @@ suspend fun <T> IAsyncTasksManager.doWithTryCatchFinallyAsync(
catchBlock: SuspendCatch<T>,
finallyBlock: SuspendFinal<T>,
handleCancellationExceptionManually: Boolean = false
): T = withContext(asyncCoroutineContext) {
): T = withContext(coroutineContext) {
tryCatchFinally(
tryBlock = tryBlock,
catchBlock = catchBlock,
Expand Down Expand Up @@ -304,7 +302,7 @@ suspend fun <T> IAsyncTasksManager.doTryFinallyAsync(
suspend fun <T> IAsyncTasksManager.doWithTryFinallyAsync(
tryBlock: SuspendTry<T>,
finallyBlock: SuspendFinal<T>
): T = withContext(asyncCoroutineContext) {
): T = withContext(coroutineContext) {
tryFinally(
tryBlock = tryBlock,
finallyBlock = finallyBlock
Expand Down Expand Up @@ -383,7 +381,7 @@ suspend fun <T> IAsyncTasksManager.doTryFinallyAsyncAwait(
*/
@Synchronized
fun IAsyncTasksManager.cancelAllAsync() {
asyncCoroutineContext.cancelChildren()
coroutineContext.cancelChildren()
cancelationHandlers?.forEach { it() }
}

Expand All @@ -392,7 +390,7 @@ fun IAsyncTasksManager.cancelAllAsync() {
*/
@Synchronized
fun IAsyncTasksManager.cleanup() {
asyncCoroutineContext.cancelChildren()
coroutineContext.cancelChildren()
cancelationHandlers?.clear()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import kotlin.coroutines.CoroutineContext
/**
* ICoroutinesManager
*/
interface ICoroutinesManager : CoroutineScope, IAsyncTasksManager {
interface ICoroutinesManager : IAsyncTasksManager {
/**
* Working job
*
Expand Down Expand Up @@ -59,8 +59,8 @@ interface ICoroutinesManager : CoroutineScope, IAsyncTasksManager {
fun ICoroutinesManager.launchOnUI(
start: CoroutineStart = CoroutineStart.DEFAULT,
block: SuspendTry<Unit>
) {
launch(start = start, block = block).also { job -> job.invokeOnCompletion { job.cancel() } }
): Job {
return launch(start = start, block = block).also { job -> job.invokeOnCompletion { job.cancel() } }
}

/**
Expand All @@ -79,12 +79,13 @@ fun ICoroutinesManager.launchOnUiBy(
finallyBlock: SuspendFinal<Unit>? = null,
handleCancellationExceptionManually: Boolean = false,
start: CoroutineStart = CoroutineStart.DEFAULT
) {
when {
): Job {
return when {
catchBlock == null && finallyBlock == null -> launchOnUI(start, tryBlock)
finallyBlock == null && catchBlock != null -> launchOnUITryCatch(tryBlock, catchBlock, handleCancellationExceptionManually, start)
finallyBlock != null && catchBlock != null -> launchOnUITryCatchFinally(tryBlock, catchBlock, finallyBlock, handleCancellationExceptionManually, start)
finallyBlock != null && catchBlock == null -> launchOnUITryFinally(tryBlock, finallyBlock, start)
else -> launchOnUI(start, tryBlock)
}
}

Expand All @@ -103,12 +104,13 @@ fun ICoroutinesManager.launchOnUiAsyncBy(
finallyBlock: SuspendFinal<Unit>? = null,
start: CoroutineStart = CoroutineStart.DEFAULT,
startAsync: CoroutineStart = CoroutineStart.DEFAULT
) {
when {
): Job {
return when {
catchBlock == null && finallyBlock == null -> launchOnUIAsyncAwait(start, startAsync, tryBlock)
finallyBlock == null && catchBlock != null -> launchOnUITryCatchAsyncAwait(start, startAsync, tryBlock, catchBlock)
finallyBlock != null && catchBlock != null -> launchOnUITryCatchFinallyAsyncAwait(start, startAsync, tryBlock, catchBlock, finallyBlock)
finallyBlock != null && catchBlock == null -> launchOnUITryFinallyAsyncAwait(start, startAsync, tryBlock, finallyBlock)
else -> launchOnUIAsyncAwait(start, startAsync, tryBlock)
}
}

Expand Down

0 comments on commit f346a96

Please sign in to comment.