Skip to content

Commit 18322f9

Browse files
Make plugin manager initialization and cleanup use react ref (#5146)
1 parent 51e1867 commit 18322f9

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

products/jbrowse-web/src/components/Loader.tsx

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Suspense, lazy, useEffect, useState } from 'react'
1+
import { Suspense, lazy, useCallback, useEffect, useRef, useState } from 'react'
22

33
import { FatalErrorDialog } from '@jbrowse/core/ui'
44
import { ErrorBoundary } from '@jbrowse/core/ui/ErrorBoundary'
@@ -98,12 +98,10 @@ const Renderer = observer(function ({
9898
loader: SessionLoaderModel
9999
}) {
100100
const [loader, setLoader] = useState(firstLoader)
101-
const { configError, ready, sessionTriaged } = loader
102-
const [pluginManager, setPluginManager] = useState<PluginManager>()
103-
const [error, setError] = useState<unknown>()
104-
105-
useEffect(() => {
106-
const reloadPluginManager = (
101+
const pluginManager = useRef<PluginManager | undefined>(undefined)
102+
const [pluginManagerCreated, setPluginManagerCreated] = useState(false)
103+
const reloadPluginManager = useCallback(
104+
(
107105
configSnapshot: Record<string, unknown>,
108106
sessionSnapshot: Record<string, unknown>,
109107
) => {
@@ -125,22 +123,32 @@ const Renderer = observer(function ({
125123
sessionSnapshot,
126124
})
127125
setLoader(newLoader)
128-
}
126+
setPluginManagerCreated(false)
127+
},
128+
[loader],
129+
)
130+
const { configError, ready, sessionTriaged } = loader
131+
const [error, setError] = useState<unknown>()
129132

130-
try {
131-
if (ready) {
132-
setPluginManager(previousPluginManager => {
133-
if (previousPluginManager?.rootModel) {
134-
destroy(previousPluginManager.rootModel)
135-
}
136-
return createPluginManager(loader, reloadPluginManager)
137-
})
133+
useEffect(() => {
134+
if (ready) {
135+
try {
136+
if (pluginManager.current?.rootModel) {
137+
destroy(pluginManager.current.rootModel)
138+
}
139+
pluginManager.current = createPluginManager(loader, reloadPluginManager)
140+
setPluginManagerCreated(true)
141+
} catch (e) {
142+
console.error(e)
143+
setError(e)
144+
}
145+
}
146+
return () => {
147+
if (pluginManager.current?.rootModel) {
148+
destroy(pluginManager.current.rootModel)
138149
}
139-
} catch (e) {
140-
console.error(e)
141-
setError(e)
142150
}
143-
}, [loader, ready])
151+
}, [ready, loader, reloadPluginManager])
144152

145153
const err = configError || error
146154
if (err) {
@@ -151,8 +159,8 @@ const Renderer = observer(function ({
151159
)
152160
} else if (sessionTriaged) {
153161
return <SessionTriaged loader={loader} sessionTriaged={sessionTriaged} />
154-
} else if (pluginManager) {
155-
return <JBrowse pluginManager={pluginManager} />
162+
} else if (pluginManagerCreated && pluginManager.current) {
163+
return <JBrowse pluginManager={pluginManager.current} />
156164
} else {
157165
return <Loading />
158166
}

products/jbrowse-web/src/tests/Loader.test.tsx

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { FatalErrorDialog } from '@jbrowse/core/ui'
2-
import { ErrorBoundary } from '@jbrowse/core/ui/ErrorBoundary'
31
import { render, waitFor } from '@testing-library/react'
42
import { Image, createCanvas } from 'canvas'
53
import { LocalFile } from 'generic-filehandle2'
@@ -118,17 +116,7 @@ test('can use config from a url with nonexistent share param ', async () => {
118116

119117
test('can catch error from loading a bad config', async () => {
120118
const { findAllByText } = render(
121-
<ErrorBoundary
122-
FallbackComponent={props => (
123-
<FatalErrorDialog
124-
{...props}
125-
resetButtonText="Reset Session"
126-
onFactoryReset={() => {}}
127-
/>
128-
)}
129-
>
130-
<App search="?config=test_data/bad_config_test/config.json" />
131-
</ErrorBoundary>,
119+
<App search="?config=test_data/bad_config_test/config.json" />,
132120
)
133121
await findAllByText(/Error while converting/)
134122
}, 20000)

0 commit comments

Comments
 (0)