forked from glenrobertson/leaflet-tilelayer-geojson
-
Notifications
You must be signed in to change notification settings - Fork 5
/
TileRequest.js
55 lines (49 loc) · 1.56 KB
/
TileRequest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
L.TileRequest = function(evented, ajax) {
this._evented = evented;
this._ajax = ajax;
};
L.TileRequest.prototype = {
process: function (tile, done) {
this._evented.fire('tilerequest', {tile: tile});
tile._request = this._ajax(tile.url, this._xhrHandler(tile, done));
},
// XMLHttpRequest handler; closure over the XHR object, the layer, and the tile
_xhrHandler: function (tile, done) {
return L.bind(function(err, data) {
if (!err) {
// check if request is about to be aborted, avoid rare error when aborted while parsing
if (tile._request) {
tile._request = null;
this._evented.fire('tileresponse', {tile: tile});
tile.datum = data;
done(null, tile);
}
} else {
tile.loading = false;
tile._request = null;
this._evented.fire('tileerror', {tile: tile});
done(err, tile);
}
}, this);
},
abort: function(tile) {
var req = tile._request;
if (req) {
tile._request = null;
req.abort();
this._evented.fire('tilerequestabort', {tile: tile});
}
}
};
L.tileRequest = function(evented, ajax) {
return new L.TileRequest(evented, ajax);
};
// dummy request (= no request)
L.noRequest = function () {
return {
process: function (tile, done) {
return done(null, tile);
},
abort: function() {}
};
};