Merge pull request #3895 from planetlabs/greedy-multi-queue
Rework the tile queue for multiple queues.
This commit is contained in:
@@ -89,14 +89,17 @@ ol.TileQueue.prototype.handleTileChange = function(event) {
|
|||||||
* @param {number} maxNewLoads Maximum number of new tiles to load.
|
* @param {number} maxNewLoads Maximum number of new tiles to load.
|
||||||
*/
|
*/
|
||||||
ol.TileQueue.prototype.loadMoreTiles = function(maxTotalLoading, maxNewLoads) {
|
ol.TileQueue.prototype.loadMoreTiles = function(maxTotalLoading, maxNewLoads) {
|
||||||
var newLoads = Math.min(
|
var newLoads = 0;
|
||||||
maxTotalLoading - this.getTilesLoading(), maxNewLoads, this.getCount());
|
var tile;
|
||||||
var i, tile;
|
while (this.tilesLoading_ < maxTotalLoading && newLoads < maxNewLoads &&
|
||||||
for (i = 0; i < newLoads; ++i) {
|
this.getCount() > 0) {
|
||||||
tile = /** @type {ol.Tile} */ (this.dequeue()[0]);
|
tile = /** @type {ol.Tile} */ (this.dequeue()[0]);
|
||||||
goog.events.listen(tile, goog.events.EventType.CHANGE,
|
if (tile.getState() === ol.TileState.IDLE) {
|
||||||
this.handleTileChange, false, this);
|
goog.events.listen(tile, goog.events.EventType.CHANGE,
|
||||||
tile.load();
|
this.handleTileChange, false, this);
|
||||||
|
tile.load();
|
||||||
|
++this.tilesLoading_;
|
||||||
|
++newLoads;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.tilesLoading_ += newLoads;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -13,6 +13,73 @@ describe('ol.TileQueue', function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var tileId = 0;
|
||||||
|
function createImageTile() {
|
||||||
|
++tileId;
|
||||||
|
var tileCoord = [tileId, tileId, tileId];
|
||||||
|
var state = ol.TileState.IDLE;
|
||||||
|
var src = 'data:image/gif;base64,R0lGODlhAQABAPAAAP8AAP///' +
|
||||||
|
'yH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==#' + tileId;
|
||||||
|
|
||||||
|
return new ol.ImageTile(tileCoord, state, src, null,
|
||||||
|
ol.source.Image.defaultImageLoadFunction);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('#loadMoreTiles()', function() {
|
||||||
|
var noop = function() {};
|
||||||
|
|
||||||
|
it('works when tile queues share tiles', function(done) {
|
||||||
|
var q1 = new ol.TileQueue(noop, noop);
|
||||||
|
var q2 = new ol.TileQueue(noop, noop);
|
||||||
|
|
||||||
|
var numTiles = 20;
|
||||||
|
for (var i = 0; i < numTiles; ++i) {
|
||||||
|
var tile = createImageTile();
|
||||||
|
q1.enqueue([tile]);
|
||||||
|
q2.enqueue([tile]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initially, both have all tiles.
|
||||||
|
expect(q1.getCount()).to.equal(numTiles);
|
||||||
|
expect(q2.getCount()).to.equal(numTiles);
|
||||||
|
|
||||||
|
var maxLoading = numTiles / 2;
|
||||||
|
|
||||||
|
// and nothing is loading
|
||||||
|
expect(q1.getTilesLoading()).to.equal(0);
|
||||||
|
expect(q2.getTilesLoading()).to.equal(0);
|
||||||
|
|
||||||
|
// ask both to load
|
||||||
|
q1.loadMoreTiles(maxLoading, maxLoading);
|
||||||
|
q2.loadMoreTiles(maxLoading, maxLoading);
|
||||||
|
|
||||||
|
// both tiles will be loading the max
|
||||||
|
expect(q1.getTilesLoading()).to.equal(maxLoading);
|
||||||
|
expect(q2.getTilesLoading()).to.equal(maxLoading);
|
||||||
|
|
||||||
|
// the second queue will be empty now
|
||||||
|
expect(q1.getCount()).to.equal(numTiles - maxLoading);
|
||||||
|
expect(q2.getCount()).to.equal(0);
|
||||||
|
|
||||||
|
// let all tiles load
|
||||||
|
setTimeout(function() {
|
||||||
|
expect(q1.getTilesLoading()).to.equal(0);
|
||||||
|
expect(q2.getTilesLoading()).to.equal(0);
|
||||||
|
|
||||||
|
// ask both to load, this should clear q1
|
||||||
|
q1.loadMoreTiles(maxLoading, maxLoading);
|
||||||
|
q2.loadMoreTiles(maxLoading, maxLoading);
|
||||||
|
|
||||||
|
expect(q1.getCount()).to.equal(0);
|
||||||
|
expect(q2.getCount()).to.equal(0);
|
||||||
|
|
||||||
|
done();
|
||||||
|
}, 20);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
describe('heapify', function() {
|
describe('heapify', function() {
|
||||||
it('does convert an arbitrary array into a heap', function() {
|
it('does convert an arbitrary array into a heap', function() {
|
||||||
|
|
||||||
@@ -56,6 +123,9 @@ describe('ol.TileQueue', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
goog.require('ol.ImageTile');
|
||||||
goog.require('ol.Tile');
|
goog.require('ol.Tile');
|
||||||
|
goog.require('ol.TileState');
|
||||||
goog.require('ol.TileQueue');
|
goog.require('ol.TileQueue');
|
||||||
|
goog.require('ol.source.Image');
|
||||||
goog.require('ol.structs.PriorityQueue');
|
goog.require('ol.structs.PriorityQueue');
|
||||||
|
|||||||
Reference in New Issue
Block a user