diff --git a/packages/jbrowse-plugin-apollo/src/ApolloInternetAccount/model.ts b/packages/jbrowse-plugin-apollo/src/ApolloInternetAccount/model.ts index d9bea4667..ae1df7a9f 100644 --- a/packages/jbrowse-plugin-apollo/src/ApolloInternetAccount/model.ts +++ b/packages/jbrowse-plugin-apollo/src/ApolloInternetAccount/model.ts @@ -451,8 +451,15 @@ const stateModelFactory = (configSchema: ApolloInternetAccountConfigModel) => { } // fires when app transitions from prerender, user returns to the app / tab. if (document.visibilityState === 'visible') { - const { session } = getRoot(self) - session.broadcastLocations() + try { + const { session } = getRoot(self) + if (session && !session.isDestroyed) { + session.broadcastLocations() + } + } catch (error) { + // Silently handle the error to prevent crashes + console.warn('Failed to broadcast locations:', error) + } } }) }), diff --git a/packages/jbrowse-plugin-apollo/src/session/ClientDataStore.ts b/packages/jbrowse-plugin-apollo/src/session/ClientDataStore.ts index d396be193..3674b0be7 100644 --- a/packages/jbrowse-plugin-apollo/src/session/ClientDataStore.ts +++ b/packages/jbrowse-plugin-apollo/src/session/ClientDataStore.ts @@ -87,17 +87,23 @@ export function clientDataStoreFactory( return self.assemblies.put(assemblySnapshot) }, addFeature(assemblyId: string, feature: AnnotationFeatureSnapshot) { - const assembly = self.assemblies.get(assemblyId) + let assembly = self.assemblies.get(assemblyId) if (!assembly) { - throw new Error( - `Could not find assembly "${assemblyId}" to add feature "${feature._id}"`, - ) + // Auto-create the assembly if it doesn't exist in client state + // This handles the race condition where assembly exists on server but not in client state + assembly = self.assemblies.put({ + _id: assemblyId, + refSeqs: {}, + }) } - const ref = assembly.refSeqs.get(feature.refSeq) + let ref = assembly.refSeqs.get(feature.refSeq) if (!ref) { - throw new Error( - `Could not find refSeq "${feature.refSeq}" to add feature "${feature._id}"`, - ) + // Auto-create the refSeq if it doesn't exist + ref = assembly.refSeqs.put({ + _id: feature.refSeq, + name: feature.refSeq, + features: {}, + }) } ref.features.put(feature) },