/
GlobalContext.kt
84 lines (70 loc) · 2.97 KB
/
GlobalContext.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Copyright 2017-Present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.koin.core.context
import co.touchlab.stately.concurrency.Lock
import co.touchlab.stately.concurrency.withLock
import org.koin.core.Koin
import org.koin.core.KoinApplication
import org.koin.core.error.ApplicationAlreadyStartedException
import org.koin.core.module.Module
import org.koin.dsl.KoinAppDeclaration
internal fun globalContextByMemoryModel(): KoinContext = MutableGlobalContext()
/**
* Mutable global context for the new memory model. Very similar to how the JVM global context works.
*/
internal class MutableGlobalContext : KoinContext {
private val lock = Lock()
private var _koin: Koin? = null
private var _koinApplication: KoinApplication? = null
override fun get(): Koin = _koin ?: error("KoinApplication has not been started")
override fun getOrNull(): Koin? = _koin
fun getKoinApplicationOrNull(): KoinApplication? = _koinApplication
private fun register(koinApplication: KoinApplication) {
if (_koin != null) {
throw ApplicationAlreadyStartedException("A Koin Application has already been started")
}
_koinApplication = koinApplication
_koin = koinApplication.koin
}
override fun stopKoin() = lock.withLock {
_koin?.close()
_koin = null
}
override fun startKoin(koinApplication: KoinApplication): KoinApplication = lock.withLock {
register(koinApplication)
koinApplication.createEagerInstances()
koinApplication
}
override fun startKoin(appDeclaration: KoinAppDeclaration): KoinApplication = lock.withLock {
val koinApplication = KoinApplication.init()
register(koinApplication)
appDeclaration(koinApplication)
koinApplication.createEagerInstances()
koinApplication
}
override fun loadKoinModules(module: Module, createEagerInstances : Boolean) = lock.withLock {
get().loadModules(listOf(module), createEagerInstances = createEagerInstances)
}
override fun loadKoinModules(modules: List<Module>, createEagerInstances : Boolean) = lock.withLock {
get().loadModules(modules, createEagerInstances = createEagerInstances)
}
override fun unloadKoinModules(module: Module) = lock.withLock {
get().unloadModules(listOf(module))
}
override fun unloadKoinModules(modules: List<Module>) = lock.withLock {
get().unloadModules(modules)
}
}