add RandomQualifier

This commit is contained in:
InsanusMokrassar 2022-09-14 22:21:30 +06:00
parent 1e1f7df86d
commit 2696e663cf
5 changed files with 10 additions and 14 deletions

View File

@ -1,10 +1,7 @@
package dev.inmo.micro_utils.koin
import com.benasher44.uuid.uuid4
import org.koin.core.definition.Definition
import org.koin.core.instance.InstanceFactory
import org.koin.core.module.Module
import org.koin.core.qualifier.StringQualifier
/**
* Will be useful in case you need to declare some singles with one type several types, but need to separate them and do
@ -12,4 +9,4 @@ import org.koin.core.qualifier.StringQualifier
*/
inline fun <reified T : Any> Module.factoryWithRandomQualifier(
noinline definition: Definition<T>
) = factory(uuid4().toString(), definition)
) = factory(RandomQualifier(), definition)

View File

@ -0,0 +1,6 @@
package dev.inmo.micro_utils.koin
import com.benasher44.uuid.uuid4
import org.koin.core.qualifier.StringQualifier
fun RandomQualifier(randomFun: () -> String = { uuid4().toString() }) = StringQualifier(randomFun())

View File

@ -1,10 +1,7 @@
package dev.inmo.micro_utils.koin
import com.benasher44.uuid.uuid4
import org.koin.core.definition.Definition
import org.koin.core.instance.InstanceFactory
import org.koin.core.module.Module
import org.koin.core.qualifier.StringQualifier
/**
* Will be useful in case you need to declare some singles with one type several types, but need to separate them and do
@ -13,4 +10,4 @@ import org.koin.core.qualifier.StringQualifier
inline fun <reified T : Any> Module.singleWithRandomQualifier(
createdAtStart: Boolean = false,
noinline definition: Definition<T>
) = single(uuid4().toString(), createdAtStart, definition)
) = single(RandomQualifier(), createdAtStart, definition)

View File

@ -1,16 +1,13 @@
package dev.inmo.micro_utils.koin
import com.benasher44.uuid.uuid4
import org.koin.core.definition.Definition
import org.koin.core.instance.InstanceFactory
import org.koin.core.module.Module
import org.koin.dsl.binds
import kotlin.reflect.KClass
import kotlin.reflect.full.allSuperclasses
inline fun <reified T : Any> Module.factoryWithRandomQualifierAndBinds(
bindFilter: (KClass<*>) -> Boolean = { true },
noinline definition: Definition<T>
): Pair<Module, InstanceFactory<*>> {
return factoryWithBinds(uuid4().toString(), bindFilter, definition)
return factoryWithBinds(RandomQualifier(), bindFilter, definition)
}

View File

@ -1,6 +1,5 @@
package dev.inmo.micro_utils.koin
import com.benasher44.uuid.uuid4
import org.koin.core.definition.Definition
import org.koin.core.instance.InstanceFactory
import org.koin.core.module.Module
@ -11,5 +10,5 @@ inline fun <reified T : Any> Module.singleWithRandomQualifierAndBinds(
bindFilter: (KClass<*>) -> Boolean = { true },
noinline definition: Definition<T>
): Pair<Module, InstanceFactory<*>> {
return singleWithBinds(uuid4().toString(), createdAtStart, bindFilter, definition)
return singleWithBinds(RandomQualifier(), createdAtStart, bindFilter, definition)
}