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

Fix parameters stability inject #2081

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion examples/gradle/versions.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ext {
// Kotlin
kotlin_version = '2.0.20'
kotlin_version = '2.0.21'
// Koin Versions
koin_version = '4.0.1-Beta2'
koin_android_version = koin_version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class MainApplication : Application() {
super.onCreate()

startKoin {
// androidLogger(Level.DEBUG)
printLogger(Level.DEBUG)
androidLogger(Level.DEBUG)
// printLogger(Level.DEBUG)
androidContext(this@MainApplication)
modules(appModule)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
package org.koin.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import org.koin.core.annotation.KoinInternalApi
import org.koin.core.parameter.ParametersDefinition
import org.koin.core.parameter.emptyParametersHolder
import org.koin.core.parameter.ParametersHolder
import org.koin.core.qualifier.Qualifier
import org.koin.core.scope.Scope

Expand All @@ -33,18 +32,15 @@ import org.koin.core.scope.Scope
*
* @author Arnaud Giuliani
*/
@OptIn(KoinInternalApi::class)
@Composable
inline fun <reified T> koinInject(
qualifier: Qualifier? = null,
scope: Scope = currentKoinScope(),
noinline parameters: ParametersDefinition? = null,
): T {
// This will always refer to the latest parameters
val currentParameters by rememberUpdatedState(parameters)

return remember(qualifier, scope) {
scope.get(qualifier) {
currentParameters?.invoke() ?: emptyParametersHolder()
}
val params: ParametersHolder? = parameters?.invoke()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect as it will recreate parameters on every recomposition

return remember(qualifier, scope, params) {
scope.getWithParameters(T::class, qualifier,params)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ open class ParametersHolder(
}

override fun toString(): String = "DefinitionParameters${_values.toList()}"

override fun equals(other: Any?): Boolean {
if (other is ParametersHolder){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect as the equals check is unstable for parameter subclasses

return this.values == other.values
} else return false
}

override fun hashCode(): Int {
return values.hashCode()
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,15 @@ class ParametersHolderTest {
val p = parametersOf(Simple.Component1())
assertNotNull(p.get<Simple.ComponentInterface1>())
}

@Test
fun `equality check`() {
val p1 = parametersOf(1, 2, 3, 4)
val p2 = parametersOf(1, 2, 3, 4)
val p3 = parametersOf(1, 2, 3)

assertEquals(p1, p2)

assertNotEquals(p1,p3)
}
}
Loading