Merge pull request #7234 from ahocevar/tileload-events
Fix abort handling of tileload events
This commit is contained in:
@@ -13,7 +13,8 @@ ol.source.TileEventType = {
|
|||||||
TILELOADSTART: 'tileloadstart',
|
TILELOADSTART: 'tileloadstart',
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Triggered when a tile finishes loading.
|
* Triggered when a tile finishes loading, either when its data is loaded,
|
||||||
|
* or when loading was aborted because the tile is no longer needed.
|
||||||
* @event ol.source.Tile.Event#tileloadend
|
* @event ol.source.Tile.Event#tileloadend
|
||||||
* @api
|
* @api
|
||||||
*/
|
*/
|
||||||
|
|||||||
+20
-15
@@ -61,6 +61,12 @@ ol.source.UrlTile = function(options) {
|
|||||||
this.setTileUrlFunction(options.tileUrlFunction);
|
this.setTileUrlFunction(options.tileUrlFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
* @type {Object.<number, boolean>}
|
||||||
|
*/
|
||||||
|
this.tileLoadingKeys_ = {};
|
||||||
|
|
||||||
};
|
};
|
||||||
ol.inherits(ol.source.UrlTile, ol.source.Tile);
|
ol.inherits(ol.source.UrlTile, ol.source.Tile);
|
||||||
|
|
||||||
@@ -110,21 +116,20 @@ ol.source.UrlTile.prototype.getUrls = function() {
|
|||||||
*/
|
*/
|
||||||
ol.source.UrlTile.prototype.handleTileChange = function(event) {
|
ol.source.UrlTile.prototype.handleTileChange = function(event) {
|
||||||
var tile = /** @type {ol.Tile} */ (event.target);
|
var tile = /** @type {ol.Tile} */ (event.target);
|
||||||
switch (tile.getState()) {
|
var uid = ol.getUid(tile);
|
||||||
case ol.TileState.LOADING:
|
var tileState = tile.getState();
|
||||||
this.dispatchEvent(
|
var type;
|
||||||
new ol.source.Tile.Event(ol.source.TileEventType.TILELOADSTART, tile));
|
if (tileState == ol.TileState.LOADING) {
|
||||||
break;
|
this.tileLoadingKeys_[uid] = true;
|
||||||
case ol.TileState.LOADED:
|
type = ol.source.TileEventType.TILELOADSTART;
|
||||||
this.dispatchEvent(
|
} else if (uid in this.tileLoadingKeys_) {
|
||||||
new ol.source.Tile.Event(ol.source.TileEventType.TILELOADEND, tile));
|
delete this.tileLoadingKeys_[uid];
|
||||||
break;
|
type = tileState == ol.TileState.ERROR ? ol.source.TileEventType.TILELOADERROR :
|
||||||
case ol.TileState.ERROR:
|
(tileState == ol.TileState.LOADED || tileState == ol.TileState.ABORT) ?
|
||||||
this.dispatchEvent(
|
ol.source.TileEventType.TILELOADEND : undefined;
|
||||||
new ol.source.Tile.Event(ol.source.TileEventType.TILELOADERROR, tile));
|
}
|
||||||
break;
|
if (type != undefined) {
|
||||||
default:
|
this.dispatchEvent(new ol.source.Tile.Event(type, tile));
|
||||||
// pass
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
|
|
||||||
goog.require('ol.ImageTile');
|
goog.require('ol.ImageTile');
|
||||||
|
goog.require('ol.TileState');
|
||||||
goog.require('ol.TileUrlFunction');
|
goog.require('ol.TileUrlFunction');
|
||||||
goog.require('ol.events');
|
goog.require('ol.events');
|
||||||
goog.require('ol.proj');
|
goog.require('ol.proj');
|
||||||
@@ -172,4 +173,67 @@ describe('ol.source.TileImage', function() {
|
|||||||
tile.load();
|
tile.load();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('tile load events', function() {
|
||||||
|
|
||||||
|
var source;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
source = new ol.source.TileImage({
|
||||||
|
url: '{z}/{x}/{y}'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('dispatches tileloadstart and tileloadend events', function() {
|
||||||
|
source.setTileLoadFunction(function(tile) {
|
||||||
|
tile.setState(ol.TileState.LOADED);
|
||||||
|
});
|
||||||
|
var startSpy = sinon.spy();
|
||||||
|
source.on('tileloadstart', startSpy);
|
||||||
|
var endSpy = sinon.spy();
|
||||||
|
source.on('tileloadend', endSpy);
|
||||||
|
var tile = source.getTile(0, 0, -1, 1, ol.proj.get('EPSG:3857'));
|
||||||
|
tile.load();
|
||||||
|
expect(startSpy.callCount).to.be(1);
|
||||||
|
expect(endSpy.callCount).to.be(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('works for loading-error-loading-loaded sequences', function(done) {
|
||||||
|
source.setTileLoadFunction(function(tile) {
|
||||||
|
tile.setState(
|
||||||
|
tile.state == ol.TileState.ERROR ? ol.TileState.LOADED : ol.TileState.ERROR);
|
||||||
|
});
|
||||||
|
var startSpy = sinon.spy();
|
||||||
|
source.on('tileloadstart', startSpy);
|
||||||
|
var errorSpy = sinon.spy();
|
||||||
|
source.on('tileloaderror', function(e) {
|
||||||
|
setTimeout(function() {
|
||||||
|
e.tile.setState(ol.TileState.LOADING);
|
||||||
|
e.tile.setState(ol.TileState.LOADED);
|
||||||
|
}, 0);
|
||||||
|
errorSpy();
|
||||||
|
});
|
||||||
|
source.on('tileloadend', function() {
|
||||||
|
expect(startSpy.callCount).to.be(2);
|
||||||
|
expect(errorSpy.callCount).to.be(1);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
var tile = source.getTile(0, 0, -1, 1, ol.proj.get('EPSG:3857'));
|
||||||
|
tile.load();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('dispatches tileloadend events for aborted tiles', function() {
|
||||||
|
source.setTileLoadFunction(function() {});
|
||||||
|
var startSpy = sinon.spy();
|
||||||
|
source.on('tileloadstart', startSpy);
|
||||||
|
var endSpy = sinon.spy();
|
||||||
|
source.on('tileloadend', endSpy);
|
||||||
|
var tile = source.getTile(0, 0, -1, 1, ol.proj.get('EPSG:3857'));
|
||||||
|
tile.load();
|
||||||
|
tile.dispose();
|
||||||
|
expect(startSpy.callCount).to.be(1);
|
||||||
|
expect(endSpy.callCount).to.be(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user