Merge pull request #66 from InsanusMokrassar/0.5.3

0.5.3
This commit is contained in:
InsanusMokrassar 2021-05-26 18:06:39 +06:00 committed by GitHub
commit 2bfad9f885
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 124 additions and 38 deletions

View File

@ -1,5 +1,18 @@
# Changelog # Changelog
## 0.5.3
* `Versions`:
* `Kotlin`: `1.5.0` -> `1.5.10`
* `Coroutines`:
* Extensions `doInUI` and `doInDefault` were replaced in common and available on any supported platform
* Extension `doInIO` replaced into `jvm` and available on any `JVM` platform
* Old extension `safelyWithouException` without `onException` has been replaced by its copy with `onException` and
default value
* New value `defaultSafelyWithoutExceptionHandlerWithNull` which is used in all `*WithoutExceptions` by default
* Analogs of `launch` and `async` for `safely` and `safelyWithoutExceptions` were added
* Analogs of `runCatching` for `safely` and `safelyWithoutExceptions` were added
## 0.5.2 ## 0.5.2
* `Ktor`: * `Ktor`:

View File

@ -0,0 +1,23 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext
inline val UI
get() = Dispatchers.Main
inline val Default
get() = Dispatchers.Default
suspend inline fun <T> doIn(context: CoroutineContext, noinline block: suspend CoroutineScope.() -> T) = withContext(
context,
block
)
suspend inline fun <T> doInUI(noinline block: suspend CoroutineScope.() -> T) = doIn(
UI,
block
)
suspend inline fun <T> doInDefault(noinline block: suspend CoroutineScope.() -> T) = doIn(
Default,
block
)

View File

@ -86,6 +86,9 @@ suspend fun <T> safelyWithContextExceptionHandler(
* * [CoroutineContext.get] with [SafelyExceptionHandlerKey] as key * * [CoroutineContext.get] with [SafelyExceptionHandlerKey] as key
* * [defaultSafelyExceptionHandler] * * [defaultSafelyExceptionHandler]
* *
* Remember, that [ExceptionHandler] from [CoroutineContext.get] will be used anyway if it is available. After it will
* be called [onException]
*
* @param [onException] Will be called when happen exception inside of [block]. By default will throw exception - this * @param [onException] Will be called when happen exception inside of [block]. By default will throw exception - this
* exception will be available for catching * exception will be available for catching
* *
@ -105,24 +108,38 @@ suspend inline fun <T> safely(
} }
} }
suspend inline fun <T> runCatchingSafely(
noinline onException: ExceptionHandler<T> = defaultSafelyExceptionHandler,
noinline block: suspend CoroutineScope.() -> T
): Result<T> = runCatching {
safely(onException, block)
}
/**
* Use this handler in cases you wish to include handling of exceptions by [defaultSafelyWithoutExceptionHandler] and
* returning null at one time
*
* @see safelyWithoutExceptions
* @see launchSafelyWithoutExceptions
* @see asyncSafelyWithoutExceptions
*/
val defaultSafelyWithoutExceptionHandlerWithNull: ExceptionHandler<Nothing?> = {
defaultSafelyWithoutExceptionHandler.invoke(it)
null
}
/** /**
* Shortcut for [safely] with exception handler, that as expected must return null in case of impossible creating of * Shortcut for [safely] with exception handler, that as expected must return null in case of impossible creating of
* result from exception (instead of throwing it) * result from exception (instead of throwing it, by default always returns null)
*/ */
suspend inline fun <T> safelyWithoutExceptions( suspend inline fun <T> safelyWithoutExceptions(
noinline onException: ExceptionHandler<T?>, noinline onException: ExceptionHandler<T?> = defaultSafelyWithoutExceptionHandlerWithNull,
noinline block: suspend CoroutineScope.() -> T noinline block: suspend CoroutineScope.() -> T
): T? = safely(onException, block) ): T? = safely(onException, block)
/** suspend inline fun <T> runCatchingSafelyWithoutExceptions(
* Shortcut for [safely] without exception handler (instead of this you will always receive null as a result) noinline onException: ExceptionHandler<T?> = defaultSafelyWithoutExceptionHandlerWithNull,
*/
suspend inline fun <T> safelyWithoutExceptions(
noinline block: suspend CoroutineScope.() -> T noinline block: suspend CoroutineScope.() -> T
): T? = safelyWithoutExceptions( ): Result<T?> = runCatching {
{ safelyWithoutExceptions(onException, block)
defaultSafelyWithoutExceptionHandler.invoke(it) }
null
},
block
)

View File

@ -0,0 +1,41 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.*
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
inline fun CoroutineScope.launchSafely(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
noinline onException: ExceptionHandler<Unit> = defaultSafelyExceptionHandler,
noinline block: suspend CoroutineScope.() -> Unit
) = launch(context, start) {
safely(onException, block)
}
inline fun CoroutineScope.launchSafelyWithoutExceptions(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
noinline onException: ExceptionHandler<Unit?> = defaultSafelyWithoutExceptionHandlerWithNull,
noinline block: suspend CoroutineScope.() -> Unit
) = launch(context, start) {
safelyWithoutExceptions(onException, block)
}
inline fun <T> CoroutineScope.asyncSafely(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
noinline onException: ExceptionHandler<T> = defaultSafelyExceptionHandler,
noinline block: suspend CoroutineScope.() -> T
) = async(context, start) {
safely(onException, block)
}
inline fun <T> CoroutineScope.asyncSafelyWithoutExceptions(
context: CoroutineContext = EmptyCoroutineContext,
start: CoroutineStart = CoroutineStart.DEFAULT,
noinline onException: ExceptionHandler<T?> = defaultSafelyWithoutExceptionHandlerWithNull,
noinline block: suspend CoroutineScope.() -> T
) = async(context, start) {
safelyWithoutExceptions(onException, block)
}

View File

@ -0,0 +1,11 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.*
val IO
get() = Dispatchers.IO
suspend inline fun <T> doInIO(noinline block: suspend CoroutineScope.() -> T) = doIn(
IO,
block
)

View File

@ -1,9 +1,8 @@
package dev.inmo.micro_utils.coroutines package dev.inmo.micro_utils.coroutines
import dev.inmo.micro_utils.coroutines.asDeferred
import dev.inmo.micro_utils.coroutines.launchSynchronously
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlin.test.* import kotlin.test.Test
import kotlin.test.assertEquals
class DoWithFirstTests { class DoWithFirstTests {
@Test @Test

View File

@ -1,18 +0,0 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
suspend inline fun <T> doInUI(noinline block: suspend CoroutineScope.() -> T) = withContext(
Dispatchers.Main,
block
)
suspend inline fun <T> doInDefault(noinline block: suspend CoroutineScope.() -> T) = withContext(
Dispatchers.Default,
block
)
suspend inline fun <T> doInIO(noinline block: suspend CoroutineScope.() -> T) = withContext(
Dispatchers.IO,
block
)

View File

@ -7,7 +7,7 @@ android.useAndroidX=true
android.enableJetifier=true android.enableJetifier=true
org.gradle.jvmargs=-Xmx2g org.gradle.jvmargs=-Xmx2g
kotlin_version=1.5.0 kotlin_version=1.5.10
kotlin_coroutines_version=1.5.0 kotlin_coroutines_version=1.5.0
kotlin_serialisation_core_version=1.2.1 kotlin_serialisation_core_version=1.2.1
kotlin_exposed_version=0.31.1 kotlin_exposed_version=0.31.1
@ -45,5 +45,5 @@ dokka_version=1.4.32
# Project data # Project data
group=dev.inmo group=dev.inmo
version=0.5.2 version=0.5.3
android_code_version=43 android_code_version=44