fix: cleanup WebGL resources before reinitializing viewer on model switch

Address CodeRabbit review feedback:
- Call viewer.cleanup() before initializeStandaloneViewer in watcher to prevent
  WebGL context leaks on 3D-to-3D model switches
- Add { flush: 'post' } to ensure DOM is ready before initialization
- Assert cleanup is called before reinitialization in test
- Unmount wrappers in all tests to prevent cross-test coupling
This commit is contained in:
bymyself
2026-03-12 03:07:19 -07:00
parent 235a7e286c
commit 85c6740928
2 changed files with 15 additions and 5 deletions

View File

@@ -57,13 +57,15 @@ describe('Preview3d', () => {
}
it('initializes the viewer on mount', async () => {
await mountPreview3d()
const wrapper = await mountPreview3d()
expect(initializeStandaloneViewer).toHaveBeenCalledOnce()
expect(initializeStandaloneViewer).toHaveBeenCalledWith(
expect.any(HTMLElement),
'http://localhost/view?filename=model.glb'
)
wrapper.unmount()
})
it('cleans up the viewer on unmount', async () => {
@@ -113,10 +115,13 @@ describe('Preview3d', () => {
await nextTick()
await nextTick()
expect(cleanup).toHaveBeenCalledOnce()
expect(initializeStandaloneViewer).toHaveBeenCalledOnce()
expect(initializeStandaloneViewer).toHaveBeenCalledWith(
expect.any(HTMLElement),
'http://localhost/view?filename=model-b.glb'
)
wrapper.unmount()
})
})

View File

@@ -13,11 +13,16 @@ const containerRef = useTemplateRef('containerRef')
const viewer = ref(useLoad3dViewer())
watch([containerRef, () => modelUrl], async () => {
if (!containerRef.value || !modelUrl) return
watch(
[containerRef, () => modelUrl],
async () => {
if (!containerRef.value || !modelUrl) return
await viewer.value.initializeStandaloneViewer(containerRef.value, modelUrl)
})
viewer.value.cleanup()
await viewer.value.initializeStandaloneViewer(containerRef.value, modelUrl)
},
{ flush: 'post' }
)
onUnmounted(() => {
viewer.value.cleanup()