Remove unused code
authorTheSaminator <TheSaminator@users.noreply.github.com>
Tue, 24 May 2022 21:27:51 +0000 (17:27 -0400)
committerTheSaminator <TheSaminator@users.noreply.github.com>
Tue, 24 May 2022 21:27:51 +0000 (17:27 -0400)
src/commonMain/kotlin/starshipfights/game/util.kt
src/jsMain/kotlin/starshipfights/game/client_game.kt
src/jsMain/kotlin/starshipfights/game/popup.kt
src/jsMain/kotlin/starshipfights/game/util_js.kt

index 2e6869627796715e5a5234216b3a449caa0f2b61..07807e9c1e64a543ceeb01d7a5e640190eb25186 100644 (file)
@@ -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 : Enum<T>> T.toUrlSlug() = name.replace('_', '-').lowercase()
 
-inline fun <T> 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)
index a92e81f0c8ef39e26070ddcdbde2e35e47d3753c..2c2a61e3463f81b41a71fbaa42f0ed4e8b287c7a 100644 (file)
@@ -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()
                        }
                }
        }
index 9974c9de1bbe7b8ae81c7c0a12db708b2f73aa35..c6a6229a924eb0fb1742481ba028677c3ad13647 100644 (file)
@@ -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<out T> {
                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<out T> {
        }
        
        companion object {
+               private val popupMutex = Mutex()
+               
                private val popup by lazy {
                        document.getElementById("popup").unsafeCast<HTMLDivElement>()
                }
index 0e86707ecaaf6c4d2189a180a9f4c6b787aece9e..fa5eb968f11543ce82db70cc38c4fe43d4f14652 100644 (file)
@@ -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<Event> = 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)