Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Moumita Mandal committed Dec 23, 2021
1 parent d6c537a commit c457dab
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 47 deletions.
6 changes: 1 addition & 5 deletions analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -986,11 +986,7 @@ class Analytics {
this.loadIntegration = !!options.loadIntegration;
}

this.eventRepository.writeKey = writeKey;
if (serverUrl) {
this.eventRepository.url = serverUrl;
}
this.eventRepository.initialize(options);
this.eventRepository.initialize(writeKey, serverUrl, options);
this.initializeUser();
this.setInitialPageProperties();
this.loaded = true;
Expand Down
53 changes: 29 additions & 24 deletions utils/EventRepository.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-lonely-if */
/* eslint-disable class-methods-use-this */
import logger from "./logUtil";
import xhr from "./xhrModule";
import xhrQueue from "./xhrModule";
import beaconQueue from "./storage/beaconQueue";
import { getCurrentTimeFormatted } from "./utils";

Expand All @@ -21,25 +21,38 @@ class EventRepository {
constructor() {
this.writeKey = "";
this.url = "";
this.useBeacon = false;
this.queue = undefined;
}

initialize(options) {
if (options && options.useBeacon) this.useBeacon = options.useBeacon;
initialize(writeKey, url, options) {
let queueOptions = {};
if (
options &&
options.queueOptions &&
options.queueOptions != null &&
typeof options.queueOptions === "object"
) {
queueOptions = options.queueOptions;
}
if (this.useBeacon) {
beaconQueue.init(this.url, this.writeKey, queueOptions);
let targetUrl;
this.writeKey = writeKey;
this.url = url.slice(-1) === "/" ? url.slice(0, -1) : url;
if (options && options.useBeacon) {
if (
options &&
options.beaconQueue &&
options.beaconQueue != null &&
typeof options.beaconQueue === "object"
) {
queueOptions = options.beaconQueue;
}
targetUrl = `${this.url}/beacon/v1/batch`;
this.queue = beaconQueue;
} else {
xhr.startQueue(queueOptions);
if (
options &&
options.queueOptions &&
options.queueOptions != null &&
typeof options.queueOptions === "object"
) {
queueOptions = options.queueOptions;
}
targetUrl = this.url;
this.queue = xhrQueue;
}
this.queue.init(targetUrl, queueOptions, this.writeKey);
}

/**
Expand Down Expand Up @@ -68,15 +81,7 @@ class EventRepository {
);
}

// modify the url for event specific endpoints
const url = this.url.slice(-1) === "/" ? this.url.slice(0, -1) : this.url;
if (this.useBeacon) {
const targetUrl = `${url}/beacon/v1/batch`;
beaconQueue.enqueue(targetUrl, headers, message, this.writeKey);
} else {
// add items to the queue
xhr.enqueue(url, type, headers, message);
}
this.queue.enqueue(headers, message, type);
}
}
const eventRepository = new EventRepository();
Expand Down
23 changes: 12 additions & 11 deletions utils/storage/beaconQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ class BeaconQueue {
this.sendDataFromQueueAndDestroyQueue();
}

init(url, writekey, options) {
init(url, options, writekey) {
this.url = url;
this.writekey = writekey;
if (options.maxItems) this.maxItems = options.maxItems;
if (options.flushQueueInterval)
this.flushQueueTimeOutInterval = options.flushQueueInterval;
const sendQueueData = this.sendQueueDataForBeacon.bind(this);
const updatedUrl = url.slice(-1) == "/" ? url.slice(0, -1) : url;
const targetUrl = `${updatedUrl}/beacon/v1/batch`;
this.url = targetUrl;
this.writekey = writekey;
window.addEventListener("unload", sendQueueData);
}

Expand All @@ -58,7 +56,7 @@ class BeaconQueue {
return value;
}

enqueue(url, headers, message, writekey) {
enqueue(headers, message) {
let queue = this.getQueue() || [];
queue = queue.slice(-(this.maxItems - 1));
queue.push(message);
Expand All @@ -67,15 +65,15 @@ class BeaconQueue {
const dataToSend = JSON.stringify(data, this.replacer);
if (dataToSend.length > defaults.maxPayloadSize) {
batch = queue.slice(0, queue.length - 1);
this.flushQueue(headers, batch, url, writekey);
this.flushQueue(headers, batch);
queue = this.getQueue();
queue.push(message);
}
this.setQueue(queue);
this.setTimer();

if (queue.length === this.maxItems) {
this.flushQueue(headers, batch, url, writekey);
this.flushQueue(headers, batch);
}
}

Expand All @@ -89,19 +87,22 @@ class BeaconQueue {
if (queue && queue.length > 0) {
const batch = queue.slice(0, queue.length);
const headers = {};
this.flushQueue(headers, batch, this.url, this.writekey);
this.flushQueue(headers, batch);
}
}

flushQueue(headers, batch, url, writekey) {
flushQueue(headers, batch) {
headers.type = "application/json";
batch.map((event) => {
event.sentAt = new Date().toISOString();
});
const data = { batch };
const payload = JSON.stringify(data, this.replacer);
const blob = new Blob([payload], headers);
const isPushed = navigator.sendBeacon(`${url}?writeKey=${writekey}`, blob);
const isPushed = navigator.sendBeacon(
`${this.url}?writeKey=${this.writekey}`,
blob
);
if (!isPushed) {
logger.debug("Unable to send data");
}
Expand Down
19 changes: 12 additions & 7 deletions utils/xhrModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ const queueOptions = {
maxItems: 100,
};

class XHR {
startQueue(options) {
class XHRQueue {
constructor() {
this.url = "";
}

init(url, options) {
this.url = url;
if (options) {
// TODO: add checks for value - has to be +ve?
Object.assign(queueOptions, options);
Expand All @@ -31,7 +36,7 @@ class XHR {
item.message.sentAt = getCurrentTimeFormatted();
// send this item for processing, with a callback to enable queue to get the done status
// eslint-disable-next-line no-use-before-define
xhr.processQueueElement(
xhrQueue.processQueueElement(
item.url,
item.headers,
item.message,
Expand Down Expand Up @@ -96,15 +101,15 @@ class XHR {
}
}

enqueue(url, type, headers, message) {
enqueue(headers, message, type) {
// add items to the queue
this.payloadQueue.addItem({
url: `${url}/v1/${type}`,
url: `${this.url}/v1/${type}`,
headers,
message,
});
}
}

const xhr = new XHR();
export default xhr;
const xhrQueue = new XHRQueue();
export default xhrQueue;

0 comments on commit c457dab

Please sign in to comment.