From 06dcfadf20ae05bae5cfa14e9a9759a09dacd643 Mon Sep 17 00:00:00 2001 From: Ryan Lester Date: Wed, 21 May 2014 18:29:40 -0700 Subject: [PATCH] Quick patch to fix mid-update image loaded callbacks --- src/drawer.js | 102 +++++++++++++++++++++++---------------------- src/imageLoader.js | 7 ++++ 2 files changed, 60 insertions(+), 49 deletions(-) diff --git a/src/drawer.js b/src/drawer.js index 54b07281..3d40343a 100644 --- a/src/drawer.js +++ b/src/drawer.js @@ -703,23 +703,10 @@ function loadTile( drawer, tile, time ) { } function onTileLoad( drawer, tile, time, image ) { - var insertionIndex, - cutoff, - worstTile, - worstTime, - worstLevel, - worstTileIndex, - prevTile, - prevTime, - prevLevel, - i; tile.loading = false; - if ( drawer.midUpdate ) { - $.console.warn( "Tile load callback in middle of drawing routine." ); - return; - } else if ( !image && !drawer.viewport.collectionMode ) { + if ( !image && !drawer.viewport.collectionMode ) { $.console.log( "Tile %s failed to load: %s", tile, tile.url ); if( !drawer.debugMode ){ tile.exists = false; @@ -733,48 +720,65 @@ function onTileLoad( drawer, tile, time, image ) { tile.loaded = true; tile.image = image; - - insertionIndex = drawer.tilesLoaded.length; - - if ( drawer.tilesLoaded.length >= drawer.maxImageCacheCount ) { - cutoff = Math.ceil( Math.log( drawer.source.tileSize ) / Math.log( 2 ) ); - - worstTile = null; - worstTileIndex = -1; - - for ( i = drawer.tilesLoaded.length - 1; i >= 0; i-- ) { - prevTile = drawer.tilesLoaded[ i ]; - - if ( prevTile.level <= drawer.cutoff || prevTile.beingDrawn ) { - continue; - } else if ( !worstTile ) { - worstTile = prevTile; - worstTileIndex = i; - continue; - } - - prevTime = prevTile.lastTouchTime; - worstTime = worstTile.lastTouchTime; - prevLevel = prevTile.level; - worstLevel = worstTile.level; - - if ( prevTime < worstTime || - ( prevTime == worstTime && prevLevel > worstLevel ) ) { - worstTile = prevTile; - worstTileIndex = i; - } + if ( drawer.tilesLoaded.length < drawer.maxImageCacheCount ) { + // always safe to append things to cache + drawer.tilesLoaded[ drawer.tilesLoaded.length ] = tile; + } + else { + // need to remove something from cache, + // make sure this doesn't happen mid update + if ( !drawer.midUpdate ) { + updateTileCache( tile, drawer ); } - - if ( worstTile && worstTileIndex >= 0 ) { - worstTile.unload(); - insertionIndex = worstTileIndex; + else { + window.setTimeout( function() { + updateTileCache( tile, drawer ); + }, 1); } } - drawer.tilesLoaded[ insertionIndex ] = tile; drawer.updateAgain = true; } +function updateTileCache( newTile, drawer ) { + var i, prevTile, prevTime, worstTime, prevLevel, worstLevel, + insertionIndex = drawer.tilesLoaded.length, + cutoff = Math.ceil( Math.log( drawer.source.tileSize ) / Math.log( 2 ) ), + + worstTile = null, + worstTileIndex = -1; + + for ( i = drawer.tilesLoaded.length - 1; i >= 0; i-- ) { + prevTile = drawer.tilesLoaded[ i ]; + + if ( prevTile.level <= drawer.cutoff || prevTile.beingDrawn ) { + continue; + } else if ( !worstTile ) { + worstTile = prevTile; + worstTileIndex = i; + continue; + } + + prevTime = prevTile.lastTouchTime; + worstTime = worstTile.lastTouchTime; + prevLevel = prevTile.level; + worstLevel = worstTile.level; + + if ( prevTime < worstTime || + ( prevTime == worstTime && prevLevel > worstLevel ) ) { + worstTile = prevTile; + worstTileIndex = i; + } + } + + if ( worstTile && worstTileIndex >= 0 ) { + worstTile.unload(); + insertionIndex = worstTileIndex; + } + + drawer.tilesLoaded[ insertionIndex ] = newTile; +} + function positionTile( tile, overlap, viewport, viewportCenter, levelVisibility ){ var boundsTL = tile.bounds.getTopLeft(), diff --git a/src/imageLoader.js b/src/imageLoader.js index 2e810d38..08ad67ab 100644 --- a/src/imageLoader.js +++ b/src/imageLoader.js @@ -142,7 +142,14 @@ $.ImageLoader.prototype = { else { this.jobQueue.push( newJob ); } + }, + /** + * Clear any unstarted image loading jobs from the queue. + * @method + */ + clear: function() { + this.jobQueue = []; } };