-
-
Notifications
You must be signed in to change notification settings - Fork 727
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
Created koin-decompose module #2062
Open
idfinance-oss
wants to merge
5
commits into
InsertKoinIO:4.1.0
Choose a base branch
from
idfinance-oss:feature/koin-decompose
base: 4.1.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b273fc6
Created koin-decompose module
sergei-mikhailovskii-idf 67a2577
Added doc comments
sergei-mikhailovskii-idf 19da42a
Merge branch '4.1.0' into feature/koin-decompose
idfinance-oss e64c33a
Added the decompose.md
sergei-mikhailovskii-idf 31f5b53
Merge remote-tracking branch 'origin/feature/koin-decompose' into fea…
sergei-mikhailovskii-idf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- | ||
title: Managing Decompose Scopes | ||
--- | ||
|
||
The `koin-decompose` module is dedicated to manage scopes inside the Decompose components. Works pretty the same as [Android setup](/reference/koin-android/scope) | ||
|
||
## Scope for Decompose components | ||
|
||
### Declare a Decompose Scope | ||
|
||
To scope dependencies on an Decompose component, you have to declare a scope section with the `scope` block like follow: | ||
|
||
```kotlin | ||
class MyUseCase() | ||
class MyDelegate(val useCase : MyUseCase) | ||
|
||
module { | ||
// Declare scope for MyComponent | ||
scope<MyComponent> { | ||
// get MyDelegate instance from current scope | ||
scoped { MyDelegate(get()) } | ||
scoped { MyUseCase() } | ||
} | ||
} | ||
``` | ||
|
||
### Decompose Scope Interface | ||
|
||
Under the hood, Decompose scopes needs to be used with `DecomposeScopeComponent` interface to implement `scope` field like this: | ||
|
||
```kotlin | ||
class MyComponent( | ||
context: ComponentContext, | ||
) : ComponentContext by context, DecomposeScopeComponent { | ||
|
||
override val scope: Scope by componentScope() | ||
} | ||
``` | ||
|
||
We need to use the `DecomposeScopeComponent` interface and implement the `scope` property. This will setting up the default scope used by your class. | ||
|
||
### Decompose Scope API | ||
|
||
To create a Koin scope bound to a Decompose component, just use the following function: | ||
- `createComponentScope()` - Create Scope for current Component (scope is not linked to the parent component) | ||
|
||
This function is available as delegate, to implement scope: | ||
|
||
- `componentScope()` - Create Scope for current Component (scope is not linked to the parent component) | ||
|
||
### DecomposeScopeComponent and Scope closing handling | ||
|
||
You can run some code before Koin Scope is being destroyed, by overriding `onCloseScope` function from `DecomposeScopeComponent`: | ||
|
||
```kotlin | ||
class MyComponent( | ||
context: ComponentContext, | ||
) : ComponentContext by context, DecomposeScopeComponent { | ||
|
||
override val scope: Scope by componentScope() | ||
|
||
override fun onCloseScope() { | ||
// Called before closing the Scope | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public final class org/koin/decompose/ComponentContextExtKt { | ||
public static final fun componentScope (Lorg/koin/decompose/DecomposeScopeComponent;)Lkotlin/Lazy; | ||
public static final fun createComponentScope (Lorg/koin/decompose/DecomposeScopeComponent;)Lorg/koin/core/scope/Scope; | ||
} | ||
|
||
public abstract interface class org/koin/decompose/DecomposeScopeComponent : com/arkivanov/decompose/ComponentContext, org/koin/core/component/KoinScopeComponent { | ||
public abstract fun onScopeClose (Lorg/koin/core/scope/Scope;)V | ||
} | ||
|
||
public final class org/koin/decompose/DecomposeScopeComponent$DefaultImpls { | ||
public static fun getKoin (Lorg/koin/decompose/DecomposeScopeComponent;)Lorg/koin/core/Koin; | ||
public static fun onScopeClose (Lorg/koin/decompose/DecomposeScopeComponent;Lorg/koin/core/scope/Scope;)V | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
||
plugins { | ||
alias(libs.plugins.kotlinMultiplatform) | ||
} | ||
|
||
val koinVersion: String by project | ||
version = koinVersion | ||
|
||
kotlin { | ||
jvmToolchain(11) | ||
jvm { | ||
withJava() | ||
} | ||
|
||
js(IR) { | ||
nodejs() | ||
browser() | ||
binaries.executable() | ||
} | ||
|
||
wasmJs { | ||
nodejs() | ||
binaries.executable() | ||
} | ||
|
||
iosX64() | ||
iosArm64() | ||
iosSimulatorArm64() | ||
macosX64() | ||
macosArm64() | ||
|
||
sourceSets { | ||
commonMain.dependencies { | ||
api(project(":core:koin-core")) | ||
api(libs.decompose) | ||
} | ||
} | ||
} | ||
|
||
tasks.withType<KotlinCompile>().all { | ||
compilerOptions { | ||
jvmTarget.set(JvmTarget.JVM_11) | ||
} | ||
} | ||
|
||
apply(from = file("../../gradle/publish.gradle.kts")) |
34 changes: 34 additions & 0 deletions
34
.../decompose/koin-decompose/src/commonMain/kotlin/org/koin/decompose/ComponentContextExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.koin.decompose | ||
|
||
import com.arkivanov.essenty.lifecycle.doOnDestroy | ||
import org.koin.core.component.getScopeId | ||
import org.koin.core.component.getScopeName | ||
import org.koin.core.scope.Scope | ||
import org.koin.core.scope.ScopeCallback | ||
|
||
/** | ||
* Create Scope for Component. | ||
* Doesn't link to parent component's Scope | ||
*/ | ||
fun DecomposeScopeComponent.createComponentScope() = | ||
getKoin().getScopeOrNull(getScopeId()) ?: createScopeForCurrentLifecycle() | ||
|
||
/** | ||
* Provide scope tied to Component | ||
*/ | ||
fun DecomposeScopeComponent.componentScope() = lazy { createComponentScope() } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tag it as experimental with |
||
|
||
private fun DecomposeScopeComponent.createScopeForCurrentLifecycle(): Scope { | ||
val scope = getKoin().createScope(getScopeId(), getScopeName(), this) | ||
scope.registerCallback( | ||
object : ScopeCallback { | ||
override fun onScopeClose(scope: Scope) = this@createScopeForCurrentLifecycle.onScopeClose(scope) | ||
}, | ||
) | ||
lifecycle.doOnDestroy { | ||
if (scope.isNotClosed()) { | ||
scope.close() | ||
} | ||
} | ||
return scope | ||
} |
16 changes: 16 additions & 0 deletions
16
...ompose/koin-decompose/src/commonMain/kotlin/org/koin/decompose/DecomposeScopeComponent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.koin.decompose | ||
|
||
import com.arkivanov.decompose.ComponentContext | ||
import org.koin.core.component.KoinScopeComponent | ||
import org.koin.core.scope.Scope | ||
|
||
/** | ||
* Decompose Component that can handle a Koin Scope | ||
*/ | ||
interface DecomposeScopeComponent : KoinScopeComponent, ComponentContext { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tag it as experimental with |
||
|
||
/** | ||
* Called before closing a scope, on onDestroy | ||
*/ | ||
fun onScopeClose(scope: Scope) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tag it as experimental with
@KoinExperimentalApi