From: TheSaminator Date: Mon, 14 Feb 2022 17:27:43 +0000 (-0500) Subject: Optimize coroutineScope calls X-Git-Url: https://gitweb.starshipfights.net/?a=commitdiff_plain;h=751cd20efdb41e3ec223e5fc129e4ae4ea24ffe8;p=starship-fights Optimize coroutineScope calls --- diff --git a/src/jvmMain/kotlin/starshipfights/auth/providers.kt b/src/jvmMain/kotlin/starshipfights/auth/providers.kt index 6b8b450..cab29d6 100644 --- a/src/jvmMain/kotlin/starshipfights/auth/providers.kt +++ b/src/jvmMain/kotlin/starshipfights/auth/providers.kt @@ -13,6 +13,7 @@ import io.ktor.response.* import io.ktor.routing.* import io.ktor.sessions.* import io.ktor.util.* +import kotlinx.coroutines.async import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.toList import kotlinx.coroutines.launch @@ -165,7 +166,9 @@ interface AuthProvider { val shipId = call.parameters["ship"]?.let { Id(it) }!! val (admiral, ship) = coroutineScope { - Admiral.get(admiralId)!! to ShipInDrydock.get(shipId)!! + val admiral = async { Admiral.get(admiralId)!! } + val ship = async { ShipInDrydock.get(shipId)!! } + admiral.await() to ship.await() } if (admiral.owningUser != currentUser) throw ForbiddenException() @@ -187,7 +190,9 @@ interface AuthProvider { val shipId = call.parameters["ship"]?.let { Id(it) }!! val (admiral, ship) = coroutineScope { - Admiral.get(admiralId)!! to ShipInDrydock.get(shipId)!! + val admiral = async { Admiral.get(admiralId)!! } + val ship = async { ShipInDrydock.get(shipId)!! } + admiral.await() to ship.await() } if (admiral.owningUser != currentUser) throw ForbiddenException() diff --git a/src/jvmMain/kotlin/starshipfights/info/views_user.kt b/src/jvmMain/kotlin/starshipfights/info/views_user.kt index 043ec17..5faf523 100644 --- a/src/jvmMain/kotlin/starshipfights/info/views_user.kt +++ b/src/jvmMain/kotlin/starshipfights/info/views_user.kt @@ -678,7 +678,9 @@ suspend fun ApplicationCall.renameShipPage(): HTML.() -> Unit { val shipId = parameters["ship"]?.let { Id(it) }!! val (admiral, ship) = coroutineScope { - Admiral.get(admiralId)!! to ShipInDrydock.get(shipId)!! + val admiral = async { Admiral.get(admiralId)!! } + val ship = async { ShipInDrydock.get(shipId)!! } + admiral.await() to ship.await() } if (admiral.owningUser != currentUser) throw ForbiddenException() @@ -724,7 +726,9 @@ suspend fun ApplicationCall.sellShipConfirmPage(): HTML.() -> Unit { val shipId = parameters["ship"]?.let { Id(it) }!! val (admiral, ship) = coroutineScope { - Admiral.get(admiralId)!! to ShipInDrydock.get(shipId)!! + val admiral = async { Admiral.get(admiralId)!! } + val ship = async { ShipInDrydock.get(shipId)!! } + admiral.await() to ship.await() } if (admiral.owningUser != currentUser) throw ForbiddenException()