From 23690f57282a0b4f323dd6787d12ff8ef91ce825 Mon Sep 17 00:00:00 2001 From: Kevin Ngo Date: Wed, 18 Oct 2017 16:21:31 -0700 Subject: [PATCH 1/2] have fileloader merge multiple concurrent requests for the same url (fixes #10644) --- src/loaders/FileLoader.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/loaders/FileLoader.js b/src/loaders/FileLoader.js index 9ac2daa355ed6f..533555a720641c 100644 --- a/src/loaders/FileLoader.js +++ b/src/loaders/FileLoader.js @@ -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 ); @@ -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 ); @@ -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 ) { @@ -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 ) { From eaa5fbcc679313409876bb7356a9a48bfc3633b1 Mon Sep 17 00:00:00 2001 From: Kevin Ngo Date: Wed, 18 Oct 2017 18:49:17 -0700 Subject: [PATCH 2/2] don't use cache object to maintain duplicate requests structure --- src/loaders/FileLoader.js | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/loaders/FileLoader.js b/src/loaders/FileLoader.js index 533555a720641c..ebc3ecfc9ad613 100644 --- a/src/loaders/FileLoader.js +++ b/src/loaders/FileLoader.js @@ -5,6 +5,8 @@ import { Cache } from './Cache'; import { DefaultLoadingManager } from './LoadingManager'; +var currentlyLoadingFiles = {}; + function FileLoader( manager ) { this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager; @@ -23,7 +25,7 @@ Object.assign( FileLoader.prototype, { var cached = Cache.get( url ); - if ( cached !== undefined && ! cached.loaderSubscriptions ) { + if ( cached !== undefined ) { scope.manager.itemStart( url ); @@ -40,9 +42,9 @@ Object.assign( FileLoader.prototype, { } // If file is already in process of loading, wait for it to load. - if ( cached !== undefined && cached.loaderSubscriptions ) { + if ( currentlyLoadingFiles[ url ] !== undefined ) { - cached.loaderSubscriptions.push( function () { + currentlyLoadingFiles[ url ].push( function () { scope.load( url, onLoad, onProgress, onError ); @@ -141,11 +143,14 @@ 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 } ); + if ( Cache.enabled ) { + + // Allow other file loaders to wait and subscribe on this request. + currentlyLoadingFiles[ url ] = []; + + } request.open( 'GET', url, true ); @@ -181,10 +186,18 @@ 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 ++ ) { + if ( Cache.enabled ) { + + var duplicateRequests = currentlyLoadingFiles[ url ]; + + delete currentlyLoadingFiles[ url ]; + + // Tell other requests for the same file that the file has finished loading. + for ( var i = 0; i < duplicateRequests.length; i ++ ) { + + duplicateRequests[i]( response ); - loaderSubscriptions[ i ]( response ); + } }