Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…line API for native
- Loading branch information
1 parent
095d859
commit 6154824
Showing
2 changed files
with
74 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
core/koin-core/src/nativeMain/kotlin/org/koin/core/module/ModuleExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
@file:OptIn(KoinInternalApi::class, KoinInternalApi::class) | ||
|
||
package org.koin.core.module | ||
|
||
import org.koin.core.annotation.KoinInternalApi | ||
import org.koin.core.definition.BeanDefinition | ||
import org.koin.core.definition.Definition | ||
import org.koin.core.definition.Kind | ||
import org.koin.core.instance.FactoryInstanceFactory | ||
import org.koin.core.instance.SingleInstanceFactory | ||
import org.koin.core.qualifier.Qualifier | ||
import org.koin.core.registry.ScopeRegistry | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* Module extension for Native to allow use of Module API without inlined KClass Type | ||
* | ||
* @author Arnaud Giuliani | ||
*/ | ||
|
||
internal fun <T : Any> Module.createDefinition( | ||
kind: Kind = Kind.Singleton, | ||
kClass: KClass<T>, | ||
qualifier: Qualifier? = null, | ||
definition: Definition<T>, | ||
secondaryTypes: List<KClass<*>> = emptyList(), | ||
scopeQualifier: Qualifier = ScopeRegistry.rootScopeQualifier | ||
): BeanDefinition<T> { | ||
return BeanDefinition( | ||
scopeQualifier, | ||
kClass, | ||
qualifier, | ||
definition, | ||
kind, | ||
secondaryTypes = secondaryTypes | ||
) | ||
} | ||
|
||
/** | ||
* Create a factory | ||
*/ | ||
fun <T : Any> Module.factory( | ||
kClass: KClass<T>, | ||
qualifier: Qualifier? = null, | ||
definition: Definition<T>, | ||
scopeQualifier: Qualifier = ScopeRegistry.rootScopeQualifier | ||
): KoinDefinition<T> { | ||
val def = createDefinition(Kind.Factory,kClass,qualifier,definition, scopeQualifier = scopeQualifier) | ||
val factory = FactoryInstanceFactory(def) | ||
indexPrimaryType(factory) | ||
return Pair(this, factory) | ||
} | ||
|
||
/** | ||
* Create a Single | ||
*/ | ||
fun <T : Any> Module.single( | ||
kClass: KClass<T>, | ||
qualifier: Qualifier? = null, | ||
definition: Definition<T>, | ||
createdAtStart: Boolean = false, | ||
scopeQualifier: Qualifier = ScopeRegistry.rootScopeQualifier | ||
): KoinDefinition<T> { | ||
val def = createDefinition(Kind.Singleton,kClass,qualifier,definition, scopeQualifier = scopeQualifier) | ||
val factory = SingleInstanceFactory(def) | ||
indexPrimaryType(factory) | ||
if (createdAtStart){ | ||
prepareForCreationAtStart(factory) | ||
} | ||
return Pair(this, factory) | ||
} |