only clear and refresh past chat messages if the bounding box has changed by over 10% - reduces network requests for chat data after small map movements
This commit is contained in:
parent
468665f8d6
commit
508c2543f4
21
code/chat.js
21
code/chat.js
@ -41,16 +41,22 @@ window.chat._oldBBox = null;
|
|||||||
window.chat.genPostData = function(isFaction, storageHash, getOlderMsgs) {
|
window.chat.genPostData = function(isFaction, storageHash, getOlderMsgs) {
|
||||||
if(typeof isFaction !== 'boolean') throw('Need to know if public or faction chat.');
|
if(typeof isFaction !== 'boolean') throw('Need to know if public or faction chat.');
|
||||||
|
|
||||||
|
// get window bounds, and extend to the minimum chat radius
|
||||||
chat._localRangeCircle.setLatLng(map.getCenter());
|
chat._localRangeCircle.setLatLng(map.getCenter());
|
||||||
var b = map.getBounds().extend(chat._localRangeCircle.getBounds());
|
var b = map.getBounds().extend(chat._localRangeCircle.getBounds());
|
||||||
var ne = b.getNorthEast();
|
|
||||||
var sw = b.getSouthWest();
|
|
||||||
|
|
||||||
// round bounds in order to ignore rounding errors
|
// set a current bounding box if none set so far
|
||||||
var bbs = $.map([ne.lat, ne.lng, sw.lat, sw.lng], function(x) { return Math.round(x*1E4) }).join();
|
if (!chat._oldBBox) chat._oldBBox = b;
|
||||||
if(chat._oldBBox && chat._oldBBox !== bbs) {
|
|
||||||
|
// to avoid unnecessary chat refreshes, a small difference compared to the previous bounding box
|
||||||
|
// is not considered different
|
||||||
|
var CHAT_BOUNDINGBOX_SAME_FACTOR = 0.1;
|
||||||
|
// if the old and new box contain each other, after expanding by the factor, don't reset chat
|
||||||
|
if (!(b.pad(CHAT_BOUNDINGBOX_SAME_FACTOR).contains(chat._oldBBox) && chat._oldBBox.pad(CHAT_BOUNDINGBOX_SAME_FACTOR).contains(b))) {
|
||||||
|
console.log('Bounding Box changed, chat will be cleared (old: '+chat._oldBBox.toBBoxString()+'; new: '+b.toBBoxString()+')');
|
||||||
|
|
||||||
$('#chat > div').data('needsClearing', true);
|
$('#chat > div').data('needsClearing', true);
|
||||||
console.log('Bounding Box changed, chat will be cleared (old: '+chat._oldBBox+' ; new: '+bbs+' )');
|
|
||||||
// need to reset these flags now because clearing will only occur
|
// need to reset these flags now because clearing will only occur
|
||||||
// after the request is finished – i.e. there would be one almost
|
// after the request is finished – i.e. there would be one almost
|
||||||
// useless request.
|
// useless request.
|
||||||
@ -61,8 +67,9 @@ window.chat.genPostData = function(isFaction, storageHash, getOlderMsgs) {
|
|||||||
chat._public.data = {};
|
chat._public.data = {};
|
||||||
chat._public.oldestTimestamp = -1;
|
chat._public.oldestTimestamp = -1;
|
||||||
chat._public.newestTimestamp = -1;
|
chat._public.newestTimestamp = -1;
|
||||||
|
|
||||||
|
chat._oldBBox = b;
|
||||||
}
|
}
|
||||||
chat._oldBBox = bbs;
|
|
||||||
|
|
||||||
var ne = b.getNorthEast();
|
var ne = b.getNorthEast();
|
||||||
var sw = b.getSouthWest();
|
var sw = b.getSouthWest();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user