ingress-intel-total-conversion/code/request_handling.js
Stefan Breunig 360f6f8336 rework chat handling completely
The old system was getting too complicated to add new features to.
The new one should be easier to understand and will be explained
below. This patch should fix #140 and fix #16.

There are several places where new data requests may come from:
- chat.needMoreMessages checks if we are scrolled too far up or
  there is no scrollbar. It then requests new messages.
- scrolling up
- switching the chat tab via chat.chooser
- auto refresh
- once on boot
The latter two always request both types of chat.

Faction and public are handled the same way. Public is later split
into categories. I will explain the faction chat here because it’s
easier.

It starts in chat.requestFaction. This will initialize the query to
the server for data. The exact query is determined by chat.
genPostData. There can only be one faction chat request at a time.
Clearing on map move is handled automatically in code/requests.js.

Let’s assume the data query is successful. It is now passed to
chat.handleFaction which runs some basic checks before the data is
processed and stored in chat._factionData. This is done by chat.
writeDataToHash. The hash keys are the message GUIDs to avoid
duplicates. The hash values are an array of timestamp, processed
message and other meta data.

There is only one way to render faction chat, so data is directly
handed to chat.renderFaction which immediately hands it to chat
.renderData. renderData discards the GUIDs and sorts the values
by their timestamp. It then iterates over the data to insert
date break lines and finally renders the data in the DOM. It also
does some magic to keep the correct scrolling position.

Chat data will be cleared on map move or zoom. However, the
display is not refreshed, so the old, invalid chat data is still
displayed. Only once the data query is finished the data is
rendered. The upside is that small map operations within your
local area appear to never lose their chat data. Downside is that
there’s invalid chat for some time when changing cities.
2013-02-13 02:52:27 +01:00

114 lines
3.4 KiB
JavaScript

// REQUEST HANDLING //////////////////////////////////////////////////
// note: only meant for portal/links/fields request, everything else
// does not count towards “loading”
window.activeRequests = [];
window.failedRequestCount = 0;
window.requests = function() {}
window.requests.add = function(ajax) {
window.activeRequests.push(ajax);
renderUpdateStatus();
}
window.requests.remove = function(ajax) {
window.activeRequests.splice(window.activeRequests.indexOf(ajax), 1);
renderUpdateStatus();
}
window.requests.abort = function() {
$.each(window.activeRequests, function(ind, actReq) {
if(actReq) actReq.abort();
});
window.activeRequests = [];
window.failedRequestCount = 0;
window.chat._requestPublicRunning = false;
window.chat._requestFactionRunning = false;
renderUpdateStatus();
}
// gives user feedback about pending operations. Draws current status
// to website. Updates info in layer chooser.
window.renderUpdateStatus = function() {
var t = '<b>map status:</b> ';
if(mapRunsUserAction)
t += 'paused during interaction';
else if(isIdle())
t += '<span style="color:red">Idle, not updating.</span>';
else if(window.activeRequests.length > 0)
t += window.activeRequests.length + ' requests running.';
else
t += 'Up to date.';
if(renderLimitReached())
t += ' <span style="color:red" class="help" title="Can only render so much before it gets unbearably slow. Not all entities are shown. Zoom in or increase the limit (search for MAX_DRAWN_*).">RENDER LIMIT</span> '
if(window.failedRequestCount > 0)
t += ' <span style="color:red">' + window.failedRequestCount + ' failed</span>.'
t += '<br/>(';
var minlvl = getMinPortalLevel();
if(minlvl === 0)
t += 'loading all portals';
else
t+= 'only loading portals with level '+minlvl+' and up';
t += ')';
var portalSelection = $('.leaflet-control-layers-overlays label');
portalSelection.slice(0, minlvl+1).addClass('disabled').attr('title', 'Zoom in to show those.');
portalSelection.slice(minlvl, 8).removeClass('disabled').attr('title', '');
$('#updatestatus').html(t);
}
// sets the timer for the next auto refresh. Ensures only one timeout
// is queued. May be given 'override' in milliseconds if time should
// not be guessed automatically. Especially useful if a little delay
// is required, for example when zooming.
window.startRefreshTimeout = function(override) {
// may be required to remove 'paused during interaction' message in
// status bar
window.renderUpdateStatus();
if(refreshTimeout) clearTimeout(refreshTimeout);
var t = 0;
if(override) {
t = override;
} else {
t = REFRESH*1000;
var adj = ZOOM_LEVEL_ADJ * (18 - window.map.getZoom());
if(adj > 0) t += adj*1000;
}
var next = new Date(new Date().getTime() + t).toLocaleTimeString();
console.log('planned refresh: ' + next);
refreshTimeout = setTimeout(window.requests._callOnRefreshFunctions, t);
}
window.requests._onRefreshFunctions = [];
window.requests._callOnRefreshFunctions = function() {
startRefreshTimeout();
if(isIdle()) {
console.log('user has been idle for ' + idleTime + ' minutes. Skipping refresh.');
renderUpdateStatus();
return;
}
console.log('refreshing');
$.each(window.requests._onRefreshFunctions, function(ind, f) {
f();
});
}
// add method here to be notified of auto-refreshes
window.requests.addRefreshFunction = function(f) {
window.requests._onRefreshFunctions.push(f);
}