Skip to content

Commit

Permalink
have fileloader merge multiple concurrent requests for the same url (f…
Browse files Browse the repository at this point in the history
…ixes #10644)
  • Loading branch information
ngokevin committed Oct 19, 2017
1 parent fdefb19 commit 23690f5
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/loaders/FileLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Object.assign( FileLoader.prototype, {

var cached = Cache.get( url );

if ( cached !== undefined ) {
if ( cached !== undefined && ! cached.loaderSubscriptions ) {

scope.manager.itemStart( url );

Expand All @@ -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
var dataUriRegex = /^data:(.*?)(;base64)?,(.*)$/;
var dataUriRegexResult = url.match( dataUriRegex );
Expand Down Expand Up @@ -128,7 +141,12 @@ Object.assign( FileLoader.prototype, {

} else {

var loaderSubscriptions = [];
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.addEventListener( 'load', function ( event ) {
Expand Down Expand Up @@ -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 );

if ( onProgress !== undefined ) {
Expand Down

0 comments on commit 23690f5

Please sign in to comment.