Browse Source

have fileloader merge multiple concurrent requests for the same url (fixes #10644)

Kevin Ngo 7 years ago
parent
commit
23690f5728
1 changed files with 26 additions and 1 deletions
  1. 26 1
      src/loaders/FileLoader.js

+ 26 - 1
src/loaders/FileLoader.js

@@ -23,7 +23,7 @@ Object.assign( FileLoader.prototype, {
 
 
 		var cached = Cache.get( url );
 		var cached = Cache.get( url );
 
 
-		if ( cached !== undefined ) {
+		if ( cached !== undefined && ! cached.loaderSubscriptions ) {
 
 
 			scope.manager.itemStart( url );
 			scope.manager.itemStart( url );
 
 
@@ -39,6 +39,19 @@ Object.assign( FileLoader.prototype, {
 
 
 		}
 		}
 
 
+		// If file is already in process of loading, wait for it to load.
+		if ( cached !== undefined && cached.loaderSubscriptions ) {
+
+			cached.loaderSubscriptions.push( function () {
+
+				scope.load( url, onLoad, onProgress, onError );
+
+			} );
+
+			return;
+
+		}
+
 		// Check for data: URI
 		// Check for data: URI
 		var dataUriRegex = /^data:(.*?)(;base64)?,(.*)$/;
 		var dataUriRegex = /^data:(.*?)(;base64)?,(.*)$/;
 		var dataUriRegexResult = url.match( dataUriRegex );
 		var dataUriRegexResult = url.match( dataUriRegex );
@@ -128,7 +141,12 @@ Object.assign( FileLoader.prototype, {
 
 
 		} else {
 		} else {
 
 
+			var loaderSubscriptions = [];
 			var request = new XMLHttpRequest();
 			var request = new XMLHttpRequest();
+
+			// Allow other file loaders to wait and subscribe on this request.
+			Cache.add( url, { loaderSubscriptions: loaderSubscriptions } );
+
 			request.open( 'GET', url, true );
 			request.open( 'GET', url, true );
 
 
 			request.addEventListener( 'load', function ( event ) {
 			request.addEventListener( 'load', function ( event ) {
@@ -163,6 +181,13 @@ Object.assign( FileLoader.prototype, {
 
 
 				}
 				}
 
 
+				// Tell other requests for the same file that the file has finished loading.
+				for ( var i = 0; i < loaderSubscriptions.length; i ++ ) {
+
+					loaderSubscriptions[ i ]( response );
+
+				}
+
 			}, false );
 			}, false );
 
 
 			if ( onProgress !== undefined ) {
 			if ( onProgress !== undefined ) {