diff options
author | Linnea Gräf <nea@nea.moe> | 2024-12-23 02:22:06 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-12-23 02:22:06 +0100 |
commit | f0285e44ed113bcf2a11b5d6635f6124deb7fc3b (patch) | |
tree | 7ebcc5faaf014287f17696c501351447cbe8890d /src/main/kotlin/moe/nea/ledger/utils/di/DI.kt | |
parent | 6b7f5b95b0541eb0e76855152e283e5f9cad0c3c (diff) | |
download | LocalTransactionLedger-f0285e44ed113bcf2a11b5d6635f6124deb7fc3b.tar.gz LocalTransactionLedger-f0285e44ed113bcf2a11b5d6635f6124deb7fc3b.tar.bz2 LocalTransactionLedger-f0285e44ed113bcf2a11b5d6635f6124deb7fc3b.zip |
feat: Add in game updater
Diffstat (limited to 'src/main/kotlin/moe/nea/ledger/utils/di/DI.kt')
-rw-r--r-- | src/main/kotlin/moe/nea/ledger/utils/di/DI.kt | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/utils/di/DI.kt b/src/main/kotlin/moe/nea/ledger/utils/di/DI.kt index 133637a..a9061d7 100644 --- a/src/main/kotlin/moe/nea/ledger/utils/di/DI.kt +++ b/src/main/kotlin/moe/nea/ledger/utils/di/DI.kt @@ -9,25 +9,32 @@ class DI { private fun formatInjectionStack() = injectionStack.joinToString(" -> ") + fun <T : Any> getProvider(type: Class<T>): BaseDIProvider<T, *> { + val provider = providers[type] as BaseDIProvider<T, *>? + ?: error("Could not find provider for type $type") + return provider + } + private fun <T : Any, C> internalProvide(type: Class<T>, element: AnnotatedElement? = null): T { - val provider = providers[type] as BaseDIProvider<T, C> - val context = if (element == null) provider.createEmptyContext() else provider.createContext(element) - val key = Pair(type, context) - val existingValue = values[key] - if (existingValue != null) return existingValue as T - if (type in injectionStack) { - error("Found injection cycle: ${formatInjectionStack()} -> $type") - } - injectionStack.push(type) - val value = try { - provider.provideWithContext(this, context) + try { + val provider = getProvider(type) as BaseDIProvider<T, C> + val context = if (element == null) provider.createEmptyContext() else provider.createContext(element) + val key = Pair(type, context) + val existingValue = values[key] + if (existingValue != null) return existingValue as T + if (type in injectionStack) { + error("Found injection cycle: ${formatInjectionStack()} -> $type") + } + injectionStack.push(type) + val value = + provider.provideWithContext(this, context) + val cycleCheckCookie = injectionStack.pop() + require(cycleCheckCookie == type) { "Unbalanced stack cookie: $cycleCheckCookie != $type" } + values[key] = value + return value } catch (ex: Exception) { throw RuntimeException("Could not create instance for type $type (in stack ${formatInjectionStack()})", ex) } - val cycleCheckCookie = injectionStack.pop() - require(cycleCheckCookie == type) { "Unbalanced stack cookie: $cycleCheckCookie != $type" } - values[key] = value - return value } fun <T : Any> provide(type: Class<T>, element: AnnotatedElement? = null): T { |