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

LocalKoinScope stuck with a closed scope #1900

Closed
tlevavasseur-decathlon opened this issue Jun 28, 2024 · 4 comments
Closed

LocalKoinScope stuck with a closed scope #1900

tlevavasseur-decathlon opened this issue Jun 28, 2024 · 4 comments
Labels
compose status:checking currently in analysis - discussion or need more detailed specs type:issue
Milestone

Comments

@tlevavasseur-decathlon
Copy link

Describe the bug
The issue was present in 3.4.X :

@Composable
inline fun <reified T> koinInject(
    qualifier: Qualifier? = null,
    scope: Scope = LocalKoinScope.current,
    noinline parameters: ParametersDefinition? = null,
): T = rememberKoinInject(qualifier, scope, parameters)

@OptIn(KoinInternalApi::class)
val LocalKoinScope = compositionLocalOf { getKoinContext().scopeRegistry.rootScope }

fixed in 3.5.0 :

@Composable
inline fun <reified T> koinInject(
    qualifier: Qualifier? = null,
    scope: Scope = getKoinScope(),
    noinline parameters: ParametersDefinition? = null,
): T = rememberKoinInject(qualifier, scope, parameters)

@OptIn(InternalComposeApi::class)
@Composable
fun getKoinScope(): Scope = currentComposer.run {
    remember {
        try {
            consume(LocalKoinScope)
        } catch (_: UnknownKoinContext) {
            val ctx = getKoinContext()
            warningNoContext(ctx)
            getKoinContext().scopeRegistry.rootScope
        }
    }
}

val LocalKoinScope: ProvidableCompositionLocal<Scope> = 
    compositionLocalOf { throw UnknownKoinContext() }

but reintroduced in 3.5.3 :

val LocalKoinScope: ProvidableCompositionLocal<Scope> = compositionLocalOf {
    getDefaultKoinContext().apply {
        warnNoContext()
    }.scopeRegistry.rootScope
}

private fun getDefaultKoinContext() = KoinPlatformTools.defaultContext().get()

We get stuck with a closed scope, the one active on initialization of LocalKoinScope.

To Reproduce
a simple way to test it is to restart koin and the activity :

MainActivity : 

        Button(onClick = {
            restartKoin()
            context.startActivity(Intent(context, MainActivity::class.java))
        })

fun restartKoin() {
    stopKoin()
    koin = startKoin {  ... }
}

Expected behavior
Like with 3.5.0 : when a new activity wants the LocalKoinScope (with koinInject()) get the currently available Koin instance.

@arnaudgiuliani arnaudgiuliani added this to the 4.0-RC2 milestone Jul 25, 2024
@arnaudgiuliani arnaudgiuliani added type:issue status:checking currently in analysis - discussion or need more detailed specs compose labels Jul 25, 2024
@arnaudgiuliani
Copy link
Member

did you check in 4.0?

@tlevavasseur-decathlon
Copy link
Author

still present in 4.0.0-RC2

indeed the code remains the same since 3.5.3 :

/**
 * Current Koin Scope
 */
@OptIn(KoinInternalApi::class)
val LocalKoinScope: ProvidableCompositionLocal<Scope> = compositionLocalOf {
    getDefaultKoinContext().apply {
        warnNoContext()
    }.scopeRegistry.rootScope
}

private fun getDefaultKoinContext() = KoinPlatformTools.defaultContext().get()

@arnaudgiuliani
Copy link
Member

ok, got it 👌

@arnaudgiuliani
Copy link
Member

Fixed in #1975

arnaudgiuliani added a commit that referenced this issue Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compose status:checking currently in analysis - discussion or need more detailed specs type:issue
Projects
None yet
Development

No branches or pull requests

2 participants