From 492c086d39cdb0ff0666b6eeb39166f98941b7b1 Mon Sep 17 00:00:00 2001
From: Xiao Gui <xgui3783@gmail.com>
Date: Thu, 24 Aug 2023 10:53:05 +0200
Subject: [PATCH] fix: user state encoded annotations fix: improve reliability
 on viewer first load

---
 .../userAnnotations/tools/service.ts          |  1 +
 .../userAnnotations/tools/type.ts             |  7 ++++-
 src/routerModule/effects.ts                   |  6 +++-
 src/state/actions.ts                          |  4 +++
 src/state/index.ts                            | 31 ++++++++++++++++++-
 5 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/src/atlasComponents/userAnnotations/tools/service.ts b/src/atlasComponents/userAnnotations/tools/service.ts
index 9ef8d363a..5fa7acf92 100644
--- a/src/atlasComponents/userAnnotations/tools/service.ts
+++ b/src/atlasComponents/userAnnotations/tools/service.ts
@@ -260,6 +260,7 @@ export class ModularUserAnnotationToolService implements OnDestroy{
 
   #voxelSize = this.store.pipe(
     select(atlasSelection.selectors.selectedTemplate),
+    filter(v => !!v),
     switchMap(tmpl => translateV3Entities.translateSpaceToVolumeImage(tmpl)),
     map(volImages => {
       if (volImages.length === 0) {
diff --git a/src/atlasComponents/userAnnotations/tools/type.ts b/src/atlasComponents/userAnnotations/tools/type.ts
index d54b231af..a100ebcf4 100644
--- a/src/atlasComponents/userAnnotations/tools/type.ts
+++ b/src/atlasComponents/userAnnotations/tools/type.ts
@@ -336,7 +336,12 @@ export abstract class IAnnotationGeometry extends Highlightable {
   constructor(spec?: TBaseAnnotationGeomtrySpec){
     super()
     this.id = spec && spec.id || getUuid()
-    this.space = spec?.space
+
+    // older version of annotations were made with at_id as key
+    // fallback
+    const spaceId = spec?.space?.id || spec?.space?.['@id']
+    
+    this.space = spaceId && { id: spaceId }
     this.name = spec?.name
     this.desc = spec?.desc
   }
diff --git a/src/routerModule/effects.ts b/src/routerModule/effects.ts
index ec7c34de0..4ef7d676e 100644
--- a/src/routerModule/effects.ts
+++ b/src/routerModule/effects.ts
@@ -67,7 +67,11 @@ export class RouterEffects {
       return generalActions.generalApplyState({
         state
       })
-    })
+    }),
+    switchMap(ac => from([
+      ac,
+      generalActions.routeParseComplete()
+    ]))
   ))
 
   onRouteUpdate$ = createEffect(() => this.#atlasesLoaded$.pipe(
diff --git a/src/state/actions.ts b/src/state/actions.ts
index 67b9df55d..c6801c132 100644
--- a/src/state/actions.ts
+++ b/src/state/actions.ts
@@ -15,6 +15,10 @@ export const generalApplyState = createAction(
   }>()
 )
 
+export const routeParseComplete = createAction(
+  `${nameSpace} routeParseComplete`
+)
+
 export const noop = createAction(
   `${nameSpace} noop`
 )
\ No newline at end of file
diff --git a/src/state/index.ts b/src/state/index.ts
index aa9964ec9..77c5f08d0 100644
--- a/src/state/index.ts
+++ b/src/state/index.ts
@@ -31,6 +31,34 @@ function debug(reducer: ActionReducer<MainState>): ActionReducer<MainState> {
   };
 }
 
+function gateKeepUntilRouteParseComplete(reducer: ActionReducer<MainState>): ActionReducer<MainState>{
+  let completeFlag = false
+  const actions = []
+  return function(state, action) {
+    const isSystem = action.type.includes("@ngrx")
+    if (completeFlag || isSystem) {
+      return reducer(state, action)
+    }
+
+    let _state = state
+    if (action.type === routeParseComplete.type) {
+      while (actions.length > 0) {
+        const _action = actions.shift() // FIFO
+        _state = reducer(_state, _action)
+      }
+      completeFlag = true
+      return reducer(_state, action)
+    }
+
+    if (action.type === generalApplyState.type) {
+
+      return reducer(state, action)
+    }
+    actions.push(action)
+    return reducer(state, noop)
+  }
+}
+
 function generalApplyStateReducer(reducer: ActionReducer<MainState>): ActionReducer<MainState> {
   return function(_state, action) {
     let state = _state
@@ -55,6 +83,7 @@ export const RootStoreModule = StoreModule.forRoot({
   [atlasAppearance.nameSpace]: atlasAppearance.reducer,
 },{
   metaReducers: [ 
+    gateKeepUntilRouteParseComplete,
     generalApplyStateReducer,
     // debug,
   ]
@@ -77,7 +106,7 @@ export function getStoreEffects() {
 }
 
 import { MainState } from "./const"
-import { generalApplyState } from "./actions"
+import { generalApplyState, routeParseComplete, noop } from "./actions"
 
 export { MainState }
 
-- 
GitLab