Cleaned up tileCache code.

This commit is contained in:
Ian Gilman 2014-08-08 16:32:43 -07:00
parent f04d538b5b
commit 94080c3180

View File

@ -34,6 +34,14 @@
(function( $ ){ (function( $ ){
var TileRecord = function( params ) {
$.console.assert( params, "[TileCache.cacheTile] params is required" );
$.console.assert( params.tile, "[TileCache.cacheTile] params.tile is required" );
$.console.assert( params.tiledImage, "[TileCache.cacheTile] params.tiledImage is required" );
this.tile = params.tile;
this.tiledImage = params.tiledImage;
};
/** /**
* @class TileCache * @class TileCache
* @classdesc * @classdesc
@ -56,17 +64,22 @@ $.TileCache.prototype = /** @lends OpenSeadragon.TileCache.prototype */{
return this._tilesLoaded.length; return this._tilesLoaded.length;
}, },
cacheTile: function( tile, cutoff ) { cacheTile: function( params ) {
cutoff = cutoff || 0; $.console.assert( params, "[TileCache.cacheTile] params is required" );
$.console.assert( params.tile, "[TileCache.cacheTile] params.tile is required" );
$.console.assert( params.tiledImage, "[TileCache.cacheTile] params.tiledImage is required" );
var cutoff = params.cutoff || 0;
var insertionIndex = this._tilesLoaded.length; var insertionIndex = this._tilesLoaded.length;
if ( this._tilesLoaded.length >= this._maxImageCacheCount ) { if ( this._tilesLoaded.length >= this._maxImageCacheCount ) {
var worstTile = null; var worstTile = null;
var worstTileIndex = -1; var worstTileIndex = -1;
var prevTile, worstTime, worstLevel, prevTime, prevLevel; var prevTile, worstTime, worstLevel, prevTime, prevLevel, prevTileRecord;
for ( var i = this._tilesLoaded.length - 1; i >= 0; i-- ) { for ( var i = this._tilesLoaded.length - 1; i >= 0; i-- ) {
prevTile = this._tilesLoaded[ i ]; prevTileRecord = this._tilesLoaded[ i ];
prevTile = prevTileRecord.tile;
if ( prevTile.level <= cutoff || prevTile.beingDrawn ) { if ( prevTile.level <= cutoff || prevTile.beingDrawn ) {
continue; continue;
@ -94,19 +107,26 @@ $.TileCache.prototype = /** @lends OpenSeadragon.TileCache.prototype */{
} }
} }
this._tilesLoaded[ insertionIndex ] = tile; this._tilesLoaded[ insertionIndex ] = new TileRecord({
tile: params.tile,
tiledImage: params.tiledImage
});
}, },
/** /**
* Clears all tiles. * Clears all tiles associated with the specified tiledImage.
* @method * @method
*/ */
reset: function() { clearTilesFor: function( tiledImage ) {
var tileRecord;
for ( var i = 0; i < this._tilesLoaded.length; ++i ) { for ( var i = 0; i < this._tilesLoaded.length; ++i ) {
this._tilesLoaded[i].unload(); tileRecord = this._tilesLoaded[ i ];
if ( tileRecord.tiledImage === tiledImage ) {
tileRecord.tile.unload();
this._tilesLoaded.splice( i, 1 );
i--;
}
} }
this._tilesLoaded = [];
} }
}; };