add alsoWithUnlockingOnSuccess and alsoWithUnlockingOnSuccessAsync

This commit is contained in:
2025-03-15 20:43:17 +06:00
parent 78494b6036
commit cfaa2a8927
3 changed files with 28 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
package dev.inmo.micro_utils.coroutines
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
suspend inline fun alsoWithUnlockingOnSuccess(
vararg lockers: SmartRWLocker,
block: suspend () -> Unit
): Result<Unit> {
return runCatching {
block()
}.onSuccess {
lockers.forEach { it.unlockWrite() }
}
}
fun alsoWithUnlockingOnSuccessAsync(
scope: CoroutineScope,
vararg lockers: SmartRWLocker,
block: suspend () -> Unit
): Job = scope.launchLoggingDropExceptions {
alsoWithUnlockingOnSuccess(*lockers, block = block)
}