diff --git a/CHANGELOG.md b/CHANGELOG.md index fcc848da961..fc0af043fba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 0.18.4 +* `Koin`: + * New extension `lazyInject` + ## 0.18.3 * `Versions`: diff --git a/koin/src/jvmMain/kotlin/LazyInject.kt b/koin/src/jvmMain/kotlin/LazyInject.kt new file mode 100644 index 00000000000..7564b646ad2 --- /dev/null +++ b/koin/src/jvmMain/kotlin/LazyInject.kt @@ -0,0 +1,27 @@ +package dev.inmo.micro_utils.koin + +import org.koin.core.parameter.ParametersDefinition +import org.koin.core.qualifier.Qualifier +import org.koin.java.KoinJavaComponent +import kotlin.reflect.KClass + +fun lazyInject( + kClassFactory: () -> KClass<*>, + qualifier: Qualifier? = null, + parameters: ParametersDefinition? = null +): Lazy { + return lazy(LazyThreadSafetyMode.SYNCHRONIZED) { + KoinJavaComponent.get(kClassFactory().java, qualifier, parameters) + } +} + +fun lazyInject( + kClass: KClass<*>, + qualifier: Qualifier? = null, + parameters: ParametersDefinition? = null +): Lazy = lazyInject({ kClass }, qualifier, parameters) + +inline fun lazyInject( + qualifier: Qualifier? = null, + noinline parameters: ParametersDefinition? = null +): Lazy = lazyInject(T::class, qualifier, parameters)