From 3053ad02a4ddec8a8f57d0e03ff678a4fd3bc3d6 Mon Sep 17 00:00:00 2001 From: Alexie Papanicolaou Date: Fri, 10 Oct 2025 00:57:41 +1100 Subject: [PATCH] address race condition where assemblies exist on the server but aren't loaded in the client state when features are being added; prevents the MobX State Tree error by checking session state before broadcastLocations --- .../src/ApolloInternetAccount/model.ts | 11 ++++++++-- .../src/session/ClientDataStore.ts | 22 ++++++++++++------- 2 files changed, 23 insertions(+), 10 deletions(-) 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) },