forked from glenrobertson/leaflet-tilelayer-geojson
-
Notifications
You must be signed in to change notification settings - Fork 5
/
TileQueue.js
60 lines (51 loc) · 1.83 KB
/
TileQueue.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
56
57
58
59
60
L.TileQueue = function(callback) {
this.callback = callback;
};
L.TileQueue.prototype = {
_queue: [],
_queueTimeout: null,
add: function(aTile) {
this._queue.push(aTile);
if (!this._queueTimeout) {
this._queueTimeout = setTimeout(L.bind(function(){
var time, timeout, start = +new Date, tile;
// handle empty elements, see remove
do {
tile = this._queue.shift();
}
while (!tile && this._queue.length > 0);
if (tile) {
//console.log('adding ' + tile.key + ' ...');
this.callback(tile);
// pause a percentage of adding time to keep UI responsive
time = +new Date - start;
timeout = Math.floor(time * 0.3);
//console.log('added ' + tile.key + ' (' + time + 'ms > ' + timeout + 'ms)');
this._queueTimeout = setTimeout(L.bind(arguments.callee, this), timeout);
} else {
this._queueTimeout = null;
}
}, this), 0);
}
},
remove: function(tile) {
var key = tile.key,
val;
for (var i = 0, len = this._queue.length; i < len; i++) {
val = this._queue[i];
if (val && val.key === key) {
//console.log('##### delete ' + key);
// set entry to undefined only for better performance (?) -
// queue consumer needs to handle empty entries!
delete this._queue[i];
}
}
},
clear: function() {
if (this._queueTimeout) {
clearTimeout(this._queueTimeout);
this._queueTimeout = null;
}
this._queue = [];
}
};