mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-11-26 12:08:45 +00:00
Update ActionWrapper.kt
This commit is contained in:
parent
79f2041565
commit
2645ea29d6
@ -3,9 +3,23 @@ package dev.inmo.micro_utils.repos.cache.fallback
|
||||
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
||||
import kotlinx.coroutines.withTimeout
|
||||
|
||||
/**
|
||||
* Realizations should [wrap] the work with some conditions like retries on exceptions, calling timeout, etc.
|
||||
*
|
||||
* @see Timeouted
|
||||
* @see Direct
|
||||
*/
|
||||
interface ActionWrapper {
|
||||
/**
|
||||
* Should execute [block] to take the result [T], but may return failure in case when something went wrong.
|
||||
* This method should never throw any [Exception]
|
||||
*/
|
||||
suspend fun <T> wrap(block: suspend () -> T): Result<T>
|
||||
|
||||
/**
|
||||
* This type of [ActionWrapper]s will use [withTimeout]([timeoutMillis]) and if original call
|
||||
* will not return anything in that timeout just return [Result] with failure
|
||||
*/
|
||||
class Timeouted(private val timeoutMillis: Long) : ActionWrapper {
|
||||
override suspend fun <T> wrap(block: suspend () -> T): Result<T> = runCatchingSafely {
|
||||
withTimeout(timeoutMillis) {
|
||||
@ -13,7 +27,11 @@ interface ActionWrapper {
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* It is passthrough variant of [ActionWrapper] which will just call incoming block with wrapping into [runCatchingSafely]
|
||||
*/
|
||||
object Direct : ActionWrapper {
|
||||
|
||||
override suspend fun <T> wrap(block: suspend () -> T): Result<T> = runCatchingSafely { block() }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user