Prune all except for the most recent z on URL change
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
39
test/spec/ol/tilecache.test.js
Normal file
39
test/spec/ol/tilecache.test.js
Normal 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);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user