summaryrefslogtreecommitdiff
path: root/sw-ui-kt/src
diff options
context:
space:
mode:
Diffstat (limited to 'sw-ui-kt/src')
-rw-r--r--sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/utils/CoroutinesUtils.kt6
-rw-r--r--sw-ui-kt/src/test/kotlin/org/luxons/sevenwonders/ui/utils/CoroutineUtilsTest.kt24
2 files changed, 26 insertions, 4 deletions
diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/utils/CoroutinesUtils.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/utils/CoroutinesUtils.kt
index 55f8e0f6..600f08d3 100644
--- a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/utils/CoroutinesUtils.kt
+++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/utils/CoroutinesUtils.kt
@@ -4,10 +4,8 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.selects.select
-suspend inline fun <R> awaitFirst(
- crossinline f1: suspend () -> R,
- crossinline f2: suspend () -> R
-): R = coroutineScope {
+// Cannot inline or it crashes for some reason
+suspend fun <R> awaitFirst(f1: suspend () -> R, f2: suspend () -> R): R = coroutineScope {
val deferred1 = async { f1() }
val deferred2 = async { f2() }
select<R> {
diff --git a/sw-ui-kt/src/test/kotlin/org/luxons/sevenwonders/ui/utils/CoroutineUtilsTest.kt b/sw-ui-kt/src/test/kotlin/org/luxons/sevenwonders/ui/utils/CoroutineUtilsTest.kt
new file mode 100644
index 00000000..d633f6f2
--- /dev/null
+++ b/sw-ui-kt/src/test/kotlin/org/luxons/sevenwonders/ui/utils/CoroutineUtilsTest.kt
@@ -0,0 +1,24 @@
+package org.luxons.sevenwonders.ui.utils
+
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.promise
+import kotlin.test.Test
+import kotlin.test.assertEquals
+
+class CoroutineUtilsTest {
+
+ @Test
+ fun awaitFirstTest(): dynamic = GlobalScope.promise {
+ val s = awaitFirst(
+ { delay(100); "1" },
+ { delay(200); "2" }
+ )
+ assertEquals("1", s)
+ val s2 = awaitFirst(
+ { delay(150); "1" },
+ { delay(50); "2" }
+ )
+ assertEquals("2", s2)
+ }
+}
bgstack15