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 24, 2021
1 parent 8085b1a commit e68d485
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
13 changes: 2 additions & 11 deletions utils/EventRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ class EventRepository {
* @memberof EventRepository
*/
constructor() {
this.writeKey = "";
this.queue = undefined;
}

initialize(writeKey, url, options) {
let queueOptions = {};
let targetUrl = url.slice(-1) === "/" ? url.slice(0, -1) : url;
this.writeKey = writeKey;
if (options && options.useBeacon) {
if (
options &&
Expand All @@ -49,7 +47,7 @@ class EventRepository {
}
this.queue = new XHRQueue();
}
this.queue.init(targetUrl, queueOptions, this.writeKey);
this.queue.init(writeKey, targetUrl, queueOptions);
}

/**
Expand All @@ -60,13 +58,6 @@ class EventRepository {
*/
enqueue(rudderElement, type) {
const message = rudderElement.getElementContent();

const headers = {
"Content-Type": "application/json",
Authorization: `Basic ${btoa(`${this.writeKey}:`)}`,
AnonymousId: btoa(message.anonymousId),
};

message.originalTimestamp = getCurrentTimeFormatted();
message.sentAt = getCurrentTimeFormatted(); // add this, will get modified when actually being sent

Expand All @@ -78,7 +69,7 @@ class EventRepository {
);
}

this.queue.enqueue(headers, message, type);
this.queue.enqueue(message, type);
}
}
const eventRepository = new EventRepository();
Expand Down
16 changes: 7 additions & 9 deletions utils/storage/beaconQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BeaconQueue {
this.sendDataFromQueueAndDestroyQueue();
}

init(url, options, writekey) {
init(writekey, url, options) {
this.url = url;
this.writekey = writekey;
if (options.maxItems) this.maxItems = options.maxItems;
Expand Down Expand Up @@ -56,7 +56,7 @@ class BeaconQueue {
return value;
}

enqueue(headers, message) {
enqueue(message) {
let queue = this.getQueue() || [];
queue = queue.slice(-(this.maxItems - 1));
queue.push(message);
Expand All @@ -65,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);
this.flushQueue(batch);
queue = this.getQueue();
queue.push(message);
}
this.setQueue(queue);
this.setTimer();

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

Expand All @@ -86,19 +86,17 @@ class BeaconQueue {
const queue = this.getQueue();
if (queue && queue.length > 0) {
const batch = queue.slice(0, queue.length);
const headers = {};
this.flushQueue(headers, batch);
this.flushQueue(batch);
}
}

flushQueue(headers, batch) {
headers.type = "application/json";
flushQueue(batch) {
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 blob = new Blob([payload], { type: "application/json" });
const isPushed = navigator.sendBeacon(
`${this.url}?writeKey=${this.writekey}`,
blob
Expand Down
11 changes: 9 additions & 2 deletions utils/xhrModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ const queueOptions = {
class XHRQueue {
constructor() {
this.url = "";
this.writeKey = "";
}

init(url, options) {
init(writeKey, url, options) {
this.url = url;
this.writeKey = writeKey;
if (options) {
// TODO: add checks for value - has to be +ve?
Object.assign(queueOptions, options);
Expand Down Expand Up @@ -102,7 +104,12 @@ class XHRQueue {
}
}

enqueue(headers, message, type) {
enqueue(message, type) {
const headers = {
"Content-Type": "application/json",
Authorization: `Basic ${btoa(`${this.writeKey}:`)}`,
AnonymousId: btoa(message.anonymousId),
};
// add items to the queue
this.payloadQueue.addItem({
url: `${this.url}/v1/${type}`,
Expand Down

0 comments on commit e68d485

Please sign in to comment.