Skip to content

Commit

Permalink
TilesRenderer: remove loadIndex in favor of abort signal (#914)
Browse files Browse the repository at this point in the history
* Move error callback

* Use abort signal instead of load index

* Remove load index from base renderer

* Remove load index from TilesRenderer
  • Loading branch information
gkjohnson authored Jan 9, 2025
1 parent 7b1d31a commit 47c6d96
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 60 deletions.
97 changes: 45 additions & 52 deletions src/base/TilesRendererBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,6 @@ export class TilesRendererBase {
tile.__active = false;

tile.__loadingState = UNLOADED;
tile.__loadIndex = 0;

tile.__loadAbort = null;

Expand Down Expand Up @@ -668,7 +667,6 @@ export class TilesRendererBase {
}

t.__loadingState = UNLOADED;
t.__loadIndex ++;

parseQueue.remove( t );
downloadQueue.remove( t );
Expand All @@ -682,10 +680,7 @@ export class TilesRendererBase {

}

// Track a new load index so we avoid the condition where this load is stopped and
// another begins soon after so we don't parse twice.
tile.__loadIndex ++;
const loadIndex = tile.__loadIndex;
// track an abort controller and pass-through the below conditions if aborted
const controller = new AbortController();
const signal = controller.signal;

Expand All @@ -694,49 +689,10 @@ export class TilesRendererBase {
tile.__loadAbort = controller;
tile.__loadingState = LOADING;

const errorCallback = e => {

// if it has been unloaded then the tile has been disposed
if ( tile.__loadIndex !== loadIndex ) {

return;

}

if ( e.name !== 'AbortError' ) {

parseQueue.remove( tile );
downloadQueue.remove( tile );

if ( tile.__loadingState === PARSING ) {

stats.parsing --;

} else if ( tile.__loadingState === LOADING ) {

stats.downloading --;

}

stats.failed ++;

console.error( `TilesRenderer : Failed to load tile at url "${ tile.content.uri }".` );
console.error( e );
tile.__loadingState = FAILED;
lruCache.setLoaded( tile, true );

} else {

lruCache.remove( tile );

}

};

// queue the download and parse
return downloadQueue.add( tile, downloadTile => {

if ( downloadTile.__loadIndex !== loadIndex ) {
if ( signal.aborted ) {

return Promise.resolve();

Expand All @@ -747,7 +703,7 @@ export class TilesRendererBase {
} )
.then( res => {

if ( tile.__loadIndex !== loadIndex ) {
if ( signal.aborted ) {

return;

Expand All @@ -767,7 +723,7 @@ export class TilesRendererBase {
.then( content => {

// if it has been unloaded then the tile has been disposed
if ( tile.__loadIndex !== loadIndex ) {
if ( signal.aborted ) {

return;

Expand All @@ -781,7 +737,7 @@ export class TilesRendererBase {
return parseQueue.add( tile, parseTile => {

// if it has been unloaded then the tile has been disposed
if ( parseTile.__loadIndex !== loadIndex ) {
if ( signal.aborted ) {

return Promise.resolve();

Expand All @@ -796,7 +752,7 @@ export class TilesRendererBase {

} else {

return this.invokeOnePlugin( plugin => plugin.parseTile && plugin.parseTile( content, parseTile, extension, uri ) );
return this.invokeOnePlugin( plugin => plugin.parseTile && plugin.parseTile( content, parseTile, extension, uri, signal ) );

}

Expand All @@ -806,7 +762,7 @@ export class TilesRendererBase {
.then( () => {

// if it has been unloaded then the tile has been disposed
if ( tile.__loadIndex !== loadIndex ) {
if ( signal.aborted ) {

return;

Expand Down Expand Up @@ -836,7 +792,44 @@ export class TilesRendererBase {
}

} )
.catch( errorCallback );
.catch( e => {

// if it has been unloaded then the tile has been disposed
if ( signal.aborted ) {

return;

}

if ( e.name !== 'AbortError' ) {

parseQueue.remove( tile );
downloadQueue.remove( tile );

if ( tile.__loadingState === PARSING ) {

stats.parsing --;

} else if ( tile.__loadingState === LOADING ) {

stats.downloading --;

}

stats.failed ++;

console.error( `TilesRenderer : Failed to load tile at url "${ tile.content.uri }".` );
console.error( e );
tile.__loadingState = FAILED;
lruCache.setLoaded( tile, true );

} else {

lruCache.remove( tile );

}

} );

}

Expand Down
10 changes: 2 additions & 8 deletions src/three/TilesRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,6 @@ export class TilesRenderer extends TilesRendererBase {

tile.cached = {

_loadIndex: 0,
transform,
transformInverse,

Expand All @@ -571,18 +570,15 @@ export class TilesRenderer extends TilesRendererBase {

}

async parseTile( buffer, tile, extension, uri ) {
async parseTile( buffer, tile, extension, uri, abortSignal ) {

const cached = tile.cached;
cached._loadIndex ++;

const uriSplits = uri.split( /[\\/]/g );
uriSplits.pop();
const workingPath = uriSplits.join( '/' );
const fetchOptions = this.fetchOptions;

const manager = this.manager;
const loadIndex = cached._loadIndex;
let promise = null;

const cachedTransform = cached.transform;
Expand Down Expand Up @@ -697,7 +693,7 @@ export class TilesRenderer extends TilesRendererBase {
} );

// exit early if a new request has already started
if ( cached._loadIndex !== loadIndex ) {
if ( abortSignal.aborted ) {

return;

Expand Down Expand Up @@ -859,8 +855,6 @@ export class TilesRenderer extends TilesRendererBase {

}

cached._loadIndex ++;

}

setTileVisible( tile, visible ) {
Expand Down

0 comments on commit 47c6d96

Please sign in to comment.