Refactor map viewer
authorLanius Trolling <lanius@laniustrolling.dev>
Sat, 27 Jan 2024 14:15:44 +0000 (09:15 -0500)
committerLanius Trolling <lanius@laniustrolling.dev>
Sat, 27 Jan 2024 14:17:49 +0000 (09:17 -0500)
src/mapMain/kotlin/info/mechyrdia/mapviewer/entryPoint.kt
src/mapMain/kotlin/info/mechyrdia/mapviewer/history.kt
src/mapMain/kotlin/info/mechyrdia/mapviewer/modal.kt
src/mapMain/kotlin/info/mechyrdia/mapviewer/search.kt

index f115749f1e9acd59e4481df4fd3b0fd615a74e7e..a06770c81386fc20bc65c3d59c550aa001f01fa2 100644 (file)
@@ -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
index 6575c56ed0f5f415e1efb57a7073664fa90d955f..862c011317327c71bcd63d85d66155bb31ea1d37 100644 (file)
@@ -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)
index ae09591337290a82627278d8574790f66d17de1a..e5424d43a1519dd8560d312aefb27e5777695697 100644 (file)
@@ -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 <T> showModalBox(boxBuilder: TagConsumer<*>.((T) -> Unit) ->
        }
 }
 
-suspend fun <T> showLoadingScreen(label: String = "Loading...", loader: suspend () -> T): T {
+suspend fun <T> showLoadingScreen(label: String = "Loading...", loader: suspend CoroutineScope.() -> T): T {
        return coroutineScope {
                val showingBox = launch {
                        showModalBox { _ ->
index fa44186c821fcb0f593a2edda77314d30915f5e3..001bc9688d9b5ec65c882ed406eae9f340c448de 100644 (file)
@@ -12,7 +12,7 @@ const val MIN_QUERY_LENGTH = 3
 const val MAX_QUERY_RESULTS = 10
 
 private fun getMatches(name: String, query: String): List<IntRange> {
-       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<SearchResult> {