Prune all except for the most recent z on URL change

This commit is contained in:
Tim Schaub
2017-10-07 10:29:40 -06:00
parent a4e0c54200
commit 97745c50af
3 changed files with 60 additions and 0 deletions

View File

@@ -155,6 +155,7 @@ ol.source.UrlTile.prototype.setTileLoadFunction = function(tileLoadFunction) {
*/
ol.source.UrlTile.prototype.setTileUrlFunction = function(tileUrlFunction, opt_key) {
this.tileUrlFunction = tileUrlFunction;
this.tileCache.pruneExceptNewestZ();
if (typeof opt_key !== 'undefined') {
this.setKey(opt_key);
} else {

View File

@@ -2,6 +2,7 @@ goog.provide('ol.TileCache');
goog.require('ol');
goog.require('ol.structs.LRUCache');
goog.require('ol.tilecoord');
/**
@@ -33,3 +34,22 @@ ol.TileCache.prototype.expireCache = function(usedTiles) {
}
}
};
/**
* Prune all tiles from the cache that don't have the same z as the newest tile.
*/
ol.TileCache.prototype.pruneExceptNewestZ = function() {
if (this.getCount() === 0) {
return;
}
var key = this.peekFirstKey();
var tileCoord = ol.tilecoord.fromKey(key);
var z = tileCoord[0];
this.forEach(function(tile) {
if (tile.tileCoord[0] !== z) {
this.remove(ol.tilecoord.getKey(tile.tileCoord));
tile.dispose();
}
}, this);
};

View File

@@ -0,0 +1,39 @@
goog.require('ol.Tile');
goog.require('ol.TileCache');
goog.require('ol.tilecoord');
describe('ol.TileCache', function() {
describe('#pruneExceptNewestZ()', function() {
it('gets rid of all entries that are not at the newest z', function() {
var tiles = [
new ol.Tile([0, 0, 0]),
new ol.Tile([1, 0, 0]),
new ol.Tile([1, 1, 0]),
new ol.Tile([2, 0, 0]),
new ol.Tile([2, 1, 0]),
new ol.Tile([2, 2, 0]),
new ol.Tile([2, 3, 0]) // newest tile at z: 2
];
var cache = new ol.TileCache();
sinon.spy(tiles[0], 'dispose');
tiles.forEach(function(tile) {
cache.set(ol.tilecoord.getKey(tile.tileCoord), tile);
});
cache.pruneExceptNewestZ();
expect(cache.getKeys()).to.eql([
'2/3/0',
'2/2/0',
'2/1/0',
'2/0/0'
]);
expect(tiles[0].dispose.calledOnce).to.be(true);
});
});
});