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 [ ] = [
@@ -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 */
268280const 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