Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequence numbers for requests #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 23 additions & 27 deletions public/script/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ var reqCorrelationQuery = null;
var curRecords = null;
var curOverlay = null;

var inFlightRequest = null;
var nextSeqNum = 1;
var displayingSeqNum = 0;


// http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript
Number.prototype.formatNice = function(c, d, t){
Expand Down Expand Up @@ -308,49 +310,43 @@ function clearCorr() {

// ==== Fetching requested data

function handleNewData(oEvent) {
function handleNewData(oEvent, seqNum) {
// ignore out of order responses
if(seqNum <= displayingSeqNum) return;
displayingSeqNum = seqNum;

var arrayBuffer = oEvent.target.response; // Note: not oReq.responseText
if (arrayBuffer) {
curRecords = readBtsfFile(arrayBuffer);
redisplay();
} else {
console.log("Couldn't fetch file " + url);
}

// TODO: maybe instead of cancelling in flight requests when new ones are sent out
// perhaps we can just always prefer results from requests that were *sent* more recently
// even if they arrive earlier due to weird networking things.
inFlightRequest = null;
}

function fetchCorrelationData(corrBuffer, filter) {
if(inFlightRequest !== null) inFlightRequest.abort();
var xhr = new XMLHttpRequest();
xhr.open("POST", "/find?"+encodeURIComponent(filter), true);
xhr.responseType = "arraybuffer";
xhr.onload = handleNewData;
xhr.send(new DataView(corrBuffer));
inFlightRequest = xhr;
}
function fetchData(filter, corrBuffer) {
var thisRequest = nextSeqNum;
nextSeqNum += 1;

function fetchRawData(filter) {
if(inFlightRequest !== null) inFlightRequest.abort();
var xhr = new XMLHttpRequest();
xhr.open("GET", "/raw?"+encodeURIComponent(filter), true);
var endpoint = (corrBuffer !== null) ? "/find?" : "/raw?";
xhr.open("POST", endpoint+encodeURIComponent(filter), true);
xhr.responseType = "arraybuffer";
xhr.onload = handleNewData;
xhr.send(null);
inFlightRequest = xhr;
}
xhr.onload = function(oEvent) {
handleNewData(oEvent, thisRequest);
}

function updateFromServer() {
if(reqCorrelationQuery !== null) {
fetchCorrelationData(reqCorrelationQuery, reqFilter);
if(corrBuffer !== null) {
xhr.send(new DataView(corrBuffer));
} else {
fetchRawData(reqFilter);
xhr.send(null);
}
}

function updateFromServer() {
fetchData(reqFilter, reqCorrelationQuery);
}

function init() {
updateFromServer();
document.getElementById("filter-box").focus();
Expand Down