Skip to content

Commit

Permalink
allow to run koinApplication and specify if eager instances are creat…
Browse files Browse the repository at this point in the history
…ed or not
  • Loading branch information
arnaudgiuliani committed Aug 30, 2023
1 parent ea90be4 commit bbd18de
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
4 changes: 2 additions & 2 deletions core/koin-core/api/koin-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,8 @@ public final class org/koin/dsl/DefinitionBindingKt {
}

public final class org/koin/dsl/KoinApplicationKt {
public static final fun koinApplication (Lkotlin/jvm/functions/Function1;)Lorg/koin/core/KoinApplication;
public static synthetic fun koinApplication$default (Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/koin/core/KoinApplication;
public static final fun koinApplication (ZLkotlin/jvm/functions/Function1;)Lorg/koin/core/KoinApplication;
public static synthetic fun koinApplication$default (ZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lorg/koin/core/KoinApplication;
}

public final class org/koin/dsl/ModuleDSLKt {
Expand Down
2 changes: 1 addition & 1 deletion core/koin-core/src/commonMain/kotlin/org/koin/core/Koin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ class Koin {
* Create Single instances Definitions marked as createdAtStart
*/
fun createEagerInstances() {
logger.debug("Eager instances ...")
logger.debug("Create eager instances ...")
val duration = measureDuration {
instanceRegistry.createAllEagerInstances()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ typealias KoinAppDeclaration = KoinApplication.() -> Unit
/**
* Create a KoinApplication instance and help configure it
* @author Arnaud Giuliani
*
* @param createEagerInstances - allow to create eager instances or not
* @param appDeclaration
*/
@KoinApplicationDslMarker
fun koinApplication(appDeclaration: KoinAppDeclaration? = null): KoinApplication {
fun koinApplication(createEagerInstances : Boolean = true, appDeclaration: KoinAppDeclaration? = null): KoinApplication {
val koinApplication = KoinApplication.init()
appDeclaration?.invoke(koinApplication)
koinApplication.createEagerInstances()
if (createEagerInstances) {
koinApplication.createEagerInstances()
}
return koinApplication
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,34 @@ class DeclareInstanceTest {

session2.get<Simple.ComponentA>()
}

@Test
fun `avoid to start eager instances`() {
var count = 0
koinApplication {
modules(
module {
single(createdAtStart = true){
count++
Simple.ComponentA()
}
},
)
}

assertEquals(1,count)
count = 0

koinApplication(createEagerInstances = false) {
modules(
module {
single(createdAtStart = true){
count++
Simple.ComponentA()
}
},
)
}
assertEquals(0,count)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ internal class MutableGlobalContext : KoinContext {
override fun startKoin(koinApplication: KoinApplication): KoinApplication = lock.withLock {
register(koinApplication)
koinApplication.createEagerInstances()
return koinApplication
koinApplication
}

override fun startKoin(appDeclaration: KoinAppDeclaration): KoinApplication = lock.withLock {
val koinApplication = KoinApplication.init()
register(koinApplication)
appDeclaration(koinApplication)
koinApplication.createEagerInstances()
return koinApplication
koinApplication
}

override fun loadKoinModules(module: Module) = lock.withLock {
Expand Down

0 comments on commit bbd18de

Please sign in to comment.