-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
Description
Description of the problem
Given the code below, only one request should be fired.
THREE.Cache.enabled = true
var fileLoader = new THREE.FileLoader();
fileLoader.load('foo.jpg'); // A
fileLoader.load('foo.jpg'); // BAchecks the cache and sees no pending requests.Amakes the request and stores the pending request in the cache as a Promise. The Promise will resolve the file.Bchecks the cache and sees a pending request.Bwaits for the Promise to resolve the file.- The request finishes, and the Promise resolves.
AandB, both waiting on the Promise, triggers their callbacks at the same time with the same file response.
I'd like to volunteer to work on this. In A-Frame, we often have been having to maintain a cache of promises to prevent duplicate requests. If it sounds reasonable, below are several possible implementations:
-
Change THREE.Cache to store promises that resolve files, rather than directly storing files. I think this is the optimal solution because requests will always be asynchronous. This is sort of a breaking change, but three.js applications don't often enable the cache, or don't care about the internals of the cache.
-
Create a separate cache object (
RequestCache) for pending requests. The current Cache stays intact, but there is more logic to maintain both the pending and complete caches. -
Keep the pending request cache internal to the FileLoader. It'll just be a variable
var requestCache = {}.
We'd also need a Promise polyfill. They're less than 1KB gzipped if that sounds fine.
Three.js version
- Dev
- r84
Browser
- All of them
OS
- All of them
Hardware Requirements (graphics card, VR Device, ...)
N/A