mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-07 00:59:26 +00:00
alsoInvalidate, alsoDoInvalidate, singleSuspend, factorySuspend
This commit is contained in:
@@ -4,6 +4,11 @@
|
||||
|
||||
* `Versions`:
|
||||
* `Exposed`: `0.59.0` -> `0.60.0`
|
||||
* `Repo`:
|
||||
* `Cache`:
|
||||
* Add extensions `alsoInvalidate` and `alsoDoInvalidate`
|
||||
* `Koin`:
|
||||
* Add extensions `singleSuspend` and `factorySuspend` for defining of dependencies with suspendable blocks
|
||||
|
||||
## 0.25.1
|
||||
|
||||
|
@@ -7,7 +7,9 @@ fun <T> CoroutineScope.launchSynchronously(block: suspend CoroutineScope.() -> T
|
||||
val objectToSynchronize = Object()
|
||||
synchronized(objectToSynchronize) {
|
||||
launch(start = CoroutineStart.UNDISPATCHED) {
|
||||
result = safelyWithResult(block)
|
||||
result = runCatching {
|
||||
block()
|
||||
}
|
||||
}.invokeOnCompletion {
|
||||
synchronized(objectToSynchronize) {
|
||||
objectToSynchronize.notifyAll()
|
||||
|
@@ -16,7 +16,7 @@ kotlin {
|
||||
}
|
||||
jvmMain {
|
||||
dependencies {
|
||||
api libs.kt.reflect
|
||||
api project(":micro_utils.coroutines")
|
||||
}
|
||||
}
|
||||
androidMain {
|
||||
|
32
koin/src/jvmMain/kotlin/FactorySuspend.kt
Normal file
32
koin/src/jvmMain/kotlin/FactorySuspend.kt
Normal file
@@ -0,0 +1,32 @@
|
||||
package dev.inmo.micro_utils.koin
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.doSynchronously
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import org.koin.core.module.Module
|
||||
import org.koin.core.parameter.ParametersHolder
|
||||
import org.koin.core.qualifier.Qualifier
|
||||
import org.koin.core.qualifier.StringQualifier
|
||||
import org.koin.core.scope.Scope
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
inline fun <reified T : Any> Module.factorySuspend(
|
||||
qualifier: Qualifier? = null,
|
||||
coroutineScope: CoroutineScope? = null,
|
||||
noinline definition: suspend Scope.(ParametersHolder) -> T
|
||||
) = factory(
|
||||
qualifier,
|
||||
if (coroutineScope == null) {
|
||||
{
|
||||
doSynchronously {
|
||||
definition(it)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
{
|
||||
coroutineScope.doSynchronously {
|
||||
definition(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
32
koin/src/jvmMain/kotlin/SingleSuspend.kt
Normal file
32
koin/src/jvmMain/kotlin/SingleSuspend.kt
Normal file
@@ -0,0 +1,32 @@
|
||||
package dev.inmo.micro_utils.koin
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.doSynchronously
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import org.koin.core.module.Module
|
||||
import org.koin.core.parameter.ParametersHolder
|
||||
import org.koin.core.qualifier.StringQualifier
|
||||
import org.koin.core.scope.Scope
|
||||
|
||||
inline fun <reified T : Any> Module.singleSuspend(
|
||||
qualifier: StringQualifier,
|
||||
createdAtStart: Boolean = false,
|
||||
coroutineScope: CoroutineScope? = null,
|
||||
noinline definition: suspend Scope.(ParametersHolder) -> T
|
||||
) = single(
|
||||
qualifier,
|
||||
createdAtStart,
|
||||
if (coroutineScope == null) {
|
||||
{
|
||||
doSynchronously {
|
||||
definition(it)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
{
|
||||
coroutineScope.doSynchronously {
|
||||
definition(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
@@ -1,5 +1,8 @@
|
||||
package dev.inmo.micro_utils.repos.cache
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.launchLoggingDropExceptions
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
||||
interface InvalidatableRepo {
|
||||
/**
|
||||
* Invalidates its internal data. It __may__ lead to autoreload of data. In case when repo makes autoreload,
|
||||
@@ -8,4 +11,14 @@ interface InvalidatableRepo {
|
||||
suspend fun invalidate()
|
||||
}
|
||||
|
||||
suspend fun <T : InvalidatableRepo> T.alsoInvalidate() = also {
|
||||
invalidate()
|
||||
}
|
||||
|
||||
fun <T : InvalidatableRepo> T.alsoDoInvalidate(scope: CoroutineScope) = also {
|
||||
scope.launchLoggingDropExceptions {
|
||||
invalidate()
|
||||
}
|
||||
}
|
||||
|
||||
typealias CacheRepo = InvalidatableRepo
|
||||
|
Reference in New Issue
Block a user