From 47c6d96dc593fd2ba96ae6a2a91a6ad590b23d92 Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Thu, 9 Jan 2025 20:10:16 +0900 Subject: [PATCH] TilesRenderer: remove loadIndex in favor of abort signal (#914) * Move error callback * Use abort signal instead of load index * Remove load index from base renderer * Remove load index from TilesRenderer --- src/base/TilesRendererBase.js | 97 ++++++++++++++++------------------- src/three/TilesRenderer.js | 10 +--- 2 files changed, 47 insertions(+), 60 deletions(-) diff --git a/src/base/TilesRendererBase.js b/src/base/TilesRendererBase.js index d36227df..0ea5ec83 100644 --- a/src/base/TilesRendererBase.js +++ b/src/base/TilesRendererBase.js @@ -481,7 +481,6 @@ export class TilesRendererBase { tile.__active = false; tile.__loadingState = UNLOADED; - tile.__loadIndex = 0; tile.__loadAbort = null; @@ -668,7 +667,6 @@ export class TilesRendererBase { } t.__loadingState = UNLOADED; - t.__loadIndex ++; parseQueue.remove( t ); downloadQueue.remove( t ); @@ -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; @@ -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(); @@ -747,7 +703,7 @@ export class TilesRendererBase { } ) .then( res => { - if ( tile.__loadIndex !== loadIndex ) { + if ( signal.aborted ) { return; @@ -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; @@ -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(); @@ -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 ) ); } @@ -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; @@ -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 ); + + } + + } ); } diff --git a/src/three/TilesRenderer.js b/src/three/TilesRenderer.js index 237e6b75..4c3a1bc2 100644 --- a/src/three/TilesRenderer.js +++ b/src/three/TilesRenderer.js @@ -546,7 +546,6 @@ export class TilesRenderer extends TilesRendererBase { tile.cached = { - _loadIndex: 0, transform, transformInverse, @@ -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; @@ -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; @@ -859,8 +855,6 @@ export class TilesRenderer extends TilesRendererBase { } - cached._loadIndex ++; - } setTileVisible( tile, visible ) {