more work on new render code. the new request class is now used - things are almost working right again
still need to change map refresh timer, add back resonators, and write a plugin for field MU count display
This commit is contained in:
@ -10,11 +10,11 @@ window.Request = function() {
|
||||
this.debugTiles = new RenderDebugTiles();
|
||||
|
||||
this.activeRequestCount = 0;
|
||||
this.requestedTiles = {};
|
||||
|
||||
this.MAX_REQUESTS = 4;
|
||||
this.MAX_TILES_PER_REQUEST = 12;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -27,7 +27,7 @@ window.Request.prototype.start = function() {
|
||||
|
||||
var bounds = clampLatLngBounds(map.getBounds());
|
||||
var zoom = getPortalDataZoom();
|
||||
var minPortalLevel = getPortalLevelForZoom(zoom);
|
||||
var minPortalLevel = getMinPortalLevelForZoom(zoom);
|
||||
|
||||
|
||||
this.render.startRenderPass(bounds);
|
||||
@ -35,27 +35,27 @@ window.Request.prototype.start = function() {
|
||||
|
||||
console.log('requesting data tiles at zoom '+zoom+' (L'+minPortalLevel+'+ portals), map zoom is '+map.getZoom());
|
||||
|
||||
var x1 = lngToTile(bounds.getWest(), z);
|
||||
var x2 = lngToTile(bounds.getEast(), z);
|
||||
var y1 = latToTile(bounds.getNorth(), z);
|
||||
var y2 = latToTile(bounds.getSouth(), z);
|
||||
|
||||
// fill tileBounds with the data needed to request each tile
|
||||
this.tileBounds = {};
|
||||
|
||||
var x1 = lngToTile(bounds.getWest(), zoom);
|
||||
var x2 = lngToTile(bounds.getEast(), zoom);
|
||||
var y1 = latToTile(bounds.getNorth(), zoom);
|
||||
var y2 = latToTile(bounds.getSouth(), zoom);
|
||||
|
||||
// y goes from left to right
|
||||
for (var y = y1; y <= y2; y++) {
|
||||
// x goes from bottom to top(?)
|
||||
for (var x = x1; x <= x2; x++) {
|
||||
var tile_id = pointToTileId(z, x, y);
|
||||
var latNorth = tileToLat(y,z);
|
||||
var latSouth = tileToLat(y+1,z);
|
||||
var lngWest = tileToLng(x,z);
|
||||
var lngEast = tileToLng(x+1,z);
|
||||
var tile_id = pointToTileId(zoom, x, y);
|
||||
var latNorth = tileToLat(y,zoom);
|
||||
var latSouth = tileToLat(y+1,zoom);
|
||||
var lngWest = tileToLng(x,zoom);
|
||||
var lngEast = tileToLng(x+1,zoom);
|
||||
|
||||
this.debugTiles.create(tile_id,[[latSouth,lngWest],[latNorth,lngEast]]);
|
||||
|
||||
var boundsParam = generateBoundsParams(
|
||||
var boundsParams = generateBoundsParams(
|
||||
tile_id,
|
||||
latSouth,
|
||||
lngWest,
|
||||
@ -63,7 +63,7 @@ window.Request.prototype.start = function() {
|
||||
lngEast
|
||||
);
|
||||
|
||||
this.tileBounds[tile_id] = boundsParam;
|
||||
this.tileBounds[tile_id] = boundsParams;
|
||||
|
||||
}
|
||||
}
|
||||
@ -71,22 +71,19 @@ window.Request.prototype.start = function() {
|
||||
|
||||
this.renderCachedTiles();
|
||||
|
||||
// if nothing left in the queue, end the render. otherwise, send network requests
|
||||
if (Object.keys(this.tileBounds).length == 0) {
|
||||
this.render.endRenderPass();
|
||||
// TODO: start timer for next refresh cycle here
|
||||
} else {
|
||||
this.processRequestQueue();
|
||||
}
|
||||
|
||||
this.processRequestQueue();
|
||||
}
|
||||
|
||||
|
||||
window.Request.prototype.renderCachedTiles = function() {
|
||||
|
||||
for (var tile_id in this.tileBounds) {
|
||||
if (this.cache.isFresh(tile_id) {
|
||||
if (this.cache.isFresh(tile_id)) {
|
||||
// fresh cache data
|
||||
|
||||
this.debugTiles.setState(tile_id, 'cache-fresh');
|
||||
|
||||
var data = this.cache.get(tile_id);
|
||||
// render it...
|
||||
// TODO?? make rendering sort-of asynchronous? rendering a large number of dense tiles is slow - so perhaps use a timer and render one/a few at a time??
|
||||
@ -101,51 +98,108 @@ window.Request.prototype.renderCachedTiles = function() {
|
||||
|
||||
|
||||
window.Request.prototype.processRequestQueue = function() {
|
||||
console.log("Request.processRequestQueue...");
|
||||
|
||||
// if nothing left in the queue, end the render. otherwise, send network requests
|
||||
if (Object.keys(this.tileBounds).length == 0) {
|
||||
this.render.endRenderPass();
|
||||
// TODO: start timer for next refresh cycle here
|
||||
console.log("Request.processRequestQueue: ended");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// create a list of tiles that aren't requested over the network
|
||||
var pendingTiles = {};
|
||||
for (var id in this.tileBounds) {
|
||||
if (!(id in this.requestedTiles) ) {
|
||||
pendingTiles[id] = true;
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Request.processRequestQueue: "+Object.keys(pendingTiles).length+" tiles waiting");
|
||||
|
||||
|
||||
while (this.activeRequestCount < this.MAX_REQUESTS && Object.keys(pendingTiles).length > 0) {
|
||||
// let's distribute the requests evenly throughout the pending list.
|
||||
|
||||
var pendingTilesArray = Object.keys(pendingTiles);
|
||||
|
||||
var mod = Math.ceil(Object.keys(pendingTiles).length / this.MAX_TILES_PER_REQUEST);
|
||||
|
||||
var tiles = [];
|
||||
for (var i in pendingTilesArray) {
|
||||
if ((i % mod) == 0) {
|
||||
id = pendingTilesArray[i];
|
||||
tiles.push(id);
|
||||
delete pendingTiles[id];
|
||||
// if (tiles.length >= this.MAX_TILES_PER_REQUEST) {
|
||||
// break;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Request.processRequestQueue: asking for "+tiles.length+" tiles in one request");
|
||||
this.sendTileRequest(tiles);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
window.Request.prototype.sendTileRequest = function(tiles) {
|
||||
|
||||
var boundsParamList = [];
|
||||
var boundsParamsList = [];
|
||||
|
||||
for (var id in tiles) {
|
||||
var boundsParam = this.tileBounds[id];
|
||||
if (boundsParam) {
|
||||
push (boundsParamList, boundsParam);
|
||||
for (var i in tiles) {
|
||||
var id = tiles[i];
|
||||
|
||||
this.debugTiles.setState (id, 'requested');
|
||||
|
||||
this.requestedTiles[id] = true;
|
||||
|
||||
var boundsParams = this.tileBounds[id];
|
||||
if (boundsParams) {
|
||||
boundsParamsList.push (boundsParams);
|
||||
} else {
|
||||
console.warn('failed to find bounds for tile id '+id);
|
||||
}
|
||||
}
|
||||
|
||||
var data = { boundsParamList: boundsParamList };
|
||||
var data = { boundsParamsList: boundsParamsList };
|
||||
|
||||
this.activeRequestCount += 1;
|
||||
|
||||
var savedThis = this;
|
||||
|
||||
window.requests.add (window.postAjax('getThinnedEntitiesV4', data,
|
||||
function(data, textStatus, jqXHR) { this.handleResponse (data, tiles, true); }, // request successful callback
|
||||
function() { this.handleResponse (undefined, tiles, false); } // request failed callback
|
||||
function(data, textStatus, jqXHR) { savedThis.handleResponse (data, tiles, true); }, // request successful callback
|
||||
function() { savedThis.handleResponse (undefined, tiles, false); } // request failed callback
|
||||
));
|
||||
}
|
||||
|
||||
window.Request.prototype.requeueTile = function(id, error) {
|
||||
if (id in this.tileBounds) {
|
||||
// tile is currently wanted, requeue
|
||||
// tile is currently wanted...
|
||||
|
||||
if (error) {
|
||||
// if error is true, it was a 'bad' error - in this case we limit the number of retries (or prefer stale cached data?)
|
||||
var data = this.cache.get(id);
|
||||
if (data) {
|
||||
// we have cached data - use it, even though it's stale
|
||||
this.debugTiles.setState (id, 'cache-stale');
|
||||
this.render.processTileData (data);
|
||||
delete this.tileBounds[id];
|
||||
} else {
|
||||
// no cached data
|
||||
this.debugTiles.setState (id, 'error');
|
||||
}
|
||||
// and delete from the pending requests...
|
||||
delete this.tileBounds[id];
|
||||
|
||||
} else {
|
||||
// if false, was a 'timeout', so unlimited retries (as the stock site does)
|
||||
|
||||
this.debugTiles.setState (id, 'retrying');
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -155,13 +209,24 @@ window.Request.prototype.handleResponse = function (data, tiles, success) {
|
||||
|
||||
this.activeRequestCount -= 1;
|
||||
|
||||
for (var i in tiles) {
|
||||
var id = tiles[i];
|
||||
delete this.requestedTiles[id];
|
||||
}
|
||||
|
||||
|
||||
if (!success || !data || !data.result) {
|
||||
console.warn("Request.handleResponse: request failed - requeing...");
|
||||
|
||||
//request failed - requeue all the tiles(?)
|
||||
for (var id in tiles) {
|
||||
for (var i in tiles) {
|
||||
var id = tiles[i];
|
||||
this.requeueTile(id, true);
|
||||
}
|
||||
} else {
|
||||
|
||||
// TODO: use result.minLevelOfDetail ??? stock site doesn't use it yet...
|
||||
|
||||
var m = data.result.map;
|
||||
|
||||
for (var id in m) {
|
||||
@ -171,6 +236,7 @@ window.Request.prototype.handleResponse = function (data, tiles, success) {
|
||||
// server returned an error for this individual data tile
|
||||
|
||||
if (val.error == "TIMEOUT") {
|
||||
console.log('map data tile '+id+' returned TIMEOUT');
|
||||
// TIMEOUT errors for individual tiles are 'expected'(!) - and result in a silent unlimited retries
|
||||
this.requeueTile(id, false);
|
||||
} else {
|
||||
@ -179,6 +245,7 @@ window.Request.prototype.handleResponse = function (data, tiles, success) {
|
||||
}
|
||||
} else {
|
||||
// no error for this data tile - process it
|
||||
console.log('map data tile '+id+' is good...');
|
||||
|
||||
// store the result in the cache
|
||||
this.cache.store (id, val);
|
||||
@ -186,6 +253,8 @@ window.Request.prototype.handleResponse = function (data, tiles, success) {
|
||||
// if this tile was in the render list, render it
|
||||
// (requests aren't aborted when new requests are started, so it's entirely possible we don't want to render it!)
|
||||
if (id in this.tileBounds) {
|
||||
this.debugTiles.setState (id, 'ok');
|
||||
|
||||
this.render.processTileData (val);
|
||||
|
||||
delete this.tileBounds[id];
|
||||
@ -195,4 +264,5 @@ window.Request.prototype.handleResponse = function (data, tiles, success) {
|
||||
}
|
||||
}
|
||||
|
||||
this.processRequestQueue();
|
||||
}
|
||||
|
Reference in New Issue
Block a user