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

TilesRenderer: remove loadIndex in favor of abort signal #914

Merged
merged 5 commits into from
Jan 9, 2025
Merged
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
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
Loading