Skip to content

Commit

Permalink
Merge pull request #1768 from hoc081098/fix-koinInject
Browse files Browse the repository at this point in the history
koin-compose: use rememberUpdatedState with ParametersDefinition, optimized KoinApplication, KoinContext, KoinIsolatedContext
  • Loading branch information
arnaudgiuliani committed Apr 15, 2024
2 parents f0e5c88 + 29fee0b commit 45da225
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 90 deletions.
15 changes: 0 additions & 15 deletions projects/compose/koin-compose/api/koin-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ public final class org/koin/compose/KoinApplicationKt {
public static final fun rememberCurrentKoinScope (Landroidx/compose/runtime/Composer;I)Lorg/koin/core/scope/Scope;
}

public final class org/koin/compose/error/UnknownKoinContext : java/lang/RuntimeException {
public static final field $stable I
public fun <init> ()V
}

public final class org/koin/compose/module/CompositionKoinModuleLoader : androidx/compose/runtime/RememberObserver {
public static final field $stable I
public fun <init> (Ljava/util/List;Lorg/koin/core/Koin;ZZ)V
Expand Down Expand Up @@ -50,13 +45,3 @@ public final class org/koin/compose/scope/RememberScopesKt {
public static final fun rememberKoinScope (Lorg/koin/core/scope/Scope;Landroidx/compose/runtime/Composer;I)Lorg/koin/core/scope/Scope;
}

public final class org/koin/compose/stable/StableHoldersKt {
public static final fun rememberStableParametersDefinition (Lkotlin/jvm/functions/Function0;Landroidx/compose/runtime/Composer;I)Lorg/koin/compose/stable/StableParametersDefinition;
}

public final class org/koin/compose/stable/StableParametersDefinition {
public static final field $stable I
public fun <init> (Lkotlin/jvm/functions/Function0;)V
public final fun getParametersDefinition ()Lkotlin/jvm/functions/Function0;
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
package org.koin.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import org.koin.compose.stable.rememberStableParametersDefinition
import androidx.compose.runtime.rememberUpdatedState
import org.koin.core.parameter.ParametersDefinition
import org.koin.core.parameter.emptyParametersHolder
import org.koin.core.qualifier.Qualifier
import org.koin.core.scope.Scope

Expand All @@ -37,9 +39,13 @@ inline fun <reified T> koinInject(
scope: Scope = currentKoinScope(),
noinline parameters: ParametersDefinition? = null,
): T {
val st = parameters?.let { rememberStableParametersDefinition(parameters) }
// This will always refer to the latest parameters
val currentParameters by rememberUpdatedState(parameters)

return remember(qualifier, scope) {
scope.get(qualifier, st?.parametersDefinition)
scope.get(qualifier) {
currentParameters?.invoke() ?: emptyParametersHolder()
}
}
}

Expand All @@ -51,14 +57,12 @@ inline fun <reified T> koinInject(
* @author Arnaud Giuliani
*/
@Composable
@Deprecated("")
@Deprecated(
message = "Use koinInject() instead",
replaceWith = ReplaceWith("koinInject<T>(qualifier, scope, parameters)")
)
inline fun <reified T> rememberKoinInject(
qualifier: Qualifier? = null,
scope: Scope = rememberCurrentKoinScope(),
noinline parameters: ParametersDefinition? = null,
): T {
val st = parameters?.let { rememberStableParametersDefinition(parameters) }
return remember(qualifier, scope) {
scope.get(qualifier, st?.parametersDefinition)
}
}
): T = koinInject(qualifier, scope, parameters)
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@
* limitations under the License.
*/

@file:OptIn(KoinInternalApi::class)

package org.koin.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.InternalComposeApi
import androidx.compose.runtime.ProvidableCompositionLocal
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.currentComposer
import androidx.compose.runtime.remember
import org.koin.core.Koin
import org.koin.core.KoinApplication
Expand All @@ -47,6 +44,7 @@ val LocalKoinApplication: ProvidableCompositionLocal<Koin> = compositionLocalOf
/**
* Current Koin Scope
*/
@OptIn(KoinInternalApi::class)
val LocalKoinScope: ProvidableCompositionLocal<Scope> = compositionLocalOf {
getDefaultKoinContext().apply {
warnNoContext()
Expand All @@ -61,9 +59,8 @@ private fun getDefaultKoinContext() = KoinPlatformTools.defaultContext().get()
* @author @author jjkester
*/
@Composable
fun getKoin(): Koin = currentComposer.run {
return LocalKoinApplication.current
}
@ReadOnlyComposable
fun getKoin(): Koin = LocalKoinApplication.current

/**
* Retrieve the current Koin scope from the composition
Expand All @@ -72,27 +69,26 @@ fun getKoin(): Koin = currentComposer.run {
*
*/
@Composable
fun currentKoinScope(): Scope = currentComposer.run {
return LocalKoinScope.current
}
@ReadOnlyComposable
fun currentKoinScope(): Scope = LocalKoinScope.current

/**
* Retrieve the current Koin scope from the composition
*
* @author @author jjkester
*
*/
@OptIn(InternalComposeApi::class)
@Deprecated(
message = "Use currentKoinScope() instead",
replaceWith = ReplaceWith("currentKoinScope()")
)
@Composable
fun rememberCurrentKoinScope(): Scope = currentComposer.run {
remember {
consume(LocalKoinScope)
}
}
fun rememberCurrentKoinScope(): Scope = currentKoinScope()

@OptIn(KoinInternalApi::class)
private fun Koin.warnNoContext() {
logger.info("[Warning] - No Koin context defined in Compose, fallback to default Koin context.\nUse KoinContext(), KoinAndroidContext() or KoinApplication() to setup or create Koin context with Compose and avoid such message.")
logger.info("[Warning] - No Koin context defined in Compose, fallback to default Koin context." +
"Use KoinContext(), KoinAndroidContext() or KoinApplication() to setup or create Koin context with Compose and avoid such message.")
}

/**
Expand All @@ -105,6 +101,7 @@ private fun Koin.warnNoContext() {
* @throws ApplicationAlreadyStartedException
* @author Arnaud Giuliani
*/
@OptIn(KoinInternalApi::class)
@Composable
@Throws(ApplicationAlreadyStartedException::class)
fun KoinApplication(
Expand All @@ -121,10 +118,9 @@ fun KoinApplication(
}
CompositionLocalProvider(
LocalKoinApplication provides koinApplication.koin,
LocalKoinScope provides koinApplication.koin.scopeRegistry.rootScope
) {
content()
}
LocalKoinScope provides koinApplication.koin.scopeRegistry.rootScope,
content = content
)
}

/**
Expand All @@ -135,17 +131,17 @@ fun KoinApplication(
*
* @author Arnaud Giuliani
*/
@OptIn(KoinInternalApi::class)
@Composable
fun KoinContext(
context: Koin = KoinPlatform.getKoin(),
content: @Composable () -> Unit
) {
CompositionLocalProvider(
LocalKoinApplication provides context,
LocalKoinScope provides context.scopeRegistry.rootScope
) {
content()
}
LocalKoinScope provides context.scopeRegistry.rootScope,
content = content
)
}

/**
Expand All @@ -161,15 +157,15 @@ fun KoinContext(
*
* @author Arnaud Giuliani
*/
@OptIn(KoinInternalApi::class)
@Composable
fun KoinIsolatedContext(
context: KoinApplication,
content: @Composable () -> Unit
) {
CompositionLocalProvider(
LocalKoinApplication provides context.koin,
LocalKoinScope provides context.koin.scopeRegistry.rootScope
) {
content()
}
LocalKoinScope provides context.koin.scopeRegistry.rootScope,
content = content
)
}

This file was deleted.

This file was deleted.

0 comments on commit 45da225

Please sign in to comment.