diff --git a/koin/src/commonMain/kotlin/FactoryWithRandomQualifier.kt b/koin/src/commonMain/kotlin/FactoryWithRandomQualifier.kt index e00675e1ff8..c5705d30c3f 100644 --- a/koin/src/commonMain/kotlin/FactoryWithRandomQualifier.kt +++ b/koin/src/commonMain/kotlin/FactoryWithRandomQualifier.kt @@ -4,8 +4,14 @@ import org.koin.core.definition.Definition import org.koin.core.module.Module /** - * Will be useful in case you need to declare some singles with one type several types, but need to separate them and do - * not care about how :) + * Declares a factory with a random qualifier in the Koin module. + * This is useful when you need to declare multiple factory definitions of the same type + * but want them to be uniquely identifiable without manually specifying qualifiers. + * Unlike singles, factories create a new instance each time they are requested. + * + * @param T The type of instance to be created by the factory + * @param definition The definition function that creates new instances + * @return A Koin definition for the factory with a random qualifier */ inline fun Module.factoryWithRandomQualifier( noinline definition: Definition diff --git a/koin/src/commonMain/kotlin/FactoryWithStringQualifier.kt b/koin/src/commonMain/kotlin/FactoryWithStringQualifier.kt index 0bf7301b107..f7d51d05ea2 100644 --- a/koin/src/commonMain/kotlin/FactoryWithStringQualifier.kt +++ b/koin/src/commonMain/kotlin/FactoryWithStringQualifier.kt @@ -4,6 +4,16 @@ import org.koin.core.definition.Definition import org.koin.core.module.Module import org.koin.core.qualifier.StringQualifier +/** + * Declares a factory with a string qualifier in the Koin module. + * This is a convenience function that wraps the string qualifier in a [StringQualifier]. + * Unlike singles, factories create a new instance each time they are requested. + * + * @param T The type of instance to be created by the factory + * @param qualifier The string value to be used as a qualifier + * @param definition The definition function that creates new instances + * @return A Koin definition for the factory with the specified string qualifier + */ inline fun Module.factory( qualifier: String, noinline definition: Definition diff --git a/koin/src/commonMain/kotlin/GetAllDistinct.kt b/koin/src/commonMain/kotlin/GetAllDistinct.kt index 59a610217cb..67743437368 100644 --- a/koin/src/commonMain/kotlin/GetAllDistinct.kt +++ b/koin/src/commonMain/kotlin/GetAllDistinct.kt @@ -3,6 +3,21 @@ package dev.inmo.micro_utils.koin import org.koin.core.Koin import org.koin.core.scope.Scope +/** + * Retrieves all instances of type [T] from the current [Scope] and returns them as a distinct list. + * This function is useful when you want to avoid duplicate instances of the same type. + * + * @param T The type of instances to retrieve + * @return A list of distinct instances of type [T] + */ inline fun Scope.getAllDistinct() = getAll().distinct() + +/** + * Retrieves all instances of type [T] from the [Koin] container and returns them as a distinct list. + * This function is useful when you want to avoid duplicate instances of the same type. + * + * @param T The type of instances to retrieve + * @return A list of distinct instances of type [T] + */ inline fun Koin.getAllDistinct() = getAll().distinct() diff --git a/koin/src/commonMain/kotlin/GetAny.kt b/koin/src/commonMain/kotlin/GetAny.kt index 351a1272fe0..ed26b32c7ce 100644 --- a/koin/src/commonMain/kotlin/GetAny.kt +++ b/koin/src/commonMain/kotlin/GetAny.kt @@ -3,6 +3,23 @@ package dev.inmo.micro_utils.koin import org.koin.core.Koin import org.koin.core.scope.Scope +/** + * Retrieves the first available instance of type [T] from the current scope. + * This is useful when you need any instance of a type and don't care which one. + * + * @param T The type of instance to retrieve + * @return The first available instance of type [T] + * @throws NoSuchElementException if no instances of type [T] are available + */ inline fun Scope.getAny() = getAll().first() + +/** + * Retrieves the first available instance of type [T] from the Koin container. + * This is useful when you need any instance of a type and don't care which one. + * + * @param T The type of instance to retrieve + * @return The first available instance of type [T] + * @throws NoSuchElementException if no instances of type [T] are available + */ inline fun Koin.getAny() = getAll().first() diff --git a/koin/src/commonMain/kotlin/GetWithDefinition.kt b/koin/src/commonMain/kotlin/GetWithDefinition.kt index 32cc88bad9e..3be5f9ac912 100644 --- a/koin/src/commonMain/kotlin/GetWithDefinition.kt +++ b/koin/src/commonMain/kotlin/GetWithDefinition.kt @@ -7,33 +7,81 @@ import org.koin.core.instance.InstanceFactory import org.koin.core.parameter.ParametersDefinition import org.koin.core.scope.Scope +/** + * Retrieves an instance of type [T] from the Koin container using a [BeanDefinition]. + * + * @param T The type of instance to retrieve + * @param definition The bean definition to use for instance retrieval + * @param parameters Optional parameters to pass to the instance constructor + * @return An instance of type [T] + */ fun Koin.get(definition: BeanDefinition, parameters: ParametersDefinition? = null): T = get( definition.primaryType, definition.qualifier, parameters ) +/** + * Retrieves an instance of type [T] from the Koin container using an [InstanceFactory]. + * + * @param T The type of instance to retrieve + * @param definition The instance factory to use for instance retrieval + * @param parameters Optional parameters to pass to the instance constructor + * @return An instance of type [T] + */ fun Koin.get(definition: InstanceFactory, parameters: ParametersDefinition? = null): T = get( definition.beanDefinition, parameters ) +/** + * Retrieves an instance of type [T] from the Koin container using a [KoinDefinition]. + * + * @param T The type of instance to retrieve + * @param definition The Koin definition to use for instance retrieval + * @param parameters Optional parameters to pass to the instance constructor + * @return An instance of type [T] + */ fun Koin.get(definition: KoinDefinition, parameters: ParametersDefinition? = null): T = get( definition.factory, parameters ) +/** + * Retrieves an instance of type [T] from the current scope using a [BeanDefinition]. + * + * @param T The type of instance to retrieve + * @param definition The bean definition to use for instance retrieval + * @param parameters Optional parameters to pass to the instance constructor + * @return An instance of type [T] + */ fun Scope.get(definition: BeanDefinition, parameters: ParametersDefinition? = null): T = get( definition.primaryType, definition.qualifier, parameters ) +/** + * Retrieves an instance of type [T] from the current scope using an [InstanceFactory]. + * + * @param T The type of instance to retrieve + * @param definition The instance factory to use for instance retrieval + * @param parameters Optional parameters to pass to the instance constructor + * @return An instance of type [T] + */ fun Scope.get(definition: InstanceFactory, parameters: ParametersDefinition? = null): T = get( definition.beanDefinition, parameters ) +/** + * Retrieves an instance of type [T] from the current scope using a [KoinDefinition]. + * + * @param T The type of instance to retrieve + * @param definition The Koin definition to use for instance retrieval + * @param parameters Optional parameters to pass to the instance constructor + * @return An instance of type [T] + */ fun Scope.get(definition: KoinDefinition, parameters: ParametersDefinition? = null): T = get( definition.factory, parameters diff --git a/koin/src/commonMain/kotlin/RandomQualifier.kt b/koin/src/commonMain/kotlin/RandomQualifier.kt index 9b5b1a39c62..64d124bd5b0 100644 --- a/koin/src/commonMain/kotlin/RandomQualifier.kt +++ b/koin/src/commonMain/kotlin/RandomQualifier.kt @@ -3,4 +3,10 @@ package dev.inmo.micro_utils.koin import com.benasher44.uuid.uuid4 import org.koin.core.qualifier.StringQualifier +/** + * Creates a [StringQualifier] with a random string value. + * + * @param randomFun A function that generates a random string. By default, it uses UUID4 string representation. + * @return A [StringQualifier] with a randomly generated string value + */ fun RandomQualifier(randomFun: () -> String = { uuid4().toString() }) = StringQualifier(randomFun()) diff --git a/koin/src/commonMain/kotlin/SingleWithRandomQualifier.kt b/koin/src/commonMain/kotlin/SingleWithRandomQualifier.kt index ef7068076e2..255ffaf9785 100644 --- a/koin/src/commonMain/kotlin/SingleWithRandomQualifier.kt +++ b/koin/src/commonMain/kotlin/SingleWithRandomQualifier.kt @@ -4,8 +4,14 @@ import org.koin.core.definition.Definition import org.koin.core.module.Module /** - * Will be useful in case you need to declare some singles with one type several types, but need to separate them and do - * not care about how :) + * Declares a single instance with a random qualifier in the Koin module. + * This is useful when you need to declare multiple instances of the same type + * but want them to be uniquely identifiable without manually specifying qualifiers. + * + * @param T The type of instance to be created + * @param createdAtStart Whether the instance should be created when the Koin module starts (default: false) + * @param definition The definition function that creates the instance + * @return A Koin definition for the single instance with a random qualifier */ inline fun Module.singleWithRandomQualifier( createdAtStart: Boolean = false, diff --git a/koin/src/commonMain/kotlin/SingleWithStringQualifier.kt b/koin/src/commonMain/kotlin/SingleWithStringQualifier.kt index cdde37efcca..5c16569130d 100644 --- a/koin/src/commonMain/kotlin/SingleWithStringQualifier.kt +++ b/koin/src/commonMain/kotlin/SingleWithStringQualifier.kt @@ -4,6 +4,16 @@ import org.koin.core.definition.Definition import org.koin.core.module.Module import org.koin.core.qualifier.StringQualifier +/** + * Declares a single instance with a string qualifier in the Koin module. + * This is a convenience function that wraps the string qualifier in a [StringQualifier]. + * + * @param T The type of instance to be created + * @param qualifier The string value to be used as a qualifier + * @param createdAtStart Whether the instance should be created when the Koin module starts (default: false) + * @param definition The definition function that creates the instance + * @return A Koin definition for the single instance with the specified string qualifier + */ inline fun Module.single( qualifier: String, createdAtStart: Boolean = false,