From 35f0e8ac166bd0cb0499f455384902cf770f7080 Mon Sep 17 00:00:00 2001 From: Arnaud Giuliani Date: Wed, 27 Mar 2024 19:27:09 +0100 Subject: [PATCH] Fix back VM resolution on root scope only, add scope logs --- .../koin/androidx/viewmodel/GetViewModel.kt | 28 ++------------- .../org/koin/core/registry/ScopeRegistry.kt | 2 +- .../kotlin/org/koin/core/scope/Scope.kt | 3 +- .../kotlin/org/koin/core/ScopeInfoTest.kt | 34 +++++++++++++++++++ 4 files changed, 39 insertions(+), 28 deletions(-) create mode 100644 projects/core/koin-core/src/commonTest/kotlin/org/koin/core/ScopeInfoTest.kt diff --git a/projects/android/koin-android/src/main/java/org/koin/androidx/viewmodel/GetViewModel.kt b/projects/android/koin-android/src/main/java/org/koin/androidx/viewmodel/GetViewModel.kt index c5725d985..defc60666 100644 --- a/projects/android/koin-android/src/main/java/org/koin/androidx/viewmodel/GetViewModel.kt +++ b/projects/android/koin-android/src/main/java/org/koin/androidx/viewmodel/GetViewModel.kt @@ -26,7 +26,6 @@ import kotlin.reflect.KClass * @param parameters - for instance building injection */ @KoinInternalApi -@Deprecated("scope is not used for ViewModel creation. This will fallback to root scope.") fun resolveViewModel( vmClass: KClass, viewModelStore: ViewModelStore, @@ -35,33 +34,10 @@ fun resolveViewModel( qualifier: Qualifier? = null, scope: Scope, parameters: ParametersDefinition? = null, -): T { - return resolveViewModel(vmClass, viewModelStore, key, extras, qualifier, scope.getKoin(), parameters) -} - -/** - * Resolve a ViewModel instance - * - * @param vmClass - * @param viewModelStore - * @param key - * @param extras - @see CreationExtras - * @param qualifier - * @param koin - * @param parameters - for instance building injection - */ -@KoinInternalApi -fun resolveViewModel( - vmClass: KClass, - viewModelStore: ViewModelStore, - key: String? = null, - extras: CreationExtras, - qualifier: Qualifier? = null, - koin : Koin, - parameters: ParametersDefinition? = null, ): T { val modelClass: Class = vmClass.java - val factory = KoinViewModelFactory(vmClass, koin.scopeRegistry.rootScope, qualifier, parameters) + //TODO In 3.6 - propose to resolve scope strictly from root or not + val factory = KoinViewModelFactory(vmClass, scope, qualifier, parameters) val provider = ViewModelProvider(viewModelStore, factory, extras) val vmKey = getViewModelKey(qualifier, key) diff --git a/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/registry/ScopeRegistry.kt b/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/registry/ScopeRegistry.kt index 95034e020..ede78c2f1 100644 --- a/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/registry/ScopeRegistry.kt +++ b/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/registry/ScopeRegistry.kt @@ -55,7 +55,7 @@ class ScopeRegistry(private val _koin: Koin) { @PublishedApi internal fun createScope(scopeId: ScopeID, qualifier: Qualifier, source: Any? = null): Scope { - _koin.logger.debug("|- (+) Scope - id:'$scopeId' q:$qualifier") + _koin.logger.debug("| (+) Scope - id:'$scopeId' q:$qualifier") if (!_scopeDefinitions.contains(qualifier)) { _koin.logger.debug("| Scope '$qualifier' not defined. Creating it ...") _scopeDefinitions.add(qualifier) diff --git a/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/scope/Scope.kt b/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/scope/Scope.kt index 1cc62f766..4127d04b1 100644 --- a/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/scope/Scope.kt +++ b/projects/core/koin-core/src/commonMain/kotlin/org/koin/core/scope/Scope.kt @@ -199,7 +199,8 @@ class Scope( ): T { return if (_koin.logger.isAt(Level.DEBUG)) { val qualifierString = qualifier?.let { " with qualifier '$qualifier'" } ?: "" - _koin.logger.display(Level.DEBUG, "|- '${clazz.getFullName()}'$qualifierString ...") + val scopeId = if (isRoot) "" else "- scope:'$id" + _koin.logger.display(Level.DEBUG, "|- '${clazz.getFullName()}'$qualifierString $scopeId...") val start = KoinPlatformTimeTools.getTimeInNanoSeconds() val instance = resolveInstance(qualifier, clazz, parameters) diff --git a/projects/core/koin-core/src/commonTest/kotlin/org/koin/core/ScopeInfoTest.kt b/projects/core/koin-core/src/commonTest/kotlin/org/koin/core/ScopeInfoTest.kt new file mode 100644 index 000000000..69c30ab5c --- /dev/null +++ b/projects/core/koin-core/src/commonTest/kotlin/org/koin/core/ScopeInfoTest.kt @@ -0,0 +1,34 @@ +package org.koin.core + +import org.koin.Simple +import org.koin.core.logger.Level +import org.koin.core.module.dsl.scopedOf +import org.koin.core.parameter.parametersOf +import org.koin.dsl.koinApplication +import org.koin.dsl.module +import kotlin.test.Test + +class ScopeInfoTest { + + @Test + fun resolve_via_a_scope(){ + + val koin = koinApplication { + printLogger(Level.DEBUG) + modules( + module { + factory { Simple.ComponentC(get()) } + single { Simple.ComponentA() } + scope { + scopedOf(Simple::ComponentB) + } + } + ) + }.koin + + val a = koin.get() + val scope = koin.createScope("_a_",a) + val b = scope.get() + scope.get { parametersOf(b) } + } +} \ No newline at end of file