Skip to content

Commit

Permalink
Remove "global" currentBatch variable
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-el committed Nov 15, 2023
1 parent 4308129 commit 7921c4e
Showing 1 changed file with 13 additions and 30 deletions.
43 changes: 13 additions & 30 deletions libraries/browser-tracker-core/src/tracker/out_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ export function OutQueueManager(
bytes: number;
};

// A reference to the current batch of events being sent
// This is copied into the onRequestSuccess and onRequestFailure callbacks when called,
// and then cleared after the callbacks are run
let currentBatch: EventBatch = [];

let executingQueue = false,
configCollectorUrl: string,
outQueue: Array<PostEvent> | Array<string> = [],
Expand Down Expand Up @@ -211,13 +206,12 @@ export function OutQueueManager(
};

// Runs `onRequestFailure` callback if defined, and clears the current batch
const tryOnRequestFailure = (status: number, message: string) => {
const tryOnRequestFailure = (status: number, message: string, batch: EventBatch) => {
onRequestFailure?.({
events: currentBatch.slice(),
events: batch,
status,
message,
});
currentBatch.length = 0;
};

/**
Expand All @@ -228,11 +222,10 @@ export function OutQueueManager(
function sendPostRequestWithoutQueueing(body: PostEvent, configCollectorUrl: string) {
const xhr = initializeXMLHttpRequest(configCollectorUrl, true, false);
const batch = attachStmToEvent([body.evt]);
currentBatch = batch as PostBatch;

// The timeout and onreadystatechange events are not set for oversized requests
// so we must set them here to listen for the response
setXhrCallbacks(xhr, 0);
setXhrCallbacks(xhr, 0, batch);

xhr.send(encloseInPayloadDataEnvelope(batch));
}
Expand All @@ -246,30 +239,29 @@ export function OutQueueManager(
}
};

function setXhrCallbacks(xhr: XMLHttpRequest, numberToSend: number) {
function setXhrCallbacks(xhr: XMLHttpRequest, numberToSend: number, batch: EventBatch) {
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
clearTimeout(xhrTimeout);
if (xhr.status >= 200 && xhr.status < 300) {
onRequestSuccess?.(currentBatch.slice());
onRequestSuccess?.(batch);
removeEventsFromQueue(numberToSend);
executeQueue();
} else {
if (!shouldRetryForStatusCode(xhr.status)) {
LOG.error(`Status ${xhr.status}, will not retry.`);
tryOnRequestFailure(xhr.status, xhr.statusText);
tryOnRequestFailure(xhr.status, xhr.statusText, batch);
removeEventsFromQueue(numberToSend);
}
executingQueue = false;
}
currentBatch.length = 0;
}
};

// Time out POST requests after connectionTimeout
const xhrTimeout = setTimeout(function () {
xhr.abort();
tryOnRequestFailure(0, 'timeout');
tryOnRequestFailure(0, 'timeout', batch);
executingQueue = false;
}, connectionTimeout);
}
Expand Down Expand Up @@ -384,17 +376,9 @@ export function OutQueueManager(
numberToSend = 1;
}

setXhrCallbacks(xhr, numberToSend);

// The events (`numberToSend` of them), have been sent, so we remove them from the outQueue
// We also call executeQueue() again, to let executeQueue() check if we should keep running through the queue
const onPostSuccess = (numberToSend: number): void => {
removeEventsFromQueue(numberToSend);
executeQueue();
};

if (!postable(outQueue)) {
// If not postable then it's a GET so just send it
setXhrCallbacks(xhr, numberToSend, [outQueue[0]]);
xhr.send();
} else {
let batch = outQueue.slice(0, numberToSend);
Expand All @@ -420,10 +404,12 @@ export function OutQueueManager(
// When beaconStatus is true, we can't _guarantee_ that it was successful (beacon queues asynchronously)
// but the browser has taken it out of our hands, so we want to flush the queue assuming it will do its job
if (beaconStatus === true) {
onPostSuccess(numberToSend);
removeEventsFromQueue(numberToSend);
executeQueue();
onRequestSuccess?.(batch);
} else {
const batch = attachStmToEvent(eventBatch);
currentBatch = eventBatch;
setXhrCallbacks(xhr, numberToSend, batch);
xhr.send(encloseInPayloadDataEnvelope(batch));
}
}
Expand Down Expand Up @@ -539,10 +525,7 @@ export function OutQueueManager(
* @param nextRequest - the query string of the next request
*/
function createGetUrl(nextRequest: string) {
const getUrl =
configCollectorUrl + (useStm ? nextRequest.replace('?', '?stm=' + new Date().getTime() + '&') : nextRequest);
currentBatch = [getUrl];
return getUrl;
return configCollectorUrl + (useStm ? nextRequest.replace('?', '?stm=' + new Date().getTime() + '&') : nextRequest);
}

return {
Expand Down

0 comments on commit 7921c4e

Please sign in to comment.