2021-11-24 07:52:27 +00:00
|
|
|
|
package dev.inmo.postssystem.client
|
|
|
|
|
|
|
|
|
|
import dev.inmo.postssystem.client.fsm.ui.*
|
|
|
|
|
import dev.inmo.postssystem.client.ui.*
|
|
|
|
|
import dev.inmo.postssystem.client.ui.fsm.*
|
|
|
|
|
import dev.inmo.postssystem.client.ui.fsm.UIFSMStateSerializer
|
|
|
|
|
import dev.inmo.postssystem.features.auth.client.ui.AuthUIModel
|
|
|
|
|
import dev.inmo.postssystem.features.auth.client.ui.AuthUIViewModel
|
|
|
|
|
import dev.inmo.postssystem.features.auth.common.AuthTokenInfo
|
|
|
|
|
import dev.inmo.micro_utils.coroutines.ContextSafelyExceptionHandler
|
2022-01-07 14:06:40 +00:00
|
|
|
|
import dev.inmo.micro_utils.fsm.common.CheckableHandlerHolder
|
|
|
|
|
import dev.inmo.micro_utils.fsm.common.StatesMachine
|
2021-11-24 07:52:27 +00:00
|
|
|
|
import dev.inmo.micro_utils.repos.mappers.withMapper
|
|
|
|
|
import dev.inmo.micro_utils.serialization.typed_serializer.TypedSerializer
|
2022-01-07 14:06:40 +00:00
|
|
|
|
import dev.inmo.postssystem.client.settings.auth.AuthSettings
|
2022-03-13 15:58:10 +00:00
|
|
|
|
import dev.inmo.postssystem.features.common.common.getAllDistinct
|
2022-03-10 15:35:32 +00:00
|
|
|
|
import dev.inmo.postssystem.services.posts.client.ui.create.*
|
|
|
|
|
import kotlinx.browser.*
|
2021-11-24 07:52:27 +00:00
|
|
|
|
import kotlinx.coroutines.*
|
|
|
|
|
import kotlinx.serialization.builtins.serializer
|
|
|
|
|
import kotlinx.serialization.serializer
|
|
|
|
|
import org.koin.core.Koin
|
|
|
|
|
import org.koin.core.context.loadKoinModules
|
2022-01-07 14:06:40 +00:00
|
|
|
|
import org.koin.core.parameter.ParametersHolder
|
|
|
|
|
import org.koin.core.qualifier.Qualifier
|
2021-11-24 07:52:27 +00:00
|
|
|
|
import org.koin.dsl.module
|
|
|
|
|
import org.w3c.dom.HTMLElement
|
2022-01-07 14:06:40 +00:00
|
|
|
|
import kotlin.reflect.KClass
|
2021-11-24 07:52:27 +00:00
|
|
|
|
|
|
|
|
|
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(),
|
|
|
|
|
"UIFSMState" to UIFSMStateSerializer
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
{
|
2022-03-10 15:35:32 +00:00
|
|
|
|
JSUIFSMStatesRepo(window.history)
|
2021-11-24 07:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
) {
|
|
|
|
|
first.apply {
|
|
|
|
|
second.apply {
|
|
|
|
|
loadKoinModules(
|
|
|
|
|
module {
|
|
|
|
|
factory { document.getElementById("main") as HTMLElement }
|
|
|
|
|
|
|
|
|
|
factory<AuthUIModel> { DefaultAuthUIModel(get(), get()) }
|
|
|
|
|
factory { AuthUIViewModel(get()) }
|
|
|
|
|
factory { AuthView(get(), get(UIScopeQualifier)) }
|
2022-03-10 15:35:32 +00:00
|
|
|
|
|
2022-03-11 16:26:06 +00:00
|
|
|
|
factory<PostCreateUIModel> { DefaultPostCreateUIModel(get(), get()) }
|
2022-03-10 15:35:32 +00:00
|
|
|
|
factory { PostCreateUIViewModel(get()) }
|
2022-03-13 15:58:10 +00:00
|
|
|
|
factory { PostCreateView(get(), getAllDistinct(), get(UIScopeQualifier)) }
|
2021-11-24 07:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
strictlyOn<AuthUIFSMState>(get<AuthView>())
|
2022-01-07 14:06:40 +00:00
|
|
|
|
|
|
|
|
|
// Костыль, в JS на момент Пн дек 6 14:19:29 +06 2021 если использовать strictlyOn генерируются
|
|
|
|
|
// некорректные безымянные классы (у них отсутствует метод handleState)
|
|
|
|
|
class DefaultStateHandlerWrapper<T : UIFSMState>(
|
|
|
|
|
private val klass: KClass<out UIFSMHandler<T>>,
|
|
|
|
|
private val stateKlass: KClass<T>,
|
|
|
|
|
private val qualifier: Qualifier? = null,
|
|
|
|
|
private val parameters: ((T) -> ParametersHolder)? = null
|
|
|
|
|
) : CheckableHandlerHolder<UIFSMState, UIFSMState> {
|
|
|
|
|
override suspend fun StatesMachine<in UIFSMState>.handleState(state: UIFSMState): UIFSMState? {
|
|
|
|
|
@Suppress("UNCHECKED_CAST", "NAME_SHADOWING")
|
|
|
|
|
val state = state as T
|
|
|
|
|
return runCatching {
|
|
|
|
|
val authSettings = get<AuthSettings>()
|
|
|
|
|
authSettings.loadingJob.join()
|
|
|
|
|
if (authSettings.authorizedDIModule.value == null) {
|
|
|
|
|
error("Can't perform state $state: Auth module was not initialized")
|
|
|
|
|
} else {
|
|
|
|
|
get<UIFSMHandler<T>>(klass, qualifier, parameters ?.let { { it(state) } }).run {
|
|
|
|
|
handleState(state)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}.getOrElse { e ->
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
AuthUIFSMState(state)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override suspend fun checkHandleable(state: UIFSMState): Boolean = stateKlass.isInstance(state)
|
|
|
|
|
}
|
|
|
|
|
inline fun <reified T : UIFSMState> registerHandler(
|
|
|
|
|
klass: KClass<out UIFSMHandler<T>>,
|
|
|
|
|
qualifier: Qualifier? = null,
|
|
|
|
|
parameters: ((T) -> ParametersHolder)? = null
|
|
|
|
|
) = add(
|
|
|
|
|
DefaultStateHandlerWrapper<T>(
|
|
|
|
|
klass,
|
|
|
|
|
T::class,
|
|
|
|
|
qualifier,
|
|
|
|
|
parameters
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2022-03-10 15:35:32 +00:00
|
|
|
|
registerHandler(PostCreateView::class)
|
2021-11-24 07:52:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|