Can Three.js loaders accept an AbortSignal for cooperative cancellation?
0 reputation · 04 Aug 2023, 23:04 UTC
0 reputation · 04 Aug 2023, 23:04 UTC
Three.js provides LoadingManager to coordinate loader progress, but individual loaders such as TextureLoader and GLTFLoader expose only onLoad, onProgress, and onError callbacks without a built‑in timeout or abort mechanism. Applications that need to cancel a long‑running load must currently discard the result and manually clean up any partially created resources, which can lead to leaks or inconsistent state if the loader internally retains references.
The core library does not expose an AbortSignal parameter, and the underlying XHR/fetch implementation varies across releases, making a uniform cancellation strategy unclear. An unresolved design decision is whether loaders should natively accept an AbortSignal for cooperative cancellation, allowing the application to signal abort and have the loader stop further work and release allocated objects.
Does adding an AbortSignal argument to loader constructors preserve backward compatibility? How would LoadingManager propagate abort signals to its child loaders? What cleanup responsibilities would remain with the application after a loader aborts?
33775 reputation · 05 Aug 2023, 09:48 UTC
No, the standard Three.js loader classes (e.g., GLTFLoader, TextureLoader, FileLoader) do not accept an AbortSignal argument in their constructors or load methods.
Loaders rely on the callback‑based API (onLoad, onProgress, onError) and internally use either XMLHttpRequest or the Fetch API via a private FileLoader. That internal loader does not expose a way to pass an AbortSignal to the underlying network request, so there is no built‑in cooperative cancellation mechanism.
Promise that resolves/rejects based on the loader’s callbacks and manually checks an AbortSignal. If the signal is aborted, ignore the loader’s result and dispose of any partially created Three.js objects.fetch-based FileLoader (r110+), you can subclass FileLoader and override its load method to accept an AbortSignal and pass it to fetch. This requires maintaining a custom loader subclass.// 1. Check the current FileLoader source (no build step)
fetch('https://unpkg.com/three@0.152.2/build/FileLoader.js')
.then(r => r.text())
.then(txt => {
console.log(/AbortSignal/.test(txt) ? 'Found' : 'Not found');
});
Replace the version in the URL with the one you are using. A “Not found” output confirms the lack of native support.
Which Three.js version are you targeting? The internal implementation switched from XMLHttpRequest to Fetch around r110, affecting how a custom abort solution would be implemented.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.