mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-09 10:15:33 +00:00
alsoInvalidate, alsoDoInvalidate, singleSuspend, factorySuspend
This commit is contained in:
@@ -4,6 +4,11 @@
|
|||||||
|
|
||||||
* `Versions`:
|
* `Versions`:
|
||||||
* `Exposed`: `0.59.0` -> `0.60.0`
|
* `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
|
## 0.25.1
|
||||||
|
|
||||||
|
@@ -7,7 +7,9 @@ fun <T> CoroutineScope.launchSynchronously(block: suspend CoroutineScope.() -> T
|
|||||||
val objectToSynchronize = Object()
|
val objectToSynchronize = Object()
|
||||||
synchronized(objectToSynchronize) {
|
synchronized(objectToSynchronize) {
|
||||||
launch(start = CoroutineStart.UNDISPATCHED) {
|
launch(start = CoroutineStart.UNDISPATCHED) {
|
||||||
result = safelyWithResult(block)
|
result = runCatching {
|
||||||
|
block()
|
||||||
|
}
|
||||||
}.invokeOnCompletion {
|
}.invokeOnCompletion {
|
||||||
synchronized(objectToSynchronize) {
|
synchronized(objectToSynchronize) {
|
||||||
objectToSynchronize.notifyAll()
|
objectToSynchronize.notifyAll()
|
||||||
|
@@ -16,7 +16,7 @@ kotlin {
|
|||||||
}
|
}
|
||||||
jvmMain {
|
jvmMain {
|
||||||
dependencies {
|
dependencies {
|
||||||
api libs.kt.reflect
|
api project(":micro_utils.coroutines")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
androidMain {
|
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
|
package dev.inmo.micro_utils.repos.cache
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.coroutines.launchLoggingDropExceptions
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
|
||||||
interface InvalidatableRepo {
|
interface InvalidatableRepo {
|
||||||
/**
|
/**
|
||||||
* Invalidates its internal data. It __may__ lead to autoreload of data. In case when repo makes autoreload,
|
* 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 invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun <T : InvalidatableRepo> T.alsoInvalidate() = also {
|
||||||
|
invalidate()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T : InvalidatableRepo> T.alsoDoInvalidate(scope: CoroutineScope) = also {
|
||||||
|
scope.launchLoggingDropExceptions {
|
||||||
|
invalidate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
typealias CacheRepo = InvalidatableRepo
|
typealias CacheRepo = InvalidatableRepo
|
||||||
|
Reference in New Issue
Block a user