96 lines
3.2 KiB
Kotlin
96 lines
3.2 KiB
Kotlin
package dev.inmo.postssystem.client
|
|
|
|
import dev.inmo.postssystem.features.auth.common.AuthTokenInfo
|
|
import dev.inmo.micro_utils.coroutines.ContextSafelyExceptionHandler
|
|
import dev.inmo.micro_utils.repos.mappers.withMapper
|
|
import dev.inmo.micro_utils.serialization.typed_serializer.TypedSerializer
|
|
import dev.inmo.postssystem.features.auth.client.ui.*
|
|
import dev.inmo.postssystem.features.common.common.baseKoin
|
|
import dev.inmo.postssystem.features.common.common.getAllDistinct
|
|
import dev.inmo.postssystem.features.common.common.ui.fsm.UIFSMHandler
|
|
import dev.inmo.postssystem.services.posts.client.ui.list.PostsListUIFSMState
|
|
import dev.inmo.postssystem.services.posts.client.ui.list.PostsListUIState
|
|
import kotlinx.browser.*
|
|
import kotlinx.coroutines.*
|
|
import kotlinx.serialization.builtins.serializer
|
|
import kotlinx.serialization.json.Json
|
|
import kotlinx.serialization.serializer
|
|
import org.koin.core.Koin
|
|
import org.koin.core.context.loadKoinModules
|
|
import org.koin.dsl.module
|
|
import org.w3c.dom.HTMLElement
|
|
|
|
val defaultTypedSerializer = TypedSerializer<Any>(
|
|
"AuthTokenInfo" to AuthTokenInfo.serializer(),
|
|
"String" to String.serializer(),
|
|
"Int" to Int.serializer(),
|
|
"Long" to Long.serializer(),
|
|
"Short" to Short.serializer(),
|
|
"Byte" to Byte.serializer(),
|
|
"Float" to Float.serializer(),
|
|
"Double" to Double.serializer()
|
|
)
|
|
val defaultSerialFormat = Json {
|
|
ignoreUnknownKeys = true
|
|
}
|
|
|
|
fun baseKoin(): Koin {
|
|
val anyToString: suspend Any.() -> String = {
|
|
defaultSerialFormat.encodeToString(
|
|
defaultTypedSerializer,
|
|
this
|
|
)
|
|
}
|
|
return baseKoin(
|
|
CoroutineScope(
|
|
Dispatchers.Default +
|
|
ContextSafelyExceptionHandler { it.printStackTrace() } +
|
|
CoroutineExceptionHandler { _, it -> it.printStackTrace() }
|
|
),
|
|
{
|
|
CookiesKeyValueRepo.withMapper<String, Any, String, String>(
|
|
{ this },
|
|
{
|
|
runCatching {
|
|
anyToString()
|
|
}.getOrElse {
|
|
if (it is NoSuchElementException) {
|
|
val name = this::class.simpleName!!
|
|
defaultTypedSerializer.include(name, serializer())
|
|
anyToString()
|
|
} else {
|
|
throw it
|
|
}
|
|
}
|
|
},
|
|
{ this },
|
|
{
|
|
defaultSerialFormat.decodeFromString(
|
|
defaultTypedSerializer,
|
|
this
|
|
)
|
|
}
|
|
)
|
|
},
|
|
{
|
|
JSUIFSMStatesRepo(window.history, AuthUIFSMState(PostsListUIFSMState()), getAllDistinct())
|
|
}
|
|
) {
|
|
val scope = first
|
|
first.apply {
|
|
second.apply {
|
|
loadKoinModules(
|
|
module {
|
|
factory { document.getElementById("main") as HTMLElement }
|
|
}
|
|
)
|
|
getAllDistinct<UIFSMHandler.Registrator>().forEach {
|
|
with(it) {
|
|
include()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|