diff --git a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/ActionWrapper.kt b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/ActionWrapper.kt index abc2a78718e..11a96e71978 100644 --- a/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/ActionWrapper.kt +++ b/repos/cache/src/commonMain/kotlin/dev/inmo/micro_utils/repos/cache/fallback/ActionWrapper.kt @@ -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 wrap(block: suspend () -> T): Result + /** + * 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 wrap(block: suspend () -> T): Result = 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 wrap(block: suspend () -> T): Result = runCatchingSafely { block() } } }