Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache parallel requests #12421

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 96 additions & 12 deletions src/loaders/Cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,123 @@ var Cache = {

enabled: false,

files: {},
entries: {},

add: function ( key, file ) {
add: function ( key, value ) {

if ( this.enabled === false ) return;

// console.log( 'THREE.Cache', 'Adding key:', key );

this.files[ key ] = file;
this.entries[ key ] = { value: value };

},

get: function ( key ) {

if ( this.enabled === false ) return;

// console.log( 'THREE.Cache', 'Checking key:', key );
if ( this.enabled === false || this.entries[ key ] === undefined ) return;

return this.files[ key ];
return this.entries[ key ].value;

},

remove: function ( key ) {

delete this.files[ key ];
delete this.entries[ key ];

},

clear: function () {

this.files = {};
this.entries = {};

},

retrieve: function ( key, resolver, onLoad, onProgress, onError ) {

if ( this.enabled === false ) {

resolver( onLoad, onProgress, onError );
return;

}

var entry = this.entries[ key ];

if ( entry === undefined ) {

entry = {
pending: [ { onLoad: onLoad, onProgress: onProgress, onError: onError } ]
};
this.entries[ key ] = entry;

var resolveLoad = function ( value ) {

entry.value = value;

for ( var i = 0; i < entry.pending.length; i ++ ) {

if ( entry.pending[ i ].onLoad !== undefined ) {

entry.pending[ i ].onLoad( value );

}

}

delete entry.pending;

};

var resolveProgress = function ( progress ) {

for ( var i = 0; i < entry.pending.length; i ++ ) {

}
if ( entry.pending[ i ].onProgress !== undefined ) {

entry.pending[ i ].onProgress( progress );

}

}

};
var resolveError = function ( error ) {

entry.error = error;

for ( var i = 0; i < entry.pending.length; i ++ ) {

if ( entry.pending[ i ].onError !== undefined ) {

entry.pending[ i ].onError( error );

}

}

delete entry.pending;

};
return resolver( resolveLoad, resolveProgress, resolveError );

} else if ( entry.pending !== undefined ) {

entry.pending.push( { onLoad: onLoad, onProgress: onProgress, onError: onError } );

} else if ( entry.value !== undefined ) {

onLoad( entry.value );

} else if ( entry.error !== undefined ) {

onError( entry.error );

} else {

throw new Error( "Invalid state in cache" );

}

},

};

Expand Down
111 changes: 55 additions & 56 deletions src/loaders/FileLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,6 @@ Object.assign( FileLoader.prototype, {

var scope = this;

var cached = Cache.get( url );

if ( cached !== undefined ) {

scope.manager.itemStart( url );

setTimeout( function () {

if ( onLoad ) onLoad( cached );

scope.manager.itemEnd( url );

}, 0 );

return cached;

}

// Check for data: URI
var dataUriRegex = /^data:(.*?)(;base64)?,(.*)$/;
var dataUriRegexResult = url.match( dataUriRegex );
Expand Down Expand Up @@ -130,80 +112,97 @@ Object.assign( FileLoader.prototype, {

} else {

var request = new XMLHttpRequest();
request.open( 'GET', url, true );
return Cache.retrieve( url, function ( _onLoad, _onProgress, _onError ) {

request.addEventListener( 'load', function ( event ) {
var request = new XMLHttpRequest();
request.open( 'GET', url, true );

var response = event.target.response;
request.addEventListener( 'load', function ( event ) {

Cache.add( url, response );
var response = event.target.response;

if ( this.status === 200 ) {
if ( this.status === 200 ) {

if ( onLoad ) onLoad( response );
if( _onLoad !== undefined ) {

scope.manager.itemEnd( url );
_onLoad( response );

} else if ( this.status === 0 ) {
}

// Some browsers return HTTP Status 0 when using non-http protocol
// e.g. 'file://' or 'data://'. Handle as success.
scope.manager.itemEnd( url );

console.warn( 'THREE.FileLoader: HTTP Status 0 received.' );
} else if ( this.status === 0 ) {

if ( onLoad ) onLoad( response );
// Some browsers return HTTP Status 0 when using non-http protocol
// e.g. 'file://' or 'data://'. Handle as success.

scope.manager.itemEnd( url );
console.warn( 'THREE.FileLoader: HTTP Status 0 received.' );

} else {
if( _onLoad !== undefined ) {

if ( onError ) onError( event );
_onLoad( response );

scope.manager.itemEnd( url );
scope.manager.itemError( url );
}

}
scope.manager.itemEnd( url );

}, false );
} else {

if( _onError !== undefined ) {

_onError( event );

}

scope.manager.itemEnd( url );
scope.manager.itemError( url );

}

}, false );

if ( onProgress !== undefined ) {

request.addEventListener( 'progress', function ( event ) {

onProgress( event );
if( _onProgress !== undefined) {

_onProgress( event );

}

}, false );

}

request.addEventListener( 'error', function ( event ) {
request.addEventListener( 'error', function ( event ) {

if ( onError ) onError( event );
if( _onError !== undefined ) {

scope.manager.itemEnd( url );
scope.manager.itemError( url );
_onError( event );

}, false );
}

if ( this.responseType !== undefined ) request.responseType = this.responseType;
if ( this.withCredentials !== undefined ) request.withCredentials = this.withCredentials;
scope.manager.itemEnd( url );
scope.manager.itemError( url );

if ( request.overrideMimeType ) request.overrideMimeType( this.mimeType !== undefined ? this.mimeType : 'text/plain' );
}, false );

for ( var header in this.requestHeader ) {
if ( scope.responseType !== undefined ) request.responseType = scope.responseType;
if ( scope.withCredentials !== undefined ) request.withCredentials = scope.withCredentials;

request.setRequestHeader( header, this.requestHeader[ header ] );
if ( request.overrideMimeType ) request.overrideMimeType( scope.mimeType !== undefined ? scope.mimeType : 'text/plain' );

}
for ( var header in scope.requestHeader ) {

request.send( null );
request.setRequestHeader( header, scope.requestHeader[ header ] );

}
}

scope.manager.itemStart( url );
request.send( null );
scope.manager.itemStart( url );

return request;
}, onLoad, onProgress, onError );

}

},

Expand Down
60 changes: 22 additions & 38 deletions src/loaders/ImageLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,64 +24,48 @@ Object.assign( ImageLoader.prototype, {

var scope = this;

var cached = Cache.get( url );
return Cache.retrieve( url, function ( _onLoad, _onProgress, _onError ) {

if ( cached !== undefined ) {
var image = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'img' );

scope.manager.itemStart( url );

setTimeout( function () {
image.addEventListener( 'load', function () {

if ( onLoad ) onLoad( cached );
_onLoad( this );

scope.manager.itemEnd( url );

}, 0 );

return cached;

}

var image = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'img' );

image.addEventListener( 'load', function () {
}, false );

Cache.add( url, this );
/*
image.addEventListener( 'progress', function ( event ) {

if ( onLoad ) onLoad( this );
if ( onProgress ) onProgress( event );

scope.manager.itemEnd( url );
}, false );
*/

}, false );
image.addEventListener( 'error', function ( event ) {

/*
image.addEventListener( 'progress', function ( event ) {
_onError( event );

if ( onProgress ) onProgress( event );

}, false );
*/

image.addEventListener( 'error', function ( event ) {

if ( onError ) onError( event );
scope.manager.itemEnd( url );
scope.manager.itemError( url );

scope.manager.itemEnd( url );
scope.manager.itemError( url );
}, false );

}, false );
if ( url.substr( 0, 5 ) !== 'data:' ) {

if ( url.substr( 0, 5 ) !== 'data:' ) {
if ( scope.crossOrigin !== undefined ) image.crossOrigin = scope.crossOrigin;

if ( this.crossOrigin !== undefined ) image.crossOrigin = this.crossOrigin;
}

}
scope.manager.itemStart( url );

scope.manager.itemStart( url );
image.src = url;

image.src = url;
return image;

return image;
}, onLoad, onProgress, onError );

},

Expand Down
2 changes: 1 addition & 1 deletion test/Three.Unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export * from '../src/geometries/Geometries.js';

//src/helpers
export { ArrowHelper } from '../src/helpers/ArrowHelper.js';
export { AxisHelper } from '../src/helpers/AxisHelper.js';
export { AxesHelper } from '../src/helpers/AxesHelper.js';
export { BoxHelper } from '../src/helpers/BoxHelper.js';
export { Box3Helper } from '../src/helpers/Box3Helper.js';
export { CameraHelper } from '../src/helpers/CameraHelper.js';
Expand Down
Loading