Skip to content

Commit

Permalink
Fix mrdoob#20705 Ability to cancel ongoing HTTP requests in loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Jan 10, 2022
1 parent 73a783f commit bd1b604
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/loaders/AnimationLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class AnimationLoader extends Loader {
loader.setPath( this.path );
loader.setRequestHeader( this.requestHeader );
loader.setWithCredentials( this.withCredentials );
loader.setAbortSignal( this.abortSignal );

loader.load( url, function ( text ) {

try {
Expand Down
2 changes: 2 additions & 0 deletions src/loaders/AudioLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class AudioLoader extends Loader {
loader.setPath( this.path );
loader.setRequestHeader( this.requestHeader );
loader.setWithCredentials( this.withCredentials );
loader.setAbortSignal( this.abortSignal );

loader.load( url, function ( buffer ) {

try {
Expand Down
2 changes: 2 additions & 0 deletions src/loaders/BufferGeometryLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class BufferGeometryLoader extends Loader {
loader.setPath( scope.path );
loader.setRequestHeader( scope.requestHeader );
loader.setWithCredentials( scope.withCredentials );
loader.setAbortSignal( this.abortSignal );

loader.load( url, function ( text ) {

try {
Expand Down
1 change: 1 addition & 0 deletions src/loaders/CompressedTextureLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class CompressedTextureLoader extends Loader {
loader.setResponseType( 'arraybuffer' );
loader.setRequestHeader( this.requestHeader );
loader.setWithCredentials( scope.withCredentials );
loader.setAbortSignal( this.abortSignal );

let loaded = 0;

Expand Down
1 change: 1 addition & 0 deletions src/loaders/CubeTextureLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class CubeTextureLoader extends Loader {
const loader = new ImageLoader( this.manager );
loader.setCrossOrigin( this.crossOrigin );
loader.setPath( this.path );
loader.setAbortSignal( this.abortSignal );

let loaded = 0;

Expand Down
2 changes: 2 additions & 0 deletions src/loaders/DataTextureLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class DataTextureLoader extends Loader {
loader.setRequestHeader( this.requestHeader );
loader.setPath( this.path );
loader.setWithCredentials( scope.withCredentials );
loader.setAbortSignal( this.abortSignal );

loader.load( url, function ( buffer ) {

const texData = scope.parse( buffer );
Expand Down
5 changes: 3 additions & 2 deletions src/loaders/FileLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ class FileLoader extends Loader {
const req = new Request( url, {
headers: new Headers( this.requestHeader ),
credentials: this.withCredentials ? 'include' : 'same-origin',
// An abort controller could be added within a future PR
} );

// start the fetch
fetch( req )
fetch( req, {
signal: this.abortSignal,
} )
.then( response => {

if ( response.status === 200 || response.status === 0 ) {
Expand Down
11 changes: 7 additions & 4 deletions src/loaders/ImageBitmapLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,14 @@ class ImageBitmapLoader extends Loader {

}

const fetchOptions = {};
fetchOptions.credentials = ( this.crossOrigin === 'anonymous' ) ? 'same-origin' : 'include';
fetchOptions.headers = this.requestHeader;
const req = new Request( url, {
credentials: ( this.crossOrigin === 'anonymous' ) ? 'same-origin' : 'include',
headers: new Headers( this.requestHeader ),
} );

fetch( url, fetchOptions ).then( function ( res ) {
fetch( req, {
signal: this.abortSignal,
} ).then( function ( res ) {

return res.blob();

Expand Down
18 changes: 18 additions & 0 deletions src/loaders/ImageLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,34 @@ class ImageLoader extends Loader {

}

function onAbortSignal() {

image.src = null;

}

function removeEventListeners() {

image.removeEventListener( 'load', onImageLoad, false );
image.removeEventListener( 'error', onImageError, false );

if ( this.abortSignal ) {

this.abortSignal.removeEventListener( 'abort', onAbortSignal, false );

}

}

image.addEventListener( 'load', onImageLoad, false );
image.addEventListener( 'error', onImageError, false );

if ( this.abortSignal ) {

this.abortSignal.addEventListener( 'abort', onAbortSignal, false );

}

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

if ( this.crossOrigin !== undefined ) image.crossOrigin = this.crossOrigin;
Expand Down
8 changes: 8 additions & 0 deletions src/loaders/Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Loader {
this.path = '';
this.resourcePath = '';
this.requestHeader = {};
this.abortSignal = null;

}

Expand Down Expand Up @@ -65,6 +66,13 @@ class Loader {

}

setAbortSignal( abortSignal ) {

this.abortSignal = abortSignal;
return this;

}

}

export { Loader };
2 changes: 2 additions & 0 deletions src/loaders/MaterialLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class MaterialLoader extends Loader {
loader.setPath( scope.path );
loader.setRequestHeader( scope.requestHeader );
loader.setWithCredentials( scope.withCredentials );
loader.setAbortSignal( this.abortSignal );

loader.load( url, function ( text ) {

try {
Expand Down
5 changes: 5 additions & 0 deletions src/loaders/ObjectLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class ObjectLoader extends Loader {
loader.setPath( this.path );
loader.setRequestHeader( this.requestHeader );
loader.setWithCredentials( this.withCredentials );
loader.setAbortSignal( this.abortSignal );

loader.load( url, function ( text ) {

let json = null;
Expand Down Expand Up @@ -123,6 +125,7 @@ class ObjectLoader extends Loader {
loader.setPath( this.path );
loader.setRequestHeader( this.requestHeader );
loader.setWithCredentials( this.withCredentials );
loader.setAbortSignal( this.abortSignal );

const text = await loader.loadAsync( url, onProgress );

Expand Down Expand Up @@ -451,6 +454,7 @@ class ObjectLoader extends Loader {

loader = new ImageLoader( manager );
loader.setCrossOrigin( this.crossOrigin );
loader.setAbortSignal( this.abortSignal );

for ( let i = 0, il = json.length; i < il; i ++ ) {

Expand Down Expand Up @@ -550,6 +554,7 @@ class ObjectLoader extends Loader {

loader = new ImageLoader( this.manager );
loader.setCrossOrigin( this.crossOrigin );
loader.setAbortSignal( this.abortSignal );

for ( let i = 0, il = json.length; i < il; i ++ ) {

Expand Down
1 change: 1 addition & 0 deletions src/loaders/TextureLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class TextureLoader extends Loader {
const loader = new ImageLoader( this.manager );
loader.setCrossOrigin( this.crossOrigin );
loader.setPath( this.path );
loader.setAbortSignal( this.abortSignal );

loader.load( url, function ( image ) {

Expand Down

0 comments on commit bd1b604

Please sign in to comment.