Skip to content

Commit 8ae2501

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 8ae2501

File tree

2 files changed

+94
-7
lines changed

2 files changed

+94
-7
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: 92 additions & 6 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[] = [
@@ -155,6 +166,8 @@ function urlQueryWatcher(
155166
// to avoid changing the store value for no reason.
156167
!(queryValue === undefined && storeValue === undefined)
157168
) {
169+
console.error('WE ARE OBVIOUSLY GOING IN THERE query : ', queryValue)
170+
console.error('WE ARE OBVIOUSLY GOING IN THERE store : ', storeValue)
158171
// dispatching URL value to the store
159172
log.debug({
160173
title: 'Router store plugin / urlQueryWatcher',
@@ -229,7 +242,6 @@ function urlQueryWatcher(
229242
})
230243
})
231244
}
232-
233245
if (requireQueryUpdate) {
234246
log.debug({
235247
title: 'Router store plugin / urlQueryWatcher',
@@ -266,7 +278,19 @@ function initialUrlQueryWatcher(to: RouteLocationNormalizedGeneric, router: Rout
266278
* parameter when on the MapView.
267279
*/
268280
const storeSyncRouterPlugin: RouterPlugin = (router): void => {
269-
let unsubscribeStoreMutation: () => void
281+
282+
let unsubscribeAppReadyMutation: () => void
283+
let unsubscribeCesiumMutation: () => void
284+
let unsubscribeDebugMutation: () => void
285+
let unsubscribeGeolocationMutation: () => void
286+
let unsubscribeI18nMutation: () => void
287+
let unsubscribeLayerMutation: () => void
288+
let unsubscribePositionMutation: () => void
289+
let unsubscribePrintMutation: () => void
290+
let unsubscribeSearchMutation: () => void
291+
let unsubscribeTopicMutation: () => void
292+
let unsubscribeUIMutation: () => void
293+
270294
router.beforeEach(
271295
(to: RouteLocationNormalizedGeneric, from: RouteLocationNormalizedGeneric) => {
272296
log.debug({
@@ -291,13 +315,36 @@ const storeSyncRouterPlugin: RouterPlugin = (router): void => {
291315
messages: [`leaving the map view`, from, to],
292316
})
293317
// leaving MapView make sure to unsubscribe the store mutation
294-
if (unsubscribeStoreMutation) {
318+
if (unsubscribeAppReadyMutation) {
295319
log.info({
296320
title: 'Router store plugin/beforeEach',
297321
titleColor: LogPreDefinedColor.Rose,
298322
messages: [`Leaving ${to.name}, unregister store mutation watcher`],
299323
})
300-
unsubscribeStoreMutation()
324+
unsubscribeAppReadyMutation()
325+
if (
326+
unsubscribePositionMutation &&
327+
unsubscribeDebugMutation &&
328+
unsubscribeTopicMutation &&
329+
unsubscribeLayerMutation &&
330+
unsubscribeSearchMutation &&
331+
unsubscribeUIMutation &&
332+
unsubscribeI18nMutation &&
333+
unsubscribeCesiumMutation &&
334+
unsubscribeGeolocationMutation &&
335+
unsubscribePrintMutation
336+
) {
337+
unsubscribePositionMutation()
338+
unsubscribeDebugMutation()
339+
unsubscribeTopicMutation()
340+
unsubscribeLayerMutation()
341+
unsubscribeSearchMutation()
342+
unsubscribeUIMutation()
343+
unsubscribeI18nMutation()
344+
unsubscribeCesiumMutation()
345+
unsubscribeGeolocationMutation()
346+
unsubscribePrintMutation()
347+
}
301348
retVal = undefined
302349
}
303350
} else if (appStore.isReady) {
@@ -342,20 +389,51 @@ const storeSyncRouterPlugin: RouterPlugin = (router): void => {
342389
// which was LEGACY. By moving this subscription to the after Each loop, we ensure the 'currentRoute'
343390
// is always set to MAPVIEW, avoiding a lock of the viewer.
344391
router.afterEach((to: RouteLocationNormalizedGeneric) => {
392+
393+
function storeSubscription(store: Store, subscription: () => void) : void {
394+
subscription = store.$onAction(({after, name, args}) => {
395+
// we need to activate the watcher after the action has been completed
396+
after(() => {
397+
storeMutationWatcher(name, args, router)
398+
})
399+
})
400+
}
345401
if (
346402
typeof to.name === 'string' &&
347403
MAP_VIEWS.includes(to.name) &&
348-
!unsubscribeStoreMutation
404+
!unsubscribeAppReadyMutation
349405
) {
350406
log.info({
351407
title: 'Router store plugin/afterEach',
352408
titleColor: LogPreDefinedColor.Rose,
353409
messages: [`MapView entered, register store mutation watcher`],
354410
})
355411
const appStore = useAppStore()
412+
const cesiumStore = useCesiumStore()
413+
const debugStore = useDebugStore()
414+
const geolocationStore = useGeolocationStore()
415+
const i18nStore = useI18nStore()
416+
const layerStore = useLayersStore()
417+
const positionStore = usePositionStore()
418+
const printStore = usePrintStore()
419+
const searchStore = useSearchStore()
420+
const topicStore = useTopicsStore()
421+
const uiStore = useUIStore()
356422

423+
function applyAllSubscriptions() {
424+
storeSubscription(cesiumStore, unsubscribeCesiumMutation)
425+
storeSubscription(debugStore, unsubscribeDebugMutation)
426+
storeSubscription(geolocationStore, unsubscribeGeolocationMutation)
427+
storeSubscription(i18nStore, unsubscribeI18nMutation)
428+
storeSubscription(layerStore, unsubscribeLayerMutation)
429+
storeSubscription(positionStore, unsubscribePositionMutation)
430+
storeSubscription(printStore, unsubscribePrintMutation)
431+
storeSubscription(searchStore, unsubscribeSearchMutation)
432+
storeSubscription(topicStore, unsubscribeTopicMutation)
433+
storeSubscription(uiStore, unsubscribeUIMutation)
434+
}
357435
// listening to store mutation to update URL
358-
unsubscribeStoreMutation = appStore.$onAction(({ name, args }) => {
436+
unsubscribeAppReadyMutation = appStore.$onAction(({ name, args }) => {
359437
if (name === 'setAppIsReady') {
360438
// If the app was not yet ready after entering the map view, we need to
361439
// trigger the initial urlQuery watcher otherwise we have a blank application.
@@ -364,9 +442,16 @@ const storeSyncRouterPlugin: RouterPlugin = (router): void => {
364442
titleColor: LogPreDefinedColor.Rose,
365443
messages: [`App is ready, trigger initial URL query watcher`],
366444
})
445+
applyAllSubscriptions()
367446
initialUrlQueryWatcher(to, router)
368447
} else if (appStore.isReady) {
448+
console.error('DO WE EVEN SET THIS THING ?')
449+
// here, we need to set up the store mutation watcher for all stores.
450+
451+
452+
applyAllSubscriptions()
369453
storeMutationWatcher(name, args, router)
454+
370455
}
371456
})
372457

@@ -382,6 +467,7 @@ const storeSyncRouterPlugin: RouterPlugin = (router): void => {
382467
`MapView entered, while app was already ready ! Trigger initial URL query watcher`,
383468
],
384469
})
470+
applyAllSubscriptions()
385471
initialUrlQueryWatcher(to, router)
386472
}
387473
}

0 commit comments

Comments
 (0)