koin icon indicating copy to clipboard operation
koin copied to clipboard

Composable fun koinInject() does not recreate the entity with new parameters

Open puritanin opened this issue 1 year ago • 0 comments

Describe the bug Composable fun koinInject() does not recreate the entity with new parameters in last versions of Koin (3.5.3, 4.0.0-RC2).

Old version of deprecated fun get() - solves the problem (but with nuances):

@Composable
@Deprecated("use koinInject() instead")
inline fun <reified T> get(
    qualifier: Qualifier? = null,
    scope: Scope = currentKoinScope(),
    noinline parameters: ParametersDefinition? = null,
): T = remember(qualifier, parameters, scope) {               <=== HERE (recreate)
    scope.get(qualifier, parameters)
}

Version 3.5.3 - does not solve the problem:

@Composable
inline fun <reified T> koinInject(
    qualifier: Qualifier? = null,
    scope: Scope = currentKoinScope(),
    noinline parameters: ParametersDefinition? = null,
): T {
    val st = parameters?.let { rememberStableParametersDefinition(parameters) }
    return remember(qualifier, scope) {                                            <=== HERE (does not recreate)
        scope.get(qualifier, st?.parametersDefinition)
    }
}

Version 4.0.0 - does not solve the problem:

@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) {                                     <=== HERE (does not recreate)
        scope.get(qualifier) {
            currentParameters?.invoke() ?: emptyParametersHolder()
        }
    }
}

Expected behavior The fun koinInject() should recreate the entity when the parameters change, like the previous fun get().

Koin module and version: koin-compose 3.5.3 - 4.0.0-RC2

Snippet or Sample project to help reproduce

https://gist.github.com/puritanin/e23d9948f1909d2dbbb8d34675969e95

puritanin avatar Sep 03 '24 19:09 puritanin