Skip to content

Commit 8e234be

Browse files
lib
1 parent 643e466 commit 8e234be

File tree

12 files changed

+136
-20
lines changed

12 files changed

+136
-20
lines changed

.idea/artifacts/di_multiplatform_lib_jvm_1_0_4.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = "com.azharkova"
8-
version = "1.0.3"
8+
version = "1.0.4.5"
99

1010
repositories {
1111
jcenter()
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.azharkova.di.container
2+
3+
import com.azharkova.di.resolver.Resolver
4+
import com.azharkova.di.scope.ScopeType
5+
import com.azharkova.di.util.createType
6+
import kotlin.native.concurrent.ThreadLocal
7+
import kotlin.reflect.KClass
8+
9+
public interface ContainerProtocol {
10+
fun <T : Any> register(type: String?, scope: ScopeType = ScopeType.Graph, fabric: () -> T?)
11+
12+
fun <T : Any> resolve(type: String?): T?
13+
}
14+
15+
public class DIContainer : ContainerProtocol {
16+
@ThreadLocal
17+
public companion object {
18+
val Factory = DIContainer()
19+
}
20+
21+
val resolver = Resolver()
22+
23+
public fun <T : Any> register(clazz: KClass<T>, scope: ScopeType = ScopeType.Graph, fabric: () -> T?) {
24+
resolver.register(clazz,scope,fabric)
25+
}
26+
27+
public fun <T : Any> resolve(clazz: KClass<T>): T? {
28+
return resolver.resolve(clazz)
29+
}
30+
31+
public override fun <T : Any> resolve(type: String?): T? {
32+
if (type != null) {
33+
val clazz = createType<T>(type)
34+
if (clazz != null) {
35+
return resolver.resolve(clazz)
36+
}
37+
}
38+
return null
39+
}
40+
41+
public override fun <T : Any> register(type: String?, scope: ScopeType, fabric: () -> T?) {
42+
if (type != null) {
43+
val clazz = createType<T>(type)
44+
if (clazz != null) {
45+
resolver.register(clazz, scope, fabric)
46+
}
47+
}
48+
}
49+
}
50+
51+
public inline fun <reified T:Any> resolved(container: DIContainer = DIContainer.Factory) = container.resolve(T::class)
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.azharkova.di.init
22

3+
import com.azharkova.di.container.ContainerProtocol
34
import com.azharkova.di.container.DIContainer
45

5-
class ConfiguratorApp {
6-
val appDIContainer: DIContainer by lazy { DIContainer() }
6+
public class ConfiguratorApp {
7+
public val appDIContainer: DIContainer by lazy { DIContainer() }
78
}

src/commonMain/kotlin/com/azharkova/di/memory/reference/singleton/SingletonValue.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ class SingletonValue<T:Any>(private val clazz: KClass<T>,
2121
}
2222
}
2323

24-
inline fun <reified T:Any> single(container: DIContainer = DIContainer.Factory, noinline fabric: () -> T?) = CachedValue(T::class,container,fabric)
24+
public inline fun <reified T:Any> single(container: DIContainer = DIContainer.Factory, noinline fabric: () -> T?) = CachedValue(T::class,container,fabric)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.azharkova.di.util
2+
3+
import kotlin.reflect.KClass
4+
5+
public expect fun<T: Any> createType(type: String): KClass<T>?

src/iosMain/kotlin/com/azharkova/di/Threading.kt

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.azharkova.di.util
2+
3+
import com.azharkova.di.container.DIContainer
4+
import com.azharkova.di.scope.ScopeType
5+
import kotlinx.cinterop.ObjCClass
6+
import kotlinx.cinterop.getOriginalKotlinClass
7+
import kotlin.reflect.KClass
8+
9+
fun<T: Any> DIContainer.createType(type: ObjCClass): KClass<T>? {
10+
return getOriginalKotlinClass(type) as? KClass<T>
11+
}
12+
13+
fun<T: Any> DIContainer.register(type: ObjCClass, scope: ScopeType, fabric: () -> T?) {
14+
val clazz = createType<T>(type)
15+
if (clazz != null) {
16+
resolver.register(clazz,scope,fabric)
17+
}
18+
}
19+
20+
fun <T : Any> DIContainer.resolve(type: ObjCClass): T? {
21+
val clazz = createType<T>(type)
22+
if (clazz != null) {
23+
return resolver.resolve(clazz)
24+
}
25+
return null
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.azharkova.di.util
2+
3+
import platform.objc.objc_sync_enter
4+
import platform.objc.objc_sync_exit
5+
6+
7+
actual inline fun <R> trySynchronized(lock: Any?, block: () -> R): R {
8+
if (lock != null) {
9+
objc_sync_enter(lock)
10+
val result = block()
11+
objc_sync_exit(lock)
12+
return result
13+
} else {
14+
return block()
15+
}
16+
}

0 commit comments

Comments
 (0)