diff --git a/examples/androidx-samples/src/main/java/org/koin/sample/sandbox/di/AppModule.kt b/examples/androidx-samples/src/main/java/org/koin/sample/sandbox/di/AppModule.kt index 3676d3aef..2c531becb 100644 --- a/examples/androidx-samples/src/main/java/org/koin/sample/sandbox/di/AppModule.kt +++ b/examples/androidx-samples/src/main/java/org/koin/sample/sandbox/di/AppModule.kt @@ -5,7 +5,6 @@ import org.koin.androidx.viewmodel.dsl.viewModel import org.koin.androidx.viewmodel.dsl.viewModelOf import org.koin.androidx.workmanager.dsl.workerOf import org.koin.core.module.dsl.* -import org.koin.core.module.includes import org.koin.core.qualifier.named import org.koin.dsl.lazyModule import org.koin.sample.sandbox.components.Counter @@ -120,6 +119,4 @@ val navModule = lazyModule { viewModelOf(::NavViewModel) } -val allModules = lazyModule { - includes(appModule, mvpModule, mvvmModule , scopeModule , workerServiceModule , workerScopedModule , navModule , scopeModuleActivityA) -} \ No newline at end of file +val allModules = appModule + mvpModule + mvvmModule + scopeModule + workerServiceModule + workerScopedModule + navModule + scopeModuleActivityA diff --git a/projects/core/koin-core-coroutines/src/commonMain/kotlin/org/koin/core/module/LazyModule.kt b/projects/core/koin-core-coroutines/src/commonMain/kotlin/org/koin/core/module/LazyModule.kt new file mode 100644 index 000000000..0e086b379 --- /dev/null +++ b/projects/core/koin-core-coroutines/src/commonMain/kotlin/org/koin/core/module/LazyModule.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2017-Present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.koin.core.module + +/** + * Koin Lazy Module - an implementation of [Lazy]<[Module]>. + * + * Accepts a lambda that initializes a [Module] via [lazy()][lazy] + * using [LazyThreadSafetyMode.NONE] as thread-safety [mode][LazyThreadSafetyMode]. + * + * @author Chris Paleopanos + * @param moduleInitializer a lambda that will be used to initialize a [Module] lazily + */ +@KoinDslMarker +class LazyModule(moduleInitializer: () -> Module) : Lazy by lazy(LazyThreadSafetyMode.NONE, moduleInitializer) { + + /** + * Adds and returns [this][LazyModule] and [other] as a list of [Lazy]<[Module]> + * + * @param other the [LazyModule] to be added + * @return a [List] of [Lazy]<[Module]> + */ + operator fun plus(other: LazyModule): List = listOf(this, other) + + /** + * Adds and returns [this][LazyModule] and [others] as a list of [Lazy]<[Module]> + * + * @param others the [LazyModule] list to be added + * @return a [List] of [Lazy]<[Module]> + */ + operator fun plus(others: List): List = listOf(this) + others +} diff --git a/projects/core/koin-core-coroutines/src/commonMain/kotlin/org/koin/dsl/LazyModuleDSL.kt b/projects/core/koin-core-coroutines/src/commonMain/kotlin/org/koin/dsl/LazyModuleDSL.kt index 05bcb9654..8b46746d7 100644 --- a/projects/core/koin-core-coroutines/src/commonMain/kotlin/org/koin/dsl/LazyModuleDSL.kt +++ b/projects/core/koin-core-coroutines/src/commonMain/kotlin/org/koin/dsl/LazyModuleDSL.kt @@ -17,6 +17,7 @@ package org.koin.dsl import org.koin.core.annotation.KoinExperimentalAPI import org.koin.core.module.KoinDslMarker +import org.koin.core.module.LazyModule import org.koin.core.module.Module /** @@ -31,4 +32,4 @@ import org.koin.core.module.Module */ @KoinExperimentalAPI @KoinDslMarker -fun lazyModule(moduleDefinition: ModuleDeclaration): Lazy = lazy(LazyThreadSafetyMode.NONE) { module(moduleDeclaration = moduleDefinition) } +fun lazyModule(moduleDefinition: ModuleDeclaration): LazyModule = LazyModule { module(moduleDeclaration = moduleDefinition) } diff --git a/projects/core/koin-core-coroutines/src/jvmTest/kotlin/org/koin/test/LazyModuleTest.kt b/projects/core/koin-core-coroutines/src/jvmTest/kotlin/org/koin/test/LazyModuleTest.kt index 9226c26f0..3f655b0f2 100644 --- a/projects/core/koin-core-coroutines/src/jvmTest/kotlin/org/koin/test/LazyModuleTest.kt +++ b/projects/core/koin-core-coroutines/src/jvmTest/kotlin/org/koin/test/LazyModuleTest.kt @@ -40,8 +40,8 @@ class LazyModuleTest { assertNotNull(koin.getOrNull()) } - - @Test + + @Test fun test_dispatchers() { var resolved: Boolean? = null val m2 = lazyModule { @@ -62,4 +62,29 @@ class LazyModuleTest { assertNotNull(koin.getOrNull()) } + + @Test + fun test_plus() { + var m2Resolved: Boolean? = null + val m2 = lazyModule { + m2Resolved = true + singleOf(::ClassB) + } + var m1Resolved: Boolean? = null + val m1 = lazyModule { + m1Resolved = true + singleOf(::ClassA) { bind() } + } + + assertTrue(m2Resolved == null, "m2Resolved should be null: $m2Resolved") + assertTrue(m1Resolved == null, "m1Resolved should be null: $m1Resolved") + + val koin = koinApplication { + printLogger(Level.DEBUG) + lazyModules(m1 + m2) + }.koin + koin.waitAllStartJobs() + + assertNotNull(koin.getOrNull()) + } }