summaryrefslogtreecommitdiff
path: root/sw-ui/src/main/kotlin
diff options
context:
space:
mode:
authorjoffrey-bion <joffrey.bion@gmail.com>2021-02-10 14:34:59 +0100
committerjoffrey-bion <joffrey.bion@gmail.com>2021-02-10 14:40:51 +0100
commitbbe342ad219125512dccdbde3a03197285aa8b22 (patch)
tree455f4e0c1cbced173c8c5b6d97860552b4d1740c /sw-ui/src/main/kotlin
parentBring wonder to the foreground on hover (diff)
downloadseven-wonders-bbe342ad219125512dccdbde3a03197285aa8b22.tar.gz
seven-wonders-bbe342ad219125512dccdbde3a03197285aa8b22.tar.bz2
seven-wonders-bbe342ad219125512dccdbde3a03197285aa8b22.zip
Properly start/stop sagas when using browser navigation
Resolves: https://github.com/joffrey-bion/seven-wonders/issues/116 Related: https://github.com/joffrey-bion/seven-wonders/issues/114 Related: https://github.com/joffrey-bion/seven-wonders/issues/60
Diffstat (limited to 'sw-ui/src/main/kotlin')
-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