Merge pull request #437 from twpayne/throttle-image-loads

Throttle image requests
This commit is contained in:
Tom Payne
2013-04-05 07:24:13 -07:00
2 changed files with 27 additions and 7 deletions
+16 -2
View File
@@ -77,6 +77,18 @@ ol.ENABLE_DOM = true;
ol.ENABLE_WEBGL = true; ol.ENABLE_WEBGL = true;
/**
* @define {number} Maximum number of simultaneously loading tiles.
*/
ol.MAXIMUM_TILES_LOADING = 8;
/**
* @define {number} Maximum new tile loads per frame.
*/
ol.MAXIMUM_NEW_TILE_LOADS_PER_FRAME = 2;
/** /**
* @enum {string} * @enum {string}
*/ */
@@ -273,7 +285,9 @@ ol.Map = function(mapOptions) {
* @private * @private
* @type {ol.TileQueue} * @type {ol.TileQueue}
*/ */
this.tileQueue_ = new ol.TileQueue(goog.bind(this.getTilePriority, this), this.tileQueue_ = new ol.TileQueue(
ol.MAXIMUM_TILES_LOADING,
goog.bind(this.getTilePriority, this),
goog.bind(this.handleTileChange_, this)); goog.bind(this.handleTileChange_, this));
goog.events.listen(this, ol.Object.getChangedEventType(ol.MapProperty.VIEW), goog.events.listen(this, ol.Object.getChangedEventType(ol.MapProperty.VIEW),
@@ -555,7 +569,7 @@ ol.Map.prototype.handleMapBrowserEvent = function(mapBrowserEvent) {
*/ */
ol.Map.prototype.handlePostRender = function() { ol.Map.prototype.handlePostRender = function() {
this.tileQueue_.reprioritize(); // FIXME only call if needed this.tileQueue_.reprioritize(); // FIXME only call if needed
this.tileQueue_.loadMoreTiles(); this.tileQueue_.loadMoreTiles(ol.MAXIMUM_NEW_TILE_LOADS_PER_FRAME);
var postRenderFunctions = this.postRenderFunctions_; var postRenderFunctions = this.postRenderFunctions_;
var i; var i;
+11 -5
View File
@@ -18,12 +18,15 @@ ol.TilePriorityFunction;
/** /**
* @constructor * @constructor
* @extends {ol.structs.PriorityQueue} * @extends {ol.structs.PriorityQueue}
* @param {number} maxTilesLoading Maximum number of simultaneously loading
* tiles.
* @param {ol.TilePriorityFunction} tilePriorityFunction * @param {ol.TilePriorityFunction} tilePriorityFunction
* Tile priority function. * Tile priority function.
* @param {Function} tileChangeCallback * @param {Function} tileChangeCallback
* Function called on each tile change event. * Function called on each tile change event.
*/ */
ol.TileQueue = function(tilePriorityFunction, tileChangeCallback) { ol.TileQueue =
function(maxTilesLoading, tilePriorityFunction, tileChangeCallback) {
goog.base( goog.base(
this, this,
@@ -52,7 +55,7 @@ ol.TileQueue = function(tilePriorityFunction, tileChangeCallback) {
* @private * @private
* @type {number} * @type {number}
*/ */
this.maxTilesLoading_ = 8; this.maxTilesLoading_ = maxTilesLoading;
/** /**
* @private * @private
@@ -74,15 +77,18 @@ ol.TileQueue.prototype.handleTileChange = function() {
/** /**
* FIXME empty description for jsdoc * @param {number} limit Maximum number of new tiles to load.
*/ */
ol.TileQueue.prototype.loadMoreTiles = function() { ol.TileQueue.prototype.loadMoreTiles = function(limit) {
var tile; var tile;
while (!this.isEmpty() && this.tilesLoading_ < this.maxTilesLoading_) { while (limit > 0 &&
!this.isEmpty() &&
this.tilesLoading_ < this.maxTilesLoading_) {
tile = /** @type {ol.Tile} */ (this.dequeue()[0]); tile = /** @type {ol.Tile} */ (this.dequeue()[0]);
goog.events.listenOnce(tile, goog.events.EventType.CHANGE, goog.events.listenOnce(tile, goog.events.EventType.CHANGE,
this.handleTileChange, false, this); this.handleTileChange, false, this);
tile.load(); tile.load();
++this.tilesLoading_; ++this.tilesLoading_;
--limit;
} }
}; };