Skip to content

Commit

Permalink
Prune all except for the most recent z on URL change
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaub committed Oct 13, 2017
1 parent a4e0c54 commit 97745c5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ol/source/urltile.js
Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions src/ol/tilecache.js
Expand Up @@ -2,6 +2,7 @@ goog.provide('ol.TileCache');

goog.require('ol');
goog.require('ol.structs.LRUCache');
goog.require('ol.tilecoord');


/**
Expand Down Expand Up @@ -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 changes: 39 additions & 0 deletions test/spec/ol/tilecache.test.js
@@ -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);
});
});
});

0 comments on commit 97745c5

Please sign in to comment.