From ad3c6713da811659c2137460d09d0149192be7aa Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Wed, 9 Dec 2015 13:30:54 +0100 Subject: [PATCH 1/2] Return if the element is added to the ol.structs.PriorityQueue --- src/ol/structs/priorityqueue.js | 3 +++ test/spec/ol/structs/priorityqueue.test.js | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ol/structs/priorityqueue.js b/src/ol/structs/priorityqueue.js index e943796e34..a1ab4c946e 100644 --- a/src/ol/structs/priorityqueue.js +++ b/src/ol/structs/priorityqueue.js @@ -122,6 +122,7 @@ ol.structs.PriorityQueue.prototype.dequeue = function() { /** * Enqueue an element. O(log N). * @param {T} element Element. + * @return {boolean} The element was added to the queue. */ ol.structs.PriorityQueue.prototype.enqueue = function(element) { goog.asserts.assert(!(this.keyFunction_(element) in this.queuedElements_), @@ -132,7 +133,9 @@ ol.structs.PriorityQueue.prototype.enqueue = function(element) { this.priorities_.push(priority); this.queuedElements_[this.keyFunction_(element)] = true; this.siftDown_(0, this.elements_.length - 1); + return true; } + return false; }; diff --git a/test/spec/ol/structs/priorityqueue.test.js b/test/spec/ol/structs/priorityqueue.test.js index bacf751ad6..1c9aded427 100644 --- a/test/spec/ol/structs/priorityqueue.test.js +++ b/test/spec/ol/structs/priorityqueue.test.js @@ -28,14 +28,25 @@ describe('ol.structs.PriorityQueue', function() { }); it('enqueue adds an element', function() { - pq.enqueue(0); + var added = pq.enqueue(0); expect(function() { pq.assertValid(); }).not.to.throwException(); + expect(added).to.be(true); expect(pq.elements_).to.eql([0]); expect(pq.priorities_).to.eql([0]); }); + it('do not enqueue element with DROP priority', function() { + var added = pq.enqueue(Infinity); + expect(function() { + pq.assertValid(); + }).not.to.throwException(); + expect(added).to.be(false); + expect(pq.elements_).to.eql([]); + expect(pq.priorities_).to.eql([]); + }); + it('maintains the pq property while elements are enqueued', function() { var i; for (i = 0; i < 32; ++i) { From 7192644c07c1bb40269ac6453c04da65ec0b937e Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Wed, 9 Dec 2015 13:38:09 +0100 Subject: [PATCH 2/2] Register the change callback when the tile is enqueued --- src/ol/tilequeue.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/ol/tilequeue.js b/src/ol/tilequeue.js index 43a588a1aa..02e9221116 100644 --- a/src/ol/tilequeue.js +++ b/src/ol/tilequeue.js @@ -55,10 +55,30 @@ ol.TileQueue = function(tilePriorityFunction, tileChangeCallback) { */ this.tilesLoading_ = 0; + /** + * @private + * @type {Object.} + */ + this.tilesLoadingKeys_ = {}; + }; goog.inherits(ol.TileQueue, ol.structs.PriorityQueue); +/** + * @inheritDoc + */ +ol.TileQueue.prototype.enqueue = function(element) { + var added = goog.base(this, 'enqueue', element); + if (added) { + var tile = element[0]; + goog.events.listen(tile, goog.events.EventType.CHANGE, + this.handleTileChange, false, this); + } + return added; +}; + + /** * @return {number} Number of tiles loading. */ @@ -78,7 +98,11 @@ ol.TileQueue.prototype.handleTileChange = function(event) { state === ol.TileState.EMPTY) { goog.events.unlisten(tile, goog.events.EventType.CHANGE, this.handleTileChange, false, this); - --this.tilesLoading_; + var tileKey = tile.getKey(); + if (tileKey in this.tilesLoadingKeys_) { + delete this.tilesLoadingKeys_[tileKey]; + --this.tilesLoading_; + } this.tileChangeCallback_(); } }; @@ -95,9 +119,8 @@ ol.TileQueue.prototype.loadMoreTiles = function(maxTotalLoading, maxNewLoads) { this.getCount() > 0) { tile = /** @type {ol.Tile} */ (this.dequeue()[0]); if (tile.getState() === ol.TileState.IDLE) { - goog.events.listen(tile, goog.events.EventType.CHANGE, - this.handleTileChange, false, this); tile.load(); + this.tilesLoadingKeys_[tile.getKey()] = true; ++this.tilesLoading_; ++newLoads; }