Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useLoader): dispose loaders #2984

Merged
merged 4 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/fiber/src/core/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,20 @@ export function useGraph(object: THREE.Object3D) {
return React.useMemo(() => buildGraph(object), [object])
}

const memoizedLoaders = new WeakMap<LoaderProto<any>, Loader<any>>()

function loadingFn<L extends LoaderProto<any>>(
extensions?: Extensions<L>,
onProgress?: (event: ProgressEvent<EventTarget>) => void,
) {
return function (Proto: L, ...input: string[]) {
// Construct new loader and run extensions
const loader = new Proto()
let loader = memoizedLoaders.get(Proto)!
if (!loader) {
loader = new Proto()
memoizedLoaders.set(Proto, loader)
}

if (extensions) extensions(loader)
// Go through the urls and load them
return Promise.all(
Expand All @@ -103,7 +110,7 @@ function loadingFn<L extends LoaderProto<any>>(
),
),
),
)
).finally(() => (loader as any).dispose?.())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @CodyJasonBennett this breaks using DRACOLoader directly (useLoader(DRACOLoader, url), without useGTLF() or similar), since DRACOLoader cannot be used again after dispose https://threejs.org/docs/#examples/en/loaders/DRACOLoader.dispose

At the moment I simply workaround this by making dispose() not do anything.

While debugging this issue I also checked #3131, and I think it won't work as expected, I think dispose() should not be called if the user wants to reuse a particular loader.

What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree. This was an oversight.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

Expand Down
32 changes: 15 additions & 17 deletions packages/fiber/tests/core/hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,21 @@ describe('hooks', () => {
mesh2.name = 'Mesh 2'
MockGroup.add(mesh1, mesh2)

jest.spyOn(Stdlib, 'GLTFLoader').mockImplementation(
() =>
({
load: jest
.fn()
.mockImplementationOnce((_url, onLoad) => {
onLoad(MockMesh)
})
.mockImplementationOnce((_url, onLoad) => {
onLoad({ scene: MockGroup })
}),
setPath: () => {},
} as unknown as Stdlib.GLTFLoader),
)
class TestLoader extends THREE.Loader {
load = jest
.fn()
.mockImplementationOnce((_url, onLoad) => {
onLoad(MockMesh)
})
.mockImplementationOnce((_url, onLoad) => {
onLoad(MockGroup)
})
}

const extensions = jest.fn()

const Component = () => {
const [mockMesh, mockScene] = useLoader(Stdlib.GLTFLoader, ['/suzanne.glb', '/myModels.glb'], (loader) => {
loader.setPath('/public/models')
})
const [mockMesh, mockScene] = useLoader(TestLoader, ['/suzanne.glb', '/myModels.glb'], extensions)

return (
<>
Expand All @@ -180,6 +176,8 @@ describe('hooks', () => {
await waitFor(() => expect(scene.children[0]).toBeDefined())

expect(scene.children[0]).toBe(MockMesh)
expect(scene.children[1]).toBe(MockGroup)
expect(extensions).toBeCalledTimes(1)
})

it('can handle useLoader with a loader extension', async () => {
Expand Down
Loading