0
# Koin Core WASM JS
1
2
Koin Core WASM JS is a pragmatic dependency injection framework for Kotlin Multiplatform projects targeting WebAssembly JavaScript environments. It provides lightweight, DSL-based dependency injection with support for scopes, components, and lifecycle management optimized for WASM JS execution.
3
4
## Package Information
5
6
- **Package Name**: io.insert-koin:koin-core
7
- **Package Type**: maven (Kotlin Multiplatform)
8
- **Language**: Kotlin
9
- **Target**: WebAssembly JavaScript (wasmJs)
10
- **Installation**: Add to your `build.gradle.kts`:
11
12
> **WASM JS Note:** This library is specifically compiled for WebAssembly JavaScript targets, providing optimal performance in WASM JS environments with single-threaded execution model.
13
14
```kotlin
15
dependencies {
16
implementation("io.insert-koin:koin-core:4.1.0")
17
}
18
```
19
20
## Core Imports
21
22
```kotlin
23
import org.koin.core.Koin
24
import org.koin.core.KoinApplication
25
import org.koin.core.component.KoinComponent
26
import org.koin.core.context.startKoin
27
import org.koin.core.context.stopKoin
28
import org.koin.core.module.Module
29
import org.koin.core.scope.Scope
30
import org.koin.dsl.koinApplication
31
import org.koin.dsl.module
32
```
33
34
## Basic Usage
35
36
```kotlin
37
import org.koin.core.context.startKoin
38
import org.koin.core.context.stopKoin
39
import org.koin.core.component.KoinComponent
40
import org.koin.core.component.inject
41
import org.koin.dsl.module
42
43
// Define a service
44
class UserService {
45
fun getUser(id: String) = "User $id"
46
}
47
48
// Define a module
49
val appModule = module {
50
single { UserService() }
51
}
52
53
// Start Koin
54
startKoin {
55
modules(appModule)
56
}
57
58
// Use dependency injection in a component
59
class UserController : KoinComponent {
60
private val userService: UserService by inject()
61
62
fun handleRequest(id: String): String {
63
return userService.getUser(id)
64
}
65
}
66
67
// Stop Koin when done
68
stopKoin()
69
```
70
71
## Architecture
72
73
Koin Core WASM JS is built around several key components:
74
75
- **Application Management**: `KoinApplication` handles configuration and lifecycle
76
- **Dependency Container**: `Koin` manages instance resolution and scope lifecycles
77
- **Module System**: `Module` organizes dependency definitions with DSL support
78
- **Component Integration**: `KoinComponent` and `KoinScopeComponent` provide seamless integration
79
- **Scope Management**: `Scope` enables scoped dependency lifecycles and isolation
80
- **DSL Support**: Rich domain-specific language for configuration and definition
81
- **Global Context**: Convenient global functions for application-wide dependency management
82
83
## Capabilities
84
85
### Application Configuration
86
87
Application lifecycle and configuration management for setting up dependency injection in your WASM JS application.
88
89
```kotlin { .api }
90
fun koinApplication(createEagerInstances: Boolean = true, appDeclaration: KoinAppDeclaration? = null): KoinApplication
91
92
class KoinApplication {
93
val koin: Koin
94
fun modules(vararg modules: Module): KoinApplication
95
fun properties(values: Map<String, Any>): KoinApplication
96
fun logger(logger: Logger): KoinApplication
97
fun close()
98
}
99
```
100
101
[Application Configuration](./application.md)
102
103
### Module Definition
104
105
DSL-based module system for organizing and defining dependencies with lifecycle management.
106
107
```kotlin { .api }
108
fun module(createdAtStart: Boolean = false, moduleDeclaration: ModuleDeclaration): Module
109
110
class Module {
111
val id: String
112
val isLoaded: Boolean
113
inline fun <reified T> single(qualifier: Qualifier? = null, createdAtStart: Boolean = false, noinline definition: Definition<T>): KoinDefinition<T>
114
inline fun <reified T> factory(qualifier: Qualifier? = null, noinline definition: Definition<T>): KoinDefinition<T>
115
}
116
```
117
118
[Module Definition](./modules.md)
119
120
### Dependency Injection
121
122
Core dependency injection functionality for resolving and managing object instances.
123
124
```kotlin { .api }
125
class Koin {
126
inline fun <reified T : Any> get(qualifier: Qualifier? = null, noinline parameters: ParametersDefinition? = null): T
127
inline fun <reified T : Any> inject(qualifier: Qualifier? = null, mode: LazyThreadSafetyMode = KoinPlatformTools.defaultLazyMode(), noinline parameters: ParametersDefinition? = null): Lazy<T>
128
inline fun <reified T> declare(instance: T, qualifier: Qualifier? = null, secondaryTypes: List<KClass<*>> = emptyList(), allowOverride: Boolean = true)
129
}
130
```
131
132
[Dependency Injection](./dependency-injection.md)
133
134
### Scope Management
135
136
Scoped dependency lifecycles for managing instances with controlled lifecycles and isolation.
137
138
```kotlin { .api }
139
class Scope {
140
val scopeQualifier: Qualifier
141
val id: ScopeID
142
val closed: Boolean
143
inline fun <reified T : Any> get(qualifier: Qualifier? = null, noinline parameters: ParametersDefinition? = null): T
144
inline fun <reified T : Any> inject(qualifier: Qualifier? = null, mode: LazyThreadSafetyMode = LazyThreadSafetyMode.SYNCHRONIZED, noinline parameters: ParametersDefinition? = null): Lazy<T>
145
fun close()
146
}
147
```
148
149
[Scope Management](./scopes.md)
150
151
### Component Integration
152
153
Component interfaces and extension functions for seamless dependency injection integration in your classes.
154
155
```kotlin { .api }
156
interface KoinComponent {
157
fun getKoin(): Koin
158
}
159
160
interface KoinScopeComponent : KoinComponent {
161
val scope: Scope
162
}
163
164
// Extension functions
165
inline fun <reified T : Any> KoinComponent.get(qualifier: Qualifier? = null, noinline parameters: ParametersDefinition? = null): T
166
inline fun <reified T : Any> KoinComponent.inject(qualifier: Qualifier? = null, mode: LazyThreadSafetyMode = KoinPlatformTools.defaultLazyMode(), noinline parameters: ParametersDefinition? = null): Lazy<T>
167
```
168
169
[Component Integration](./components.md)
170
171
### Global Context
172
173
Application-wide dependency management with convenient global functions for common operations.
174
175
```kotlin { .api }
176
fun startKoin(appDeclaration: KoinAppDeclaration): KoinApplication
177
fun startKoin(koinApplication: KoinApplication): KoinApplication
178
fun stopKoin()
179
fun loadKoinModules(modules: List<Module>)
180
fun unloadKoinModules(modules: List<Module>)
181
```
182
183
[Global Context](./global-context.md)
184
185
## Common Types
186
187
```kotlin { .api }
188
typealias KoinAppDeclaration = KoinApplication.() -> Unit
189
typealias ModuleDeclaration = Module.() -> Unit
190
typealias ParametersDefinition = () -> ParametersHolder
191
typealias ScopeID = String
192
typealias QualifierValue = String
193
194
interface Qualifier {
195
val value: QualifierValue
196
}
197
198
class ParametersHolder {
199
val values: List<Any?>
200
operator fun <T> get(i: Int): T
201
inline fun <reified T : Any> get(): T
202
fun size(): Int
203
}
204
205
// Factory functions
206
fun named(name: String): StringQualifier
207
inline fun <reified T> named(): TypeQualifier
208
fun parametersOf(vararg parameters: Any?): ParametersHolder
209
```