summaryrefslogtreecommitdiff
path: root/sw-ui/src
diff options
context:
space:
mode:
Diffstat (limited to 'sw-ui/src')
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/router/Router.kt24
1 files changed, 20 insertions, 4 deletions
diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/router/Router.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/router/Router.kt
index b5dcb978..00f8cda5 100644
--- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/router/Router.kt
+++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/router/Router.kt
@@ -11,7 +11,13 @@ enum class Route(val path: String) {
HOME("/"),
GAME_BROWSER("/games"),
LOBBY("/lobby"),
- GAME("/game"),
+ GAME("/game");
+
+ companion object {
+ private val all = values().associateBy { it.path }
+
+ fun from(path: String) = all.getValue(path)
+ }
}
data class Navigate(val route: Route) : RAction
@@ -22,11 +28,21 @@ suspend fun SwSagaContext.routerSaga(
) {
coroutineScope {
window.location.hash = startRoute.path
+ launch { changeRouteOnNavigateAction() }
var currentSaga: Job = launch { runRouteSaga(startRoute) }
- onEach<Navigate> {
+ window.onhashchange = { event ->
+ val route = Route.from(event.newURL.substringAfter("#"))
currentSaga.cancel()
- window.location.hash = it.route.path
- currentSaga = launch { runRouteSaga(it.route) }
+ currentSaga = this@coroutineScope.launch {
+ runRouteSaga(route)
+ }
+ Unit
}
}
}
+
+suspend fun SwSagaContext.changeRouteOnNavigateAction() {
+ onEach<Navigate> {
+ window.location.hash = it.route.path
+ }
+}
bgstack15