diff options
author | joffrey-bion <joffrey.bion@gmail.com> | 2021-02-10 14:34:59 +0100 |
---|---|---|
committer | joffrey-bion <joffrey.bion@gmail.com> | 2021-02-10 14:40:51 +0100 |
commit | bbe342ad219125512dccdbde3a03197285aa8b22 (patch) | |
tree | 455f4e0c1cbced173c8c5b6d97860552b4d1740c /sw-ui/src/main/kotlin | |
parent | Bring wonder to the foreground on hover (diff) | |
download | seven-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.kt | 24 |
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 + } +} |