fixes and improvements

This commit is contained in:
InsanusMokrassar 2022-12-15 15:07:10 +06:00
parent 85e5cee154
commit c584c24fce
2 changed files with 27 additions and 3 deletions

View File

@ -3,7 +3,7 @@ package dev.inmo.micro_utils.startup.launcher
import dev.inmo.kslog.common.i import dev.inmo.kslog.common.i
import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonObject
import org.koin.core.KoinApplication import org.koin.core.KoinApplication
import org.koin.core.context.GlobalContext import org.koin.core.context.startKoin
import org.koin.dsl.module import org.koin.dsl.module
/** /**
@ -23,7 +23,7 @@ suspend fun start(rawConfig: JsonObject) {
} }
) )
logger.i("Modules loaded") logger.i("Modules loaded")
GlobalContext.startKoin(koinApp) startKoin(koinApp)
logger.i("Koin started") logger.i("Koin started")
startPlugin(koinApp.koin) startPlugin(koinApp.koin)
logger.i("App has been setup") logger.i("App has been setup")

View File

@ -1,5 +1,8 @@
package dev.inmo.micro_utils.startup.plugin package dev.inmo.micro_utils.startup.plugin
import com.benasher44.uuid.uuid4
import kotlin.reflect.KClass
/** /**
* Creates [T] using [block], register it in [StartPluginSerializer] using its [StartPluginSerializer.registerPlugin] * Creates [T] using [block], register it in [StartPluginSerializer] using its [StartPluginSerializer.registerPlugin]
* and returns created by [block] plugin * and returns created by [block] plugin
@ -7,8 +10,29 @@ package dev.inmo.micro_utils.startup.plugin
* @param name Will be used as a key for registration in [StartPluginSerializer] and will be passed to the [block] as * @param name Will be used as a key for registration in [StartPluginSerializer] and will be passed to the [block] as
* parameter * parameter
*/ */
inline fun <T : StartPlugin> createStartupPluginAndRegister(name: String, block: (String) -> T): T { inline fun <T : StartPlugin> createStartupPluginAndRegister(
name: String = uuid4().toString(),
block: (String) -> T
): T {
val plugin = block(name) val plugin = block(name)
StartPluginSerializer.registerPlugin(name, plugin) StartPluginSerializer.registerPlugin(name, plugin)
return plugin return plugin
} }
/**
* Creates [T] using [block], register it in [StartPluginSerializer] using its [StartPluginSerializer.registerPlugin]
* and returns created by [block] plugin
*/
inline fun <T : StartPlugin> createStartupPluginAndRegister(
kClass: KClass<T>,
name: String = uuid4().toString(),
block: (String) -> T
): T = createStartupPluginAndRegister("${kClass.simpleName}_$name", block)
/**
* Creates [T] using [block], register it in [StartPluginSerializer] using its [StartPluginSerializer.registerPlugin]
* and returns created by [block] plugin
*/
inline fun <reified T : StartPlugin> createStartupPluginAndRegister(
block: (String) -> T
): T = createStartupPluginAndRegister(T::class, uuid4().toString(), block)