@@ -3,6 +3,7 @@ import { suspend, clear } from 'suspend-react';
33import type { ISceneLoaderAsyncResult , ImportMeshOptions } from '@babylonjs/core' ;
44import { ImportMeshAsync } from '@babylonjs/core/Loading/sceneLoader.js' ;
55import { useScene } from './store' ;
6+ import isEqual from 'lodash/isEqual' ;
67
78function dispose ( model : ISceneLoaderAsyncResult ) {
89 Object . values ( model ) . forEach ( attribute => {
@@ -15,14 +16,20 @@ function dispose(model: ISceneLoaderAsyncResult) {
1516/**
1617 * Custom hook to load a model using Babylon.js's ImportMeshAsync function.
1718 *
18- * @remarks This hook activates suspense.
19+ * @remarks This hook activates suspense. The cache is invalidated when the url or key change.
1920 *
2021 * @param url the URL or file or ArrayBufferView of the model to load.
2122 * @param options optional parameters for importing the model.
2223 * @param onCreate the callback function to execute when the model is loaded.
24+ * @param key unique identifier to invalidate the cache.
2325 * @returns the result of the model import.
2426 */
25- export function useModel ( url : string | File | ArrayBufferView , options ?: ImportMeshOptions , onCreate ?: ( model : ISceneLoaderAsyncResult ) => void ) : ISceneLoaderAsyncResult {
27+ export function useModel (
28+ url : string | File | ArrayBufferView ,
29+ options ?: ImportMeshOptions ,
30+ onCreate ?: ( model : ISceneLoaderAsyncResult ) => void ,
31+ key ?: string ,
32+ ) : ISceneLoaderAsyncResult {
2633 const scene = useScene ( ) ;
2734
2835 const model = suspend (
@@ -33,15 +40,16 @@ export function useModel(url: string | File | ArrayBufferView, options?: ImportM
3340 }
3441 return result ;
3542 } ,
36- [ url , scene , options ] /*{ equal: isEqual }*/ ,
43+ [ url , scene , key ] ,
44+ { equal : isEqual } ,
3745 ) ;
3846
3947 useEffect ( ( ) => {
4048 return ( ) => {
4149 dispose ( model ) ;
4250 clear ( [ url , scene , options ] ) ;
4351 } ;
44- } , [ ] ) ;
52+ } , [ url , scene , key ] ) ;
4553
4654 return model ;
4755}
0 commit comments