Skip to content

Commit deec86e

Browse files
committed
PB-2036: fixing store / URL synchronisation
- Issue: when changing the state through actions, the URL would not be updated. - Cause: when migrating to Pinia, we no longer had a global watch on mutations, instead they are defined by store, and we only checked on the app Readyness store. - Fix : (ugly) we subscribe every store that is needed. - New Issue: apparently we also broke the legacy parser.
1 parent 982b0d4 commit deec86e

File tree

2 files changed

+91
-9
lines changed

2 files changed

+91
-9
lines changed

packages/viewer/src/router/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import EmbedView from '@/views/EmbedView.vue'
1616
import LegacyParamsView from '@/views/LegacyParamsView.vue'
1717
import MapView from '@/views/MapView.vue'
1818
import PrintView from '@/views/PrintView.vue'
19+
import storeSyncRouterPlugin from '@/router/storeSync'
1920

2021
const history = createWebHashHistory()
2122

@@ -90,6 +91,6 @@ if (IS_TESTING_WITH_CYPRESS) {
9091
appReadinessRouterPlugin(router)
9192
legacyPermalinkRouterPlugin(router)
9293
// WIP on it to make it work again, disabling it in the meantime
93-
// storeSyncRouterPlugin(router)
94+
storeSyncRouterPlugin(router)
9495

9596
export default router

packages/viewer/src/router/storeSync/storeSync.routerPlugin.ts

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Store } from 'pinia'
12
import type { LocationQuery, RouteLocationNormalizedGeneric, Router } from 'vue-router'
23

34
import log, { LogPreDefinedColor } from '@swissgeo/log'
@@ -14,7 +15,17 @@ import UrlParamConfig, {
1415
} from '@/router/storeSync/UrlParamConfig.class'
1516
import { MAP_VIEWS } from '@/router/viewNames'
1617
import useAppStore from '@/store/modules/app'
18+
import useCesiumStore from '@/store/modules/cesium'
19+
import useDebugStore from '@/store/modules/debug'
20+
import useGeolocationStore from '@/store/modules/geolocation'
21+
import useI18nStore from '@/store/modules/i18n'
22+
import useLayersStore from '@/store/modules/layers'
23+
import usePositionStore from '@/store/modules/position'
24+
import usePrintStore from '@/store/modules/print'
25+
import useSearchStore from '@/store/modules/search'
1726
import useShareStore from '@/store/modules/share'
27+
import useTopicsStore from '@/store/modules/topics'
28+
import useUIStore from '@/store/modules/ui'
1829

1930
export const FAKE_URL_CALLED_AFTER_ROUTE_CHANGE: string = '/tell-cypress-route-has-changed'
2031
const watchedActions: string[] = [
@@ -229,7 +240,6 @@ function urlQueryWatcher(
229240
})
230241
})
231242
}
232-
233243
if (requireQueryUpdate) {
234244
log.debug({
235245
title: 'Router store plugin / urlQueryWatcher',
@@ -266,7 +276,19 @@ function initialUrlQueryWatcher(to: RouteLocationNormalizedGeneric, router: Rout
266276
* parameter when on the MapView.
267277
*/
268278
const storeSyncRouterPlugin: RouterPlugin = (router): void => {
269-
let unsubscribeStoreMutation: () => void
279+
280+
let unsubscribeAppReadyMutation: () => void
281+
let unsubscribeCesiumMutation: () => void
282+
let unsubscribeDebugMutation: () => void
283+
let unsubscribeGeolocationMutation: () => void
284+
let unsubscribeI18nMutation: () => void
285+
let unsubscribeLayerMutation: () => void
286+
let unsubscribePositionMutation: () => void
287+
let unsubscribePrintMutation: () => void
288+
let unsubscribeSearchMutation: () => void
289+
let unsubscribeTopicMutation: () => void
290+
let unsubscribeUIMutation: () => void
291+
270292
router.beforeEach(
271293
(to: RouteLocationNormalizedGeneric, from: RouteLocationNormalizedGeneric) => {
272294
log.debug({
@@ -291,13 +313,36 @@ const storeSyncRouterPlugin: RouterPlugin = (router): void => {
291313
messages: [`leaving the map view`, from, to],
292314
})
293315
// leaving MapView make sure to unsubscribe the store mutation
294-
if (unsubscribeStoreMutation) {
316+
if (unsubscribeAppReadyMutation) {
295317
log.info({
296318
title: 'Router store plugin/beforeEach',
297319
titleColor: LogPreDefinedColor.Rose,
298320
messages: [`Leaving ${to.name}, unregister store mutation watcher`],
299321
})
300-
unsubscribeStoreMutation()
322+
unsubscribeAppReadyMutation()
323+
if (
324+
unsubscribePositionMutation &&
325+
unsubscribeDebugMutation &&
326+
unsubscribeTopicMutation &&
327+
unsubscribeLayerMutation &&
328+
unsubscribeSearchMutation &&
329+
unsubscribeUIMutation &&
330+
unsubscribeI18nMutation &&
331+
unsubscribeCesiumMutation &&
332+
unsubscribeGeolocationMutation &&
333+
unsubscribePrintMutation
334+
) {
335+
unsubscribePositionMutation()
336+
unsubscribeDebugMutation()
337+
unsubscribeTopicMutation()
338+
unsubscribeLayerMutation()
339+
unsubscribeSearchMutation()
340+
unsubscribeUIMutation()
341+
unsubscribeI18nMutation()
342+
unsubscribeCesiumMutation()
343+
unsubscribeGeolocationMutation()
344+
unsubscribePrintMutation()
345+
}
301346
retVal = undefined
302347
}
303348
} else if (appStore.isReady) {
@@ -342,10 +387,11 @@ const storeSyncRouterPlugin: RouterPlugin = (router): void => {
342387
// which was LEGACY. By moving this subscription to the after Each loop, we ensure the 'currentRoute'
343388
// is always set to MAPVIEW, avoiding a lock of the viewer.
344389
router.afterEach((to: RouteLocationNormalizedGeneric) => {
390+
345391
if (
346392
typeof to.name === 'string' &&
347393
MAP_VIEWS.includes(to.name) &&
348-
!unsubscribeStoreMutation
394+
!unsubscribeAppReadyMutation
349395
) {
350396
log.info({
351397
title: 'Router store plugin/afterEach',
@@ -354,20 +400,54 @@ const storeSyncRouterPlugin: RouterPlugin = (router): void => {
354400
})
355401
const appStore = useAppStore()
356402

357-
// listening to store mutation to update URL
358-
unsubscribeStoreMutation = appStore.$onAction(({ name, args }) => {
359-
if (name === 'setAppIsReady') {
403+
/**
404+
* We subscribe to every store we need to in order to sync the URL and the
405+
* state of the application.
406+
*
407+
*/
408+
function applyAllPersistentSubscriptions(): void {
409+
410+
function setSubscription(store: Store): () => void {
411+
return store.$onAction(({after, name, args}) => {
412+
after(() => storeMutationWatcher(name, args, router))
413+
})
414+
}
415+
416+
unsubscribeCesiumMutation = setSubscription(useCesiumStore())
417+
unsubscribeDebugMutation = setSubscription(useDebugStore())
418+
unsubscribeGeolocationMutation = setSubscription(useGeolocationStore())
419+
unsubscribeI18nMutation = setSubscription(useI18nStore())
420+
unsubscribeLayerMutation = setSubscription(useLayersStore())
421+
unsubscribePositionMutation = setSubscription(usePositionStore())
422+
unsubscribePrintMutation = setSubscription(usePrintStore())
423+
unsubscribeSearchMutation = setSubscription(useSearchStore())
424+
unsubscribeTopicMutation = setSubscription(useTopicsStore())
425+
unsubscribeUIMutation = setSubscription(useUIStore())
426+
}
427+
428+
// we are waiting for the app to be ready to start listening on the store changes for the URL sync
429+
unsubscribeAppReadyMutation = appStore.$onAction(({ after, name, args }) => {
430+
431+
after(() => {
432+
if (name === 'setAppIsReady') {
360433
// If the app was not yet ready after entering the map view, we need to
361434
// trigger the initial urlQuery watcher otherwise we have a blank application.
362435
log.info({
363436
title: 'Router store plugin/afterEach',
364437
titleColor: LogPreDefinedColor.Rose,
365438
messages: [`App is ready, trigger initial URL query watcher`],
366439
})
440+
applyAllPersistentSubscriptions()
367441
initialUrlQueryWatcher(to, router)
368442
} else if (appStore.isReady) {
443+
// here, we need to set up the store mutation watcher for all stores.
444+
445+
applyAllPersistentSubscriptions()
369446
storeMutationWatcher(name, args, router)
447+
370448
}
449+
})
450+
371451
})
372452

373453
if (appStore.isReady) {
@@ -382,6 +462,7 @@ const storeSyncRouterPlugin: RouterPlugin = (router): void => {
382462
`MapView entered, while app was already ready ! Trigger initial URL query watcher`,
383463
],
384464
})
465+
applyAllPersistentSubscriptions()
385466
initialUrlQueryWatcher(to, router)
386467
}
387468
}

0 commit comments

Comments
 (0)