From 178518db5ece59f19e687fb66ee8b2b4416f8ce8 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 14 Mar 2025 15:01:22 +0600 Subject: [PATCH] alsoInvalidateSync, alsoInvalidateSyncLogging, alsoDoInvalidate -> alsoInvalidateAsync --- CHANGELOG.md | 2 +- .../coroutines/FlowSubscriptionAsync.kt | 2 +- .../inmo/micro_utils/repos/cache/CacheRepo.kt | 2 +- .../jvmMain/kotlin/InvalidateSynchronously.kt | 54 +++++++++++++++++++ .../src/jvmTest/kotlin/KtorCRUDRepoTests.kt | 4 +- .../jvmTest/kotlin/KtorKeyValueRepoTests.kt | 4 +- 6 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 repos/cache/src/jvmMain/kotlin/InvalidateSynchronously.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index a46b4923021..aea531e3d55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ * `Exposed`: `0.59.0` -> `0.60.0` * `Repo`: * `Cache`: - * Add extensions `alsoInvalidate` and `alsoDoInvalidate` + * Add extensions `alsoInvalidate`, `alsoInvalidateAsync`, `alsoInvalidateSync` and `alsoInvalidateSyncLogging` * `Koin`: * Add extensions `singleSuspend` and `factorySuspend` for defining of dependencies with suspendable blocks diff --git a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/FlowSubscriptionAsync.kt b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/FlowSubscriptionAsync.kt index 667bba39c3f..3f46576e8dc 100644 --- a/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/FlowSubscriptionAsync.kt +++ b/coroutines/src/commonMain/kotlin/dev/inmo/micro_utils/coroutines/FlowSubscriptionAsync.kt @@ -71,7 +71,7 @@ fun Flow.subscribeAsync( it.invoke(markersMap) } - val job = subscribeSafelyWithoutExceptions(subscope) { data -> + val job = subscribeLoggingDropExceptions(subscope) { data -> val dataCommand = AsyncSubscriptionCommandData(data, subscope, markerFactory, block) { marker -> actor.send( AsyncSubscriptionCommandClearReceiver(marker) diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CacheRepo.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CacheRepo.kt index 8286091002b..bf709c4657a 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CacheRepo.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/CacheRepo.kt @@ -15,7 +15,7 @@ suspend fun T.alsoInvalidate() = also { invalidate() } -fun T.alsoDoInvalidate(scope: CoroutineScope) = also { +fun T.alsoInvalidateAsync(scope: CoroutineScope) = also { scope.launchLoggingDropExceptions { invalidate() } diff --git a/repos/cache/src/jvmMain/kotlin/InvalidateSynchronously.kt b/repos/cache/src/jvmMain/kotlin/InvalidateSynchronously.kt new file mode 100644 index 00000000000..73fc94b7c5c --- /dev/null +++ b/repos/cache/src/jvmMain/kotlin/InvalidateSynchronously.kt @@ -0,0 +1,54 @@ +package dev.inmo.micro_utils.repos.cache + +import dev.inmo.kslog.common.KSLog +import dev.inmo.micro_utils.coroutines.doSynchronously +import dev.inmo.micro_utils.coroutines.runCatchingLogging +import kotlinx.coroutines.CoroutineScope + +fun T.alsoInvalidateSync( + scope: CoroutineScope, + onFailure: suspend (Throwable) -> Unit = {}, +) = also { + scope.doSynchronously { + runCatching { + invalidate() + }.onFailure { + onFailure(it) + } + } +} + +fun T.alsoInvalidateSync( + onFailure: suspend (Throwable) -> Unit = {}, +) = also { + doSynchronously { + runCatching { + invalidate() + }.onFailure { + onFailure(it) + } + } +} + +fun T.alsoInvalidateSyncLogging( + scope: CoroutineScope, + errorMessageBuilder: CoroutineScope.(Throwable) -> Any = { "Something web wrong" }, + logger: KSLog = KSLog, +) = also { + scope.doSynchronously { + runCatchingLogging(errorMessageBuilder, logger) { + invalidate() + } + } +} + +fun T.alsoInvalidateSyncLogging( + errorMessageBuilder: CoroutineScope.(Throwable) -> Any = { "Something web wrong" }, + logger: KSLog = KSLog, +) = also { + doSynchronously { + runCatchingLogging(errorMessageBuilder, logger) { + invalidate() + } + } +} diff --git a/repos/ktor/common/src/jvmTest/kotlin/KtorCRUDRepoTests.kt b/repos/ktor/common/src/jvmTest/kotlin/KtorCRUDRepoTests.kt index 0426e0e6aca..e94b0bcf8b0 100644 --- a/repos/ktor/common/src/jvmTest/kotlin/KtorCRUDRepoTests.kt +++ b/repos/ktor/common/src/jvmTest/kotlin/KtorCRUDRepoTests.kt @@ -73,7 +73,7 @@ class KtorCRUDRepoTests : CommonCRUDRepoTests() { } val server = io.ktor.server.engine.embeddedServer( CIO, - 34567, + 34568, "127.0.0.1" ) { install(ContentNegotiation) { @@ -100,7 +100,7 @@ class KtorCRUDRepoTests : CommonCRUDRepoTests() { } } val crudClient = KtorCRUDRepoClient( - "http://127.0.0.1:34567", + "http://127.0.0.1:34568", client, ContentType.Application.Json ) { diff --git a/repos/ktor/common/src/jvmTest/kotlin/KtorKeyValueRepoTests.kt b/repos/ktor/common/src/jvmTest/kotlin/KtorKeyValueRepoTests.kt index 781c15c764d..a2b336b7065 100644 --- a/repos/ktor/common/src/jvmTest/kotlin/KtorKeyValueRepoTests.kt +++ b/repos/ktor/common/src/jvmTest/kotlin/KtorKeyValueRepoTests.kt @@ -63,7 +63,7 @@ class KtorKeyValueRepoTests : CommonKeyValueRepoTests() { val repo = MapKeyValueRepo(map) val server = io.ktor.server.engine.embeddedServer( CIO, - 34567, + 34569, "127.0.0.1" ) { install(ContentNegotiation) { @@ -91,7 +91,7 @@ class KtorKeyValueRepoTests : CommonKeyValueRepoTests() { } } val crudClient = KtorKeyValueRepoClient( - "http://127.0.0.1:34567", + "http://127.0.0.1:34569", client, ContentType.Application.Json, Int.serializer(),