Skip to content

Commit

Permalink
Create tryOnRequestFailure function to reduce filesize
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-el committed Oct 27, 2023
1 parent 98c2f9d commit b1ab390
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions libraries/browser-tracker-core/src/tracker/out_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,13 @@ export function OutQueueManager(
return typeof queue[0] === 'object' && 'evt' in queue[0];
};

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

Expand All @@ -236,7 +235,7 @@ export function OutQueueManager(
// so we must set them here to listen for the response
const xhrTimeout = setTimeout(function () {
xhr.abort();
onRequestFailureTimeout();
tryOnRequestFailure(0, 'timeout');
}, connectionTimeout);

xhr.onreadystatechange = function () {
Expand All @@ -245,11 +244,7 @@ export function OutQueueManager(
if (xhr.status >= 200 && xhr.status < 300) {
onRequestSuccess?.(currentBatch.slice());
} else {
onRequestFailure?.({
events: currentBatch.slice(),
status: xhr.status,
message: xhr.statusText,
});
tryOnRequestFailure(xhr.status, xhr.statusText);
}
currentBatch.length = 0;
}
Expand Down Expand Up @@ -369,7 +364,7 @@ export function OutQueueManager(
// Time out POST requests after connectionTimeout
const xhrTimeout = setTimeout(function () {
xhr.abort();
onRequestFailureTimeout();
tryOnRequestFailure(0, 'timeout');
executingQueue = false;
}, connectionTimeout);

Expand Down Expand Up @@ -398,11 +393,7 @@ export function OutQueueManager(
} else {
if (!shouldRetryForStatusCode(xhr.status)) {
LOG.error(`Status ${xhr.status}, will not retry.`);
onRequestFailure?.({
events: currentBatch.slice(),
status: xhr.status,
message: xhr.statusText,
});
tryOnRequestFailure(xhr.status, xhr.statusText);
removeEventsFromQueue(numberToSend);
}
executingQueue = false;
Expand Down

0 comments on commit b1ab390

Please sign in to comment.