Improve WASM client reusability
This commit is contained in:
@@ -154,16 +154,4 @@ fun envValue(key: String): String? {
|
||||
}
|
||||
.firstOrNull { it.first == key }?.second
|
||||
}
|
||||
}
|
||||
|
||||
buildConfig {
|
||||
sourceSets.getByName("wasmJsMain") {
|
||||
val serverUrl = envValue("WASM_SERVER_URL") ?:
|
||||
throw GradleException("MISSING CONFIGURATION: WASM_SERVER_URL. Please set it as environment variable or `.env` variable.")
|
||||
|
||||
className("WasmConfig")
|
||||
packageName("com.jaytux.phoebench.clients")
|
||||
useKotlinOutput()
|
||||
buildConfigField("SERVER_URL", serverUrl)
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
package com.jaytux.phoebench.clients
|
||||
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.ui.ExperimentalComposeUiApi
|
||||
import androidx.compose.ui.window.ComposeViewport
|
||||
import kotlinx.browser.document
|
||||
|
||||
@OptIn(ExperimentalComposeUiApi::class)
|
||||
fun main() {
|
||||
suspend fun main() {
|
||||
config = loadConfig()
|
||||
ComposeViewport(document.body!!) {
|
||||
App()
|
||||
}
|
||||
|
||||
+16
-1
@@ -6,7 +6,10 @@ import com.jaytux.phoebench.clients.ui.constantServerSelect
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.engine.js.*
|
||||
import kotlinx.browser.window
|
||||
import kotlinx.coroutines.await
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.serializer
|
||||
import kotlin.uuid.Uuid
|
||||
@@ -56,7 +59,19 @@ private external fun jsFormat(num: Double): String
|
||||
|
||||
actual fun format(f: Float): String = jsFormat(f.toDouble())
|
||||
|
||||
@Serializable
|
||||
data class Config(val server: String)
|
||||
|
||||
@OptIn(ExperimentalWasmJsInterop::class)
|
||||
suspend fun loadConfig(): Config {
|
||||
val resp = window.fetch("config.json").await()
|
||||
val text = resp.text().await()
|
||||
return Json.decodeFromString<Config>(text.toString())
|
||||
}
|
||||
|
||||
lateinit var config: Config
|
||||
|
||||
@Composable
|
||||
actual fun ServerSelect(server: String, onUpdate: (String) -> Unit) {
|
||||
constantServerSelect(WasmConfig.SERVER_URL)(server, onUpdate)
|
||||
constantServerSelect(config.server)(server, onUpdate)
|
||||
}
|
||||
Reference in New Issue
Block a user