diff --git a/src/ol/tile.js b/src/ol/tile.js index c9cf6bed73..ce00f9a1db 100644 --- a/src/ol/tile.js +++ b/src/ol/tile.js @@ -28,6 +28,13 @@ ol.Tile = function(tileCoord) { goog.base(this); + /** + * A count incremented each time the tile is inQueue in a tile queue, + * and decremented each time the tile is dequeued from a tile queue. + * @type {number} + */ + this.inQueue = 0; + /** * @type {ol.TileCoord} */ diff --git a/src/ol/tilequeue.js b/src/ol/tilequeue.js index c5788df972..7e20822308 100644 --- a/src/ol/tilequeue.js +++ b/src/ol/tilequeue.js @@ -89,6 +89,8 @@ ol.TileQueue.prototype.dequeue_ = function() { } var tileKey = tile.getKey(); delete this.queuedTileKeys_[tileKey]; + tile.inQueue--; + goog.asserts.assert(tile.inQueue >= 0); return tile; }; @@ -110,6 +112,8 @@ ol.TileQueue.prototype.enqueue = function(tile, tileSourceKey, tileCenter) { this.heap_.push([priority, tile, tileSourceKey, tileCenter]); this.queuedTileKeys_[tileKey] = true; this.siftDown_(0, this.heap_.length - 1); + tile.inQueue++; + goog.asserts.assert(tile.inQueue > 0); } } }; @@ -246,6 +250,11 @@ ol.TileQueue.prototype.reprioritize = function() { if (priority == ol.TileQueue.DROP) { tileKey = tile.getKey(); delete this.queuedTileKeys_[tileKey]; + tile.inQueue--; + goog.asserts.assert(tile.inQueue >= 0); + if (tile.inQueue === 0) { + goog.events.removeAll(tile); + } } else { node[0] = priority; heap[n++] = node; diff --git a/test/spec/ol/tilequeue.test.js b/test/spec/ol/tilequeue.test.js index 3ca38a2981..57db2b2f8c 100644 --- a/test/spec/ol/tilequeue.test.js +++ b/test/spec/ol/tilequeue.test.js @@ -21,13 +21,20 @@ describe('ol.TileQueue', function() { } function addRandomPriorityTiles(tq, num) { + var tiles = []; var i, tile, priority; for (i = 0; i < num; i++) { tile = new ol.Tile(); + tile.toDrop = (i % 2 === 0) ? true : false; + goog.events.listen(tile, goog.events.EventType.CHANGE, + goog.nullFunction); priority = Math.floor(Math.random() * 100); tq.heap_.push([priority, tile, '', new ol.Coordinate(0, 0)]); tq.queuedTileKeys_[tile.getKey()] = true; + tile.inQueue++; + tiles.push(tile); } + return tiles; } describe('heapify', function() { @@ -45,16 +52,15 @@ describe('ol.TileQueue', function() { it('does reprioritize the array', function() { var tq = new ol.TileQueue(function() {}); - addRandomPriorityTiles(tq, 100); + var tiles = addRandomPriorityTiles(tq, 100); tq.heapify_(); // now reprioritize, changing the priority of 50 tiles and removing the // rest - var i = 0; - tq.tilePriorityFunction_ = function() { - if ((i++) % 2 === 0) { + tq.tilePriorityFunction_ = function(tile) { + if (tile.toDrop) { return ol.TileQueue.DROP; } return Math.floor(Math.random() * 100); @@ -64,10 +70,23 @@ describe('ol.TileQueue', function() { expect(tq.heap_.length).toEqual(50); expect(isHeap(tq)).toBeTruthy(); + var i, tile; + for (i = 0; i < tiles.length; ++i) { + tile = tiles[i]; + var hasListener = goog.events.hasListener(tile); + if (tile.toDrop) { + expect(hasListener).toBeFalsy(); + } else { + expect(hasListener).toBeTruthy(); + } + } + }); }); }); +goog.require('goog.events'); +goog.require('goog.events.EventType'); goog.require('ol.Coordinate'); goog.require('ol.Tile'); goog.require('ol.TileQueue');