2
0
Эх сурвалжийг харах

ImageBitmapLoader: Cache promises to deduplicate requests. (#27270)

* Fix for dispose error
sampleRenderTarget is being deleted and set to null by super.dispose();
the check for undefined was incorrect causing an attempt to call dispose on a null object.

* Removed disposal of sampleRenderTarget entierly

* Fix for setting path not affecting GLTF Sub Assets correctly.

* Fixed Comment

* Update GLTFLoader.js

Fix code style.

* Added additional caching to ImageBitmapLoader
So that if multiple calls to load the same image come in,
before the first one is loaded it will still only load it once.

* Lint

---------

Co-authored-by: Michael Herzog <[email protected]>
Jesse 1 жил өмнө
parent
commit
a5a728dab1

+ 25 - 1
src/loaders/ImageBitmapLoader.js

@@ -49,6 +49,25 @@ class ImageBitmapLoader extends Loader {
 
 			scope.manager.itemStart( url );
 
+			// If cached is a promise, wait for it to resolve
+			if ( cached.then ) {
+
+				cached.then( imageBitmap => {
+
+					if ( onLoad ) onLoad( imageBitmap );
+
+					scope.manager.itemEnd( url );
+
+				} ).catch( e => {
+
+					if ( onError ) onError( e );
+
+				} );
+				return;
+
+			}
+
+			// If cached is not a promise (i.e., it's already an imageBitmap)
 			setTimeout( function () {
 
 				if ( onLoad ) onLoad( cached );
@@ -65,7 +84,7 @@ class ImageBitmapLoader extends Loader {
 		fetchOptions.credentials = ( this.crossOrigin === 'anonymous' ) ? 'same-origin' : 'include';
 		fetchOptions.headers = this.requestHeader;
 
-		fetch( url, fetchOptions ).then( function ( res ) {
+		const promise = fetch( url, fetchOptions ).then( function ( res ) {
 
 			return res.blob();
 
@@ -81,15 +100,20 @@ class ImageBitmapLoader extends Loader {
 
 			scope.manager.itemEnd( url );
 
+			return imageBitmap;
+
 		} ).catch( function ( e ) {
 
 			if ( onError ) onError( e );
 
+			Cache.remove( url );
+
 			scope.manager.itemError( url );
 			scope.manager.itemEnd( url );
 
 		} );
 
+		Cache.add( url, promise );
 		scope.manager.itemStart( url );
 
 	}