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

[v9] fix(useLoader): localize url caching per loader #3243

Open
wants to merge 1 commit into
base: v9
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 29 additions & 14 deletions packages/fiber/src/core/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ export type LoaderResult<T> = T extends { scene: THREE.Object3D } ? T & ObjectMa
export type Extensions<T> = (loader: Loader<T>) => void

const memoizedLoaders = new WeakMap<LoaderProto<any>, Loader<any>>()
const memoizedResults = new WeakMap<Loader<unknown>, Map<string, Promise<unknown>>>()

const isConstructor = <T,>(value: unknown): value is LoaderProto<T> =>
typeof value === 'function' && value?.prototype?.constructor === value

function loadingFn<T>(extensions?: Extensions<T>, onProgress?: (event: ProgressEvent) => void) {
return async function (Proto: Loader<T> | LoaderProto<T>, ...input: string[]) {
let loader: Loader<any>

return async function (Proto: Loader<T> | LoaderProto<T>, ...urls: string[]) {
// Construct and cache loader if constructor was passed
let loader!: Loader<T>
if (isConstructor(Proto)) {
loader = memoizedLoaders.get(Proto)!
if (!loader) {
Expand All @@ -110,22 +110,37 @@ function loadingFn<T>(extensions?: Extensions<T>, onProgress?: (event: ProgressE
loader = Proto
}

// Prime per-loader cache
let results: Map<string, Promise<unknown>> = memoizedResults.get(loader)!
if (!results) {
results = new Map()
memoizedResults.set(loader, results)
}

// Apply loader extensions
if (extensions) extensions(loader)

// Go through the urls and load them
return Promise.all(
input.map(
(input) =>
new Promise<LoaderResult<T>>((res, reject) =>
loader.load(
input,
(data) => res(isObject3D(data?.scene) ? Object.assign(data, buildGraph(data.scene)) : data),
onProgress,
(error) => reject(new Error(`Could not load ${input}: ${(error as ErrorEvent)?.message}`)),
),
),
),
urls.map((url) => {
// Load url and cache the result on first try
if (!results.has(url)) {
try {
results.set(
url,
loader
.loadAsync(url, onProgress)
.then((data) => (isObject3D(data?.scene) ? Object.assign(data, buildGraph(data.scene)) : data)),
)
} catch (error) {
// TODO: merge errors for multiple urls
throw new Error(`Could not load ${url}: ${(error as ErrorEvent)?.message}`)
Comment on lines +136 to +137
Copy link
Member Author

Choose a reason for hiding this comment

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

Related: #3181

}
}

// Return cached pending or completed result
return results.get(url)
}),
)
}
}
Expand Down