mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-07 00:59:26 +00:00
add alsoWithUnlockingOnSuccess and alsoWithUnlockingOnSuccessAsync
This commit is contained in:
@@ -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)
|
||||
}
|
Reference in New Issue
Block a user