Skip to content

Commit 51ee437

Browse files
committed
fix(components): add equality function + key to invalidate cache on useModel
1 parent b5687e6 commit 51ee437

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

packages/library/src/core/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export { register } from './inventory';
22
export * from './store';
3-
export * from './useLoader';
3+
export * from './useModel';
44
export * from './Scene';
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { suspend, clear } from 'suspend-react';
33
import type { ISceneLoaderAsyncResult, ImportMeshOptions } from '@babylonjs/core';
44
import { ImportMeshAsync } from '@babylonjs/core/Loading/sceneLoader.js';
55
import { useScene } from './store';
6+
import isEqual from 'lodash/isEqual';
67

78
function 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

Comments
 (0)