This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c23797d
commit e36b740
Showing
9 changed files
with
343 additions
and
106 deletions.
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
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
94 changes: 0 additions & 94 deletions
94
kdi/src/commonMain/kotlin/ru/astrainteractive/klibs/kdi/WiredModule.kt
This file was deleted.
Oops, something went wrong.
84 changes: 84 additions & 0 deletions
84
kdi/src/commonMain/kotlin/ru/astrainteractive/klibs/kdi/wired/DefaultWiredModule.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,84 @@ | ||
package ru.astrainteractive.klibs.kdi.wired | ||
|
||
import co.touchlab.kermit.Logger | ||
import ru.astrainteractive.klibs.kdi.ExperimentalKDIApi | ||
import ru.astrainteractive.klibs.kdi.Factory | ||
import ru.astrainteractive.klibs.kdi.Lateinit | ||
import ru.astrainteractive.klibs.kdi.Provider | ||
import ru.astrainteractive.klibs.kdi.Reloadable | ||
import ru.astrainteractive.klibs.kdi.Single | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
* This is default implementation of [WiredModule] which can be used with delegation | ||
*/ | ||
@OptIn(ExperimentalKDIApi::class) | ||
@Suppress("TooManyFunctions") | ||
internal class DefaultWiredModule : WiredModule { | ||
|
||
private val wires = HashSet<Wire<*>>() | ||
private inline fun <T : Any> rememberWire(wire: Wire<T>) { | ||
if (wires.contains(wire)) error("Factory set already contains $this") | ||
Logger.i(tag = TAG) { "Remembered Factory $this" } | ||
wires.add(wire) | ||
} | ||
|
||
override fun <T : Any> Factory<T>.remember(clazz: KClass<T>): Factory<T> { | ||
Wire.Factory(clazz, this).run(::rememberWire) | ||
return this | ||
} | ||
|
||
override fun <T : Any> Provider<T>.remember(clazz: KClass<T>): Provider<T> { | ||
Wire.Provider(clazz, this).run(::rememberWire) | ||
return this | ||
} | ||
|
||
override fun <T : Any, K : Single<T>> K.remember(clazz: KClass<T>): K { | ||
Wire.Dependency.Single(clazz, this).run(::rememberWire) | ||
return this | ||
} | ||
|
||
override fun <T : Any, K : Lateinit<T>> K.remember(clazz: KClass<T>): K { | ||
Wire.Dependency.Lateinit(clazz, this).run(::rememberWire) | ||
return this | ||
} | ||
|
||
override fun <T : Any, K : Reloadable<T>> K.remember(clazz: KClass<T>): K { | ||
Wire.Dependency.Reloadable(clazz, this).run(::rememberWire) | ||
return this | ||
} | ||
|
||
override fun <T : Any> single(kClass: KClass<T>): Single<T> { | ||
return wires.filterIsInstance<Wire.Dependency.Single<T>>().first { | ||
it.kClass == kClass | ||
}.instance | ||
} | ||
|
||
override fun <T : Any> reloadable(kClass: KClass<T>): Reloadable<T> { | ||
return wires.filterIsInstance<Wire.Dependency.Reloadable<T>>().first { | ||
it.kClass == kClass | ||
}.instance | ||
} | ||
|
||
override fun <T : Any> lateinit(kClass: KClass<T>): Lateinit<T> { | ||
return wires.filterIsInstance<Wire.Dependency.Lateinit<T>>().first { | ||
it.kClass == kClass | ||
}.instance | ||
} | ||
|
||
override fun <T : Any> factory(kClass: KClass<T>): Factory<T> { | ||
return wires.filterIsInstance<Wire.Factory<T>>().first { | ||
it.kClass == kClass | ||
}.instance | ||
} | ||
|
||
override fun <T : Any> provider(kClass: KClass<T>): Provider<T> { | ||
return wires.filterIsInstance<Wire.Provider<T>>().first { | ||
it.kClass == kClass | ||
}.instance | ||
} | ||
|
||
companion object { | ||
private const val TAG = "WiredModule" | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
kdi/src/commonMain/kotlin/ru/astrainteractive/klibs/kdi/wired/Wire.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,35 @@ | ||
package ru.astrainteractive.klibs.kdi.wired | ||
|
||
import kotlin.reflect.KClass | ||
|
||
internal sealed class Wire<T : Any>(val kClass: KClass<T>) { | ||
class Factory<T : Any>( | ||
kClass: KClass<T>, | ||
val instance: ru.astrainteractive.klibs.kdi.Factory<T> | ||
) : Wire<T>(kClass) | ||
|
||
class Provider<T : Any>( | ||
kClass: KClass<T>, | ||
val instance: ru.astrainteractive.klibs.kdi.Provider<T> | ||
) : Wire<T>(kClass) | ||
|
||
sealed class Dependency<T : Any, K : ru.astrainteractive.klibs.kdi.Dependency<T>>( | ||
kClass: KClass<T>, | ||
val instance: K | ||
) : Wire<T>(kClass) { | ||
class Single<T : Any>( | ||
kClass: KClass<T>, | ||
instance: ru.astrainteractive.klibs.kdi.Single<T> | ||
) : Dependency<T, ru.astrainteractive.klibs.kdi.Single<T>>(kClass, instance) | ||
|
||
class Reloadable<T : Any>( | ||
kClass: KClass<T>, | ||
instance: ru.astrainteractive.klibs.kdi.Reloadable<T> | ||
) : Dependency<T, ru.astrainteractive.klibs.kdi.Reloadable<T>>(kClass, instance) | ||
|
||
class Lateinit<T : Any>( | ||
kClass: KClass<T>, | ||
instance: ru.astrainteractive.klibs.kdi.Lateinit<T> | ||
) : Dependency<T, ru.astrainteractive.klibs.kdi.Lateinit<T>>(kClass, instance) | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
kdi/src/commonMain/kotlin/ru/astrainteractive/klibs/kdi/wired/WiredExt.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,88 @@ | ||
package ru.astrainteractive.klibs.kdi.wired | ||
|
||
import ru.astrainteractive.klibs.kdi.ExperimentalKDIApi | ||
import ru.astrainteractive.klibs.kdi.Factory | ||
import ru.astrainteractive.klibs.kdi.Lateinit | ||
import ru.astrainteractive.klibs.kdi.Provider | ||
import ru.astrainteractive.klibs.kdi.Reloadable | ||
import ru.astrainteractive.klibs.kdi.Single | ||
|
||
/** | ||
* This will remember and return current factory instance | ||
*/ | ||
@OptIn(ExperimentalKDIApi::class) | ||
inline fun <reified T : Any> WiredModule.remember(instance: Factory<T>): Factory<T> { | ||
return instance.remember(T::class) | ||
} | ||
|
||
/** | ||
* This will remember and return current lateinit instance | ||
*/ | ||
@OptIn(ExperimentalKDIApi::class) | ||
inline fun <reified T : Any> WiredModule.remember(instance: Lateinit<T>): Lateinit<T> { | ||
return instance.remember(T::class) | ||
} | ||
|
||
/** | ||
* This will remember and return current provider instance | ||
*/ | ||
@OptIn(ExperimentalKDIApi::class) | ||
inline fun <reified T : Any> WiredModule.remember(instance: Provider<T>): Provider<T> { | ||
return instance.remember(T::class) | ||
} | ||
|
||
/** | ||
* This will remember and return current reloadable instance | ||
*/ | ||
@OptIn(ExperimentalKDIApi::class) | ||
inline fun <reified T : Any> WiredModule.remember(instance: Reloadable<T>): Reloadable<T> { | ||
return instance.remember(T::class) | ||
} | ||
|
||
/** | ||
* This will remember and return current single instance | ||
*/ | ||
@OptIn(ExperimentalKDIApi::class) | ||
inline fun <reified T : Any> WiredModule.remember(instance: Single<T>): Single<T> { | ||
return instance.remember(T::class) | ||
} | ||
|
||
/** | ||
* This will return factory or throws [IllegalStateException] if not found | ||
*/ | ||
@OptIn(ExperimentalKDIApi::class) | ||
inline fun <reified T : Any> WiredModule.factory(): Factory<T> { | ||
return factory(T::class) | ||
} | ||
|
||
/** | ||
* This will return reloadable or throws [IllegalStateException] if not found | ||
*/ | ||
@OptIn(ExperimentalKDIApi::class) | ||
inline fun <reified T : Any> WiredModule.lateinit(): Lateinit<T> { | ||
return lateinit(T::class) | ||
} | ||
|
||
/** | ||
* This will return provider or throws [IllegalStateException] if not found | ||
*/ | ||
@OptIn(ExperimentalKDIApi::class) | ||
inline fun <reified T : Any> WiredModule.provider(): Provider<T> { | ||
return provider(T::class) | ||
} | ||
|
||
/** | ||
* This will return reloadable or throws [IllegalStateException] if not found | ||
*/ | ||
@OptIn(ExperimentalKDIApi::class) | ||
inline fun <reified T : Any> WiredModule.reloadable(): Reloadable<T> { | ||
return reloadable(T::class) | ||
} | ||
|
||
/** | ||
* This will return single or throws [IllegalStateException] if not found | ||
*/ | ||
@OptIn(ExperimentalKDIApi::class) | ||
inline fun <reified T : Any> WiredModule.single(): Single<T> { | ||
return single(T::class) | ||
} |
Oops, something went wrong.