diff --git a/CHANGELOG.md b/CHANGELOG.md index 3794730e54f..dd6045f7252 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 0.4.28 + +* `Versions`: + * `Kotlin`: `1.4.30` -> `1.4.31` + * `Ktor`: `1.5.1` -> `1.5.2` +* `Coroutines` + * Add `createActionsActor`/`createSafeActionsActor` and `doWithSuspending` + ## 0.4.27 * `Repos` diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/ActionsActor.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/ActionsActor.kt new file mode 100644 index 00000000000..9a22f9f1cfe --- /dev/null +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/ActionsActor.kt @@ -0,0 +1,46 @@ +package dev.inmo.micro_utils.coroutines + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.channels.Channel +import kotlin.coroutines.* + +interface ActorAction { + suspend operator fun invoke(): T +} + +/** + * Planned to use with [doWithSuspending]. Will execute incoming lambdas sequentially + * + * @see actor + */ +fun CoroutineScope.createActionsActor() = actor Unit> { + it() +} + +/** + * Planned to use with [doWithSuspending]. Will execute incoming lambdas sequentially + * + * @see safeActor + */ +inline fun CoroutineScope.createSafeActionsActor( + noinline onException: ExceptionHandler = defaultSafelyExceptionHandler +) = safeActor Unit>(Channel.UNLIMITED, onException) { + it() +} + +/** + * Must be use with actor created by [createActionsActor] or [createSafeActionsActor]. Will send lambda which will + * execute [action] and return result. + * + * @see suspendCoroutine + * @see safely + */ +suspend fun Channel Unit>.doWithSuspending( + action: ActorAction +) = suspendCoroutine { + offer { + safely({ e -> it.resumeWithException(e) }) { + it.resume(action()) + } + } +} diff --git a/gradle.properties b/gradle.properties index 9f8ad7e19c9..5bac5c44c53 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,12 +6,12 @@ kotlin.incremental.js=true android.useAndroidX=true android.enableJetifier=true -kotlin_version=1.4.30 +kotlin_version=1.4.31 kotlin_coroutines_version=1.4.2 kotlin_serialisation_core_version=1.1.0 kotlin_exposed_version=0.29.1 -ktor_version=1.5.1 +ktor_version=1.5.2 klockVersion=2.0.6 @@ -44,5 +44,5 @@ dokka_version=1.4.20 # Project data group=dev.inmo -version=0.4.27 -android_code_version=31 +version=0.4.28 +android_code_version=32 diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/DatabaseOperations.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/DatabaseOperations.kt index b1cfdb28804..35b04fa0c4b 100644 --- a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/DatabaseOperations.kt +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/DatabaseOperations.kt @@ -26,20 +26,39 @@ fun SQLiteDatabase.createTable( } } -fun Cursor.getString(columnName: String) = getString( - getColumnIndex(columnName) +/** + * @throws IllegalArgumentException + */ +fun Cursor.getString(columnName: String): String = getString( + getColumnIndexOrThrow(columnName) ) -fun Cursor.getLong(columnName: String) = getLong( - getColumnIndex(columnName) +/** + * @throws IllegalArgumentException + */ +fun Cursor.getShort(columnName: String): Short = getShort( + getColumnIndexOrThrow(columnName) ) -fun Cursor.getInt(columnName: String) = getInt( - getColumnIndex(columnName) +/** + * @throws IllegalArgumentException + */ +fun Cursor.getLong(columnName: String): Long = getLong( + getColumnIndexOrThrow(columnName) ) -fun Cursor.getDouble(columnName: String) = getDouble( - getColumnIndex(columnName) +/** + * @throws IllegalArgumentException + */ +fun Cursor.getInt(columnName: String): Int = getInt( + getColumnIndexOrThrow(columnName) +) + +/** + * @throws IllegalArgumentException + */ +fun Cursor.getDouble(columnName: String): Double = getDouble( + getColumnIndexOrThrow(columnName) ) fun SQLiteDatabase.select(