Skip to content

Commit

Permalink
wip: Improve resolution logic
Browse files Browse the repository at this point in the history
Adding a new dependency of an already existing type should reset all
dependencies that depend on that class/interface/type so they can be
re-resolved with the new dependency.
  • Loading branch information
mantono committed Apr 28, 2023
1 parent ca3f50a commit 1f9b1af
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 23 deletions.
15 changes: 11 additions & 4 deletions lib/src/main/kotlin/io/nexure/capsule/Capsule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ private constructor(
vararg parents: Capsule,
config: Configuration.() -> Unit
): Capsule {
val dependencies: LinkedList<Dependency> = parents.map { it.dependencies }.flatten().let { LinkedList(it) }
val dependencies: LinkedList<Dependency> = parents
.map { it.dependencies }
.flatten()
.map { it.reset() }
.let { LinkedList(it) }

val parentsPriority: Int = parents.minOfOrNull { it.priority } ?: DEFAULT_PRIORITY
val priority: Int = parentsPriority - 1
val moduleConfig = Configuration(priority, dependencies).apply(config)
Expand All @@ -92,18 +97,20 @@ internal class Dependency
private constructor(
val key: String,
val priority: Int,
constructor: () -> Any,
private val constructor: () -> Any,
): Comparable<Dependency> {
private val instance: LazyValue<Any> = LazyValue(constructor)
private val instance: Lazy<Any> = lazy(initializer = constructor)

fun getInstance(): Any {
return try {
instance()
instance.value
} catch (e: Exception) {
throw DependencyException(key, e)
}
}

fun reset(): Dependency = Dependency(key, priority, constructor)

override fun toString(): String = key

override fun compareTo(other: Dependency): Int = this.priority.compareTo(other.priority)
Expand Down
19 changes: 0 additions & 19 deletions lib/src/main/kotlin/io/nexure/capsule/LazyValue.kt

This file was deleted.

0 comments on commit 1f9b1af

Please sign in to comment.