@benshi.ai/js-sdk
Version:
Benshi SDK
147 lines • 5.77 kB
JavaScript
"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
var logging_1 = require("../logging");
var uuid_1 = require("uuid");
var BsSystem_1 = require("../drivers/BsSystem");
var package_json_1 = require("../../package.json");
var BsSender = /** @class */ (function () {
function BsSender(bsNetwork, options) {
var _this = this;
this.flushing = false;
this.flushIntervalId = 0;
this.userId = "";
this.deviceId = "";
this.queue = [];
this.bsNetwork = bsNetwork;
this.options = options;
this.debug = (0, logging_1.debug)(this.options.debug);
// to avoid short timeout to save cpu usage
if (this.options.flushInterval < 1000) {
this.options.flushInterval = 1000;
}
if (this.options.cacheEventsInLocalstorage && typeof Storage !== 'undefined') {
try {
this.queue = JSON.parse(localStorage.getItem(this.options.cacheEventsKey) || '[]');
}
catch (e) {
this.debug(logging_1.Debug.Error, "Error getting queue items from cache ".concat(e.message || ''));
}
}
this.endpointURL = "".concat(this.options.baseUrl).concat(this.options.ingestPath);
this.updateQueueCache();
this.flushIntervalId = +setInterval(function () {
_this.flush();
}, this.options.flushInterval);
}
BsSender.prototype.removeQueueItemsExceedMaxRetries = function () {
var _this = this;
this.queue = this.queue.filter(function (item) { return item.retries <= _this.options.flushMaxRetries; });
};
BsSender.prototype.removeFromQueue = function (uuids) {
this.queue = this.queue.filter(function (item) { return uuids.indexOf(item.uuid) === -1; });
this.updateQueueCache();
};
BsSender.prototype.updateQueueCache = function () {
if (this.options.cacheEventsInLocalstorage && typeof Storage !== 'undefined') {
localStorage.setItem(this.options.cacheEventsKey, JSON.stringify(this.queue));
}
};
BsSender.prototype.addRetries = function (uuids) {
var time = +(new Date());
this.queue
.filter(function (item) { return uuids.indexOf(item.uuid) !== -1; })
.forEach(function (item) {
item.retries = (+item.retries) + 1;
item.lastRetry = time;
});
this.updateQueueCache();
};
BsSender.prototype.flush = function () {
var flushItems = __spreadArray([], this.queue, true);
if (flushItems.length === 0) {
this.debug(logging_1.Debug.Log, 'Skip sending empty list of logs ');
return;
}
var uuids = flushItems.map(function (item) { return item.uuid; });
var data = flushItems.map(function (item) { return item.event; });
this.flushing = true;
this.send(data, uuids);
};
BsSender.prototype.send = function (dataList, uuids) {
var _this = this;
var platform = (0, BsSystem_1.getOperatingSystem)();
var _a = (0, BsSystem_1.getNetworkInformation)(), downlink = _a.downlink, uplink = _a.uplink;
var deviceObject = {
id: this.deviceId,
brand: "testBrand",
model: "testBrand",
os: platform,
os_ver: platform,
};
var appObject = {
id: "websiteURL",
min_sdk_version: 21,
target_sdk_version: 31,
version: "1.0.3 (3)",
version_code: 3,
version_name: "1.0.3",
};
var eventMainObject = {
s_id: this.generateSessionID(this.userId),
u_id: this.userId,
app_info: appObject,
d_info: deviceObject,
sdk: "jsXCF/".concat(package_json_1.version),
up: uplink,
dn: downlink,
data: dataList
};
return this.bsNetwork.send(this.endpointURL, eventMainObject)
.then(function () {
_this.debug(logging_1.Debug.Info, "Flushed ".concat(dataList.length, " events"));
_this.removeFromQueue(uuids);
})
.catch(function (e) {
_this.addRetries(uuids);
_this.debug(logging_1.Debug.Error, e.message || '');
})
.finally(function () {
_this.flushing = false;
_this.removeQueueItemsExceedMaxRetries();
});
};
BsSender.prototype.generateSessionID = function (userId) {
// return `${userId}_${startTimeInMillis}_${endTimeInMillis}`;
return "".concat(userId, "_0_0");
};
BsSender.prototype.add = function (userId, deviceId, event, forceSend) {
if (forceSend === void 0) { forceSend = false; }
var item = {
uuid: (0, uuid_1.v4)(),
event: event,
time: +new Date(),
retries: 0
};
this.userId = userId;
this.deviceId = deviceId;
this.queue.push(item);
this.updateQueueCache();
// send immediately. In case of error, it was already enqueued
// so will be tried again in interval tick
if (forceSend) {
this.send([item.event], [item.uuid]);
}
};
return BsSender;
}());
exports.default = BsSender;
//# sourceMappingURL=BsSender.js.map