From: TheSaminator Date: Tue, 24 May 2022 21:27:51 +0000 (-0400) Subject: Remove unused code X-Git-Url: https://gitweb.starshipfights.net/?a=commitdiff_plain;h=22bc59c26a8f0ebc5e54b1b4ede4affae4e39d45;p=starship-fights Remove unused code --- diff --git a/src/commonMain/kotlin/starshipfights/game/util.kt b/src/commonMain/kotlin/starshipfights/game/util.kt index 2e68696..07807e9 100644 --- a/src/commonMain/kotlin/starshipfights/game/util.kt +++ b/src/commonMain/kotlin/starshipfights/game/util.kt @@ -1,9 +1,5 @@ package starshipfights.game -import kotlinx.coroutines.currentCoroutineContext -import kotlinx.coroutines.delay -import kotlinx.coroutines.flow.flow -import kotlinx.coroutines.isActive import kotlinx.html.* import kotlinx.serialization.json.Json import kotlin.math.abs @@ -23,20 +19,6 @@ const val EPSILON = 0.00_001 fun > T.toUrlSlug() = name.replace('_', '-').lowercase() -inline fun pollFlow(intervalMs: Long = 50, crossinline poll: () -> T) = flow { - var prev = poll() - emit(prev) - - while (currentCoroutineContext().isActive) { - delay(intervalMs) - val curr = poll() - if (curr != prev) { - prev = curr - emit(prev) - } - } -} - fun Double.toPercent() = "${(this * 100).roundToInt()}%" fun smoothMinus1To1(x: Double, exponent: Double = 1.0) = x / (1 + abs(x).pow(exponent)).pow(1 / exponent) diff --git a/src/jsMain/kotlin/starshipfights/game/client_game.kt b/src/jsMain/kotlin/starshipfights/game/client_game.kt index a92e81f..2c2a61e 100644 --- a/src/jsMain/kotlin/starshipfights/game/client_game.kt +++ b/src/jsMain/kotlin/starshipfights/game/client_game.kt @@ -100,16 +100,22 @@ private suspend fun GameRenderInteraction.execute(scope: CoroutineScope) { } launch { + val doneDeploying = Job() + + launch { + doneDeploying.join() + + val pickContext = pickContextDeferred.await() + beginSelecting(pickContext) + handleSelections(pickContext) + } + gameState.collect { state -> GameRender.renderGameState(scene, state) GameUI.drawGameUI(state) if (state.phase != GamePhase.Deploy) - launch { - val pickContext = pickContextDeferred.await() - beginSelecting(pickContext) - handleSelections(pickContext) - } + doneDeploying.complete() } } } diff --git a/src/jsMain/kotlin/starshipfights/game/popup.kt b/src/jsMain/kotlin/starshipfights/game/popup.kt index 9974c9d..c6a6229 100644 --- a/src/jsMain/kotlin/starshipfights/game/popup.kt +++ b/src/jsMain/kotlin/starshipfights/game/popup.kt @@ -1,13 +1,12 @@ package starshipfights.game import kotlinx.browser.document -import kotlinx.coroutines.flow.collect -import kotlinx.coroutines.flow.takeWhile import kotlinx.coroutines.launch import kotlinx.coroutines.suspendCancellableCoroutine +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import kotlinx.dom.addClass import kotlinx.dom.clear -import kotlinx.dom.hasClass import kotlinx.dom.removeClass import kotlinx.html.* import kotlinx.html.dom.append @@ -22,12 +21,10 @@ sealed class Popup { consumer.render(context, callback) } - suspend fun display(): T { - pollFlow(100) { popup.hasClass("hide") }.takeWhile { !it }.collect() - - popupBox.clear() - - return suspendCancellableCoroutine { continuation -> + suspend fun display(): T = popupMutex.withLock { + suspendCancellableCoroutine { continuation -> + popupBox.clear() + popupBox.append { renderInto(this, continuation.context) { hide() @@ -44,6 +41,8 @@ sealed class Popup { } companion object { + private val popupMutex = Mutex() + private val popup by lazy { document.getElementById("popup").unsafeCast() } diff --git a/src/jsMain/kotlin/starshipfights/game/util_js.kt b/src/jsMain/kotlin/starshipfights/game/util_js.kt index 0e86707..fa5eb96 100644 --- a/src/jsMain/kotlin/starshipfights/game/util_js.kt +++ b/src/jsMain/kotlin/starshipfights/game/util_js.kt @@ -2,11 +2,9 @@ package starshipfights.game import io.ktor.http.cio.websocket.* import kotlinx.browser.window -import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.currentCoroutineContext import kotlinx.coroutines.flow.* import kotlinx.coroutines.isActive -import kotlinx.coroutines.launch import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.SerializationStrategy @@ -39,23 +37,6 @@ suspend fun EventTarget.awaitEvent(eventName: String, shouldPreventDefault: Bool addEventListener(eventName, listener) } -suspend fun EventTarget.eventFlow(eventName: String, shouldPreventDefault: Boolean = false): Flow = callbackFlow { - val listener = object : EventListener { - override fun handleEvent(event: Event) { - if (shouldPreventDefault) - event.preventDefault() - - launch { send(event) } - } - } - - addEventListener(eventName, listener) - - awaitClose { - removeEventListener(eventName, listener) - } -} - suspend fun awaitAnimationFrame(): Double = suspendCancellableCoroutine { continuation -> val handle = window.requestAnimationFrame { t -> continuation.resume(t)