Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Lazy<Module> concrete implementation & missing "plus" operator #1756

Merged
merged 5 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Module> by lazy(LazyThreadSafetyMode.NONE, moduleInitializer) {
cpaleop marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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<LazyModule> = 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<LazyModule>): List<LazyModule> = listOf(this) + others
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -31,4 +32,4 @@ import org.koin.core.module.Module
*/
@KoinExperimentalAPI
@KoinDslMarker
fun lazyModule(moduleDefinition: ModuleDeclaration): Lazy<Module> = lazy(LazyThreadSafetyMode.NONE) { module(moduleDeclaration = moduleDefinition) }
fun lazyModule(moduleDefinition: ModuleDeclaration): LazyModule = LazyModule { module(moduleDeclaration = moduleDefinition) }
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,29 @@ class LazyModuleTest {

assertNotNull(koin.getOrNull<ClassB>())
}

@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<IClassA>() }
}

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<ClassB>())
}
}