Skip to content

Commit

Permalink
Fix back VM resolution on root scope only, add scope logs
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed Mar 27, 2024
1 parent eb6a982 commit 35f0e8a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T : ViewModel> resolveViewModel(
vmClass: KClass<T>,
viewModelStore: ViewModelStore,
Expand All @@ -35,33 +34,10 @@ fun <T : ViewModel> 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 <T : ViewModel> resolveViewModel(
vmClass: KClass<T>,
viewModelStore: ViewModelStore,
key: String? = null,
extras: CreationExtras,
qualifier: Qualifier? = null,
koin : Koin,
parameters: ParametersDefinition? = null,
): T {
val modelClass: Class<T> = 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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(qualifier, clazz, parameters)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Simple.ComponentA> {
scopedOf(Simple::ComponentB)
}
}
)
}.koin

val a = koin.get<Simple.ComponentA>()
val scope = koin.createScope<Simple.ComponentA>("_a_",a)
val b = scope.get<Simple.ComponentB>()
scope.get<Simple.ComponentC> { parametersOf(b) }
}
}

0 comments on commit 35f0e8a

Please sign in to comment.