1+ import type { Store } from 'pinia'
12import type { LocationQuery , RouteLocationNormalizedGeneric , Router } from 'vue-router'
23
34import log , { LogPreDefinedColor } from '@swissgeo/log'
@@ -14,7 +15,17 @@ import UrlParamConfig, {
1415} from '@/router/storeSync/UrlParamConfig.class'
1516import { MAP_VIEWS } from '@/router/viewNames'
1617import 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'
1726import useShareStore from '@/store/modules/share'
27+ import useTopicsStore from '@/store/modules/topics'
28+ import useUIStore from '@/store/modules/ui'
1829
1930export const FAKE_URL_CALLED_AFTER_ROUTE_CHANGE : string = '/tell-cypress-route-has-changed'
2031const 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 */
268278const 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