From 2a892d17cabf357c946c2dd87e790925d8cdcf4d Mon Sep 17 00:00:00 2001 From: Lanius Trolling Date: Sat, 27 Jan 2024 09:15:44 -0500 Subject: [PATCH] Refactor map viewer --- .../info/mechyrdia/mapviewer/entryPoint.kt | 38 ++++++++----------- .../info/mechyrdia/mapviewer/history.kt | 10 ++--- .../kotlin/info/mechyrdia/mapviewer/modal.kt | 7 +--- .../kotlin/info/mechyrdia/mapviewer/search.kt | 2 +- 4 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/mapMain/kotlin/info/mechyrdia/mapviewer/entryPoint.kt b/src/mapMain/kotlin/info/mechyrdia/mapviewer/entryPoint.kt index f115749..a06770c 100644 --- a/src/mapMain/kotlin/info/mechyrdia/mapviewer/entryPoint.kt +++ b/src/mapMain/kotlin/info/mechyrdia/mapviewer/entryPoint.kt @@ -1,35 +1,29 @@ package info.mechyrdia.mapviewer -import kotlinx.coroutines.* - -val AppScope = MainScope() + CoroutineExceptionHandler { ctx, ex -> - val coroName = ctx[CoroutineName]?.name?.let { "coroutine $it" } ?: "unnamed coroutine" - console.warn("Unhandled exception in $coroName", ex) - ex.printStackTrace() -} +import kotlinx.coroutines.async +import kotlinx.coroutines.launch +import kotlinx.coroutines.supervisorScope lateinit var galaxyMap: GalaxyMap lateinit var galaxyLore: GalaxyLore -fun main() { - AppScope.launch { +suspend fun main() { + supervisorScope { val ptrProvider = initPopHistoryEntryHandler() val (map, lore) = showLoadingScreen { - coroutineScope { - launch { loadFlags() } - launch { loadUiImages() } - launch { loadCelestialBodyFactories() } - launch { loadSpaceboxes() } - - val loreJson = async { loadJson("/assets/map/lore.json", GalaxyLore.serializer()) } - loadJson("/assets/map/map.json", GalaxyMap.serializer()).also { - launch { - loadGalaxyBgTextures(it.background) - } - } to loreJson.await() - } + launch { loadFlags() } + launch { loadUiImages() } + launch { loadCelestialBodyFactories() } + launch { loadSpaceboxes() } + + val loreJson = async { loadJson("/assets/map/lore.json", GalaxyLore.serializer()) } + loadJson("/assets/map/map.json", GalaxyMap.serializer()).also { + launch { + loadGalaxyBgTextures(it.background) + } + } to loreJson.await() } galaxyMap = map diff --git a/src/mapMain/kotlin/info/mechyrdia/mapviewer/history.kt b/src/mapMain/kotlin/info/mechyrdia/mapviewer/history.kt index 6575c56..862c011 100644 --- a/src/mapMain/kotlin/info/mechyrdia/mapviewer/history.kt +++ b/src/mapMain/kotlin/info/mechyrdia/mapviewer/history.kt @@ -13,10 +13,10 @@ private class MapPtrHolder { lateinit var ptr: MapObjectPtr } -private var isHandlingPopState = true +private var shouldNotPushHistoryEntry = true fun doneInitialRender() { - isHandlingPopState = false + shouldNotPushHistoryEntry = false } fun URLSearchParams.toMapObjectPtr(): MapObjectPtr { @@ -55,9 +55,9 @@ fun CoroutineScope.initPopHistoryEntryHandler(): () -> MapObjectPtr { val statePtr = JsonCodec.decodeFromDynamic(MapObjectPtr.serializer(), ev.state) if (isRenderActive) { - isHandlingPopState = true + shouldNotPushHistoryEntry = true renderMap(statePtr) - isHandlingPopState = false + shouldNotPushHistoryEntry = false } else ptrHolder.ptr = statePtr }) @@ -71,7 +71,7 @@ fun CoroutineScope.initPopHistoryEntryHandler(): () -> MapObjectPtr { @OptIn(ExperimentalSerializationApi::class) fun pushHistoryEntry(ptr: MapObjectPtr) { - if (isHandlingPopState) + if (shouldNotPushHistoryEntry) return val state = JsonCodec.encodeToDynamic(MapObjectPtr.serializer(), ptr) diff --git a/src/mapMain/kotlin/info/mechyrdia/mapviewer/modal.kt b/src/mapMain/kotlin/info/mechyrdia/mapviewer/modal.kt index ae09591..e5424d4 100644 --- a/src/mapMain/kotlin/info/mechyrdia/mapviewer/modal.kt +++ b/src/mapMain/kotlin/info/mechyrdia/mapviewer/modal.kt @@ -1,10 +1,7 @@ package info.mechyrdia.mapviewer import kotlinx.browser.document -import kotlinx.coroutines.cancelAndJoin -import kotlinx.coroutines.coroutineScope -import kotlinx.coroutines.launch -import kotlinx.coroutines.suspendCancellableCoroutine +import kotlinx.coroutines.* import kotlinx.dom.clear import kotlinx.html.* import kotlinx.html.dom.append @@ -32,7 +29,7 @@ private suspend fun showModalBox(boxBuilder: TagConsumer<*>.((T) -> Unit) -> } } -suspend fun showLoadingScreen(label: String = "Loading...", loader: suspend () -> T): T { +suspend fun showLoadingScreen(label: String = "Loading...", loader: suspend CoroutineScope.() -> T): T { return coroutineScope { val showingBox = launch { showModalBox { _ -> diff --git a/src/mapMain/kotlin/info/mechyrdia/mapviewer/search.kt b/src/mapMain/kotlin/info/mechyrdia/mapviewer/search.kt index fa44186..001bc96 100644 --- a/src/mapMain/kotlin/info/mechyrdia/mapviewer/search.kt +++ b/src/mapMain/kotlin/info/mechyrdia/mapviewer/search.kt @@ -12,7 +12,7 @@ const val MIN_QUERY_LENGTH = 3 const val MAX_QUERY_RESULTS = 10 private fun getMatches(name: String, query: String): List { - return Regex.fromLiteral(query).findAll(name).map { it.range }.toList() + return Regex.fromLiteral(query).findAll(name).map { it.range }.toList().sortedBy { it.first } } suspend fun searchGalaxy(query: String): List { -- 2.25.1