avo-inspector
Version:
[](https://badge.fury.io/js/avo-inspector)
143 lines (142 loc) • 5.66 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AvoDeduplicator = void 0;
var AvoSchemaParser_1 = require("./AvoSchemaParser");
var utils_1 = require("./utils");
var AvoDeduplicator = /** @class */ (function () {
function AvoDeduplicator() {
this.avoFunctionsEvents = {};
this.manualEvents = {};
this.avoFunctionsEventsParams = {};
this.manualEventsParams = {};
}
AvoDeduplicator.prototype.shouldRegisterEvent = function (eventName, params, fromAvoFunction) {
this.clearOldEvents();
if (fromAvoFunction) {
this.avoFunctionsEvents[Date.now()] = eventName;
this.avoFunctionsEventsParams[eventName] = params;
}
else {
this.manualEvents[Date.now()] = eventName;
this.manualEventsParams[eventName] = params;
}
var checkInAvoFunctions = !fromAvoFunction;
return !this.hasSameEventAs(eventName, params, checkInAvoFunctions);
};
AvoDeduplicator.prototype.hasSameEventAs = function (eventName, params, checkInAvoFunctions) {
var result = false;
if (checkInAvoFunctions) {
if (this.lookForEventIn(eventName, params, this.avoFunctionsEventsParams)) {
result = true;
}
}
else {
if (this.lookForEventIn(eventName, params, this.manualEventsParams)) {
result = true;
}
}
if (result) {
delete this.avoFunctionsEventsParams[eventName];
delete this.manualEventsParams[eventName];
}
return result;
};
AvoDeduplicator.prototype.lookForEventIn = function (eventName, params, eventsStorage) {
for (var otherEventName in eventsStorage) {
if (eventsStorage.hasOwnProperty(otherEventName)) {
if (otherEventName === eventName) {
var otherParams = eventsStorage[eventName];
if (otherParams && (0, utils_1.deepEquals)(params, otherParams)) {
return true;
}
}
}
}
return false;
};
AvoDeduplicator.prototype.hasSeenEventParams = function (params, checkInAvoFunctions) {
var result = false;
if (checkInAvoFunctions) {
if (this.lookForEventParamsIn(params, this.avoFunctionsEventsParams)) {
result = true;
}
}
else {
if (this.lookForEventParamsIn(params, this.manualEventsParams)) {
result = true;
}
}
return result;
};
AvoDeduplicator.prototype.lookForEventParamsIn = function (params, eventsStorage) {
for (var otherEventName in eventsStorage) {
if (eventsStorage.hasOwnProperty(otherEventName)) {
var otherParams = eventsStorage[otherEventName];
if (otherParams && (0, utils_1.deepEquals)(params, otherParams)) {
return true;
}
}
}
return false;
};
AvoDeduplicator.prototype.shouldRegisterSchemaFromManually = function (eventName, eventSchema) {
this.clearOldEvents();
return !this.hasSameShapeInAvoFunctionsAs(eventName, eventSchema);
};
AvoDeduplicator.prototype.hasSameShapeInAvoFunctionsAs = function (eventName, eventSchema) {
var result = false;
if (this.lookForEventSchemaIn(eventName, eventSchema, this.avoFunctionsEventsParams)) {
result = true;
}
if (result) {
delete this.avoFunctionsEventsParams[eventName];
}
return result;
};
AvoDeduplicator.prototype.lookForEventSchemaIn = function (eventName, eventSchema, eventsStorage) {
for (var otherEventName in eventsStorage) {
if (eventsStorage.hasOwnProperty(otherEventName)) {
if (otherEventName === eventName) {
var otherSchema = AvoSchemaParser_1.AvoSchemaParser.extractSchema(eventsStorage[eventName]);
if (otherSchema && (0, utils_1.deepEquals)(eventSchema, otherSchema)) {
return true;
}
}
}
}
return false;
};
AvoDeduplicator.prototype.clearOldEvents = function () {
var now = Date.now();
var msToConsiderOld = 300;
for (var time in this.avoFunctionsEvents) {
if (this.avoFunctionsEvents.hasOwnProperty(time)) {
var timestamp = Number(time) || 0;
if (now - timestamp > msToConsiderOld) {
var eventName = this.avoFunctionsEvents[time];
delete this.avoFunctionsEvents[time];
delete this.avoFunctionsEventsParams[eventName];
}
}
}
for (var time in this.manualEvents) {
if (this.manualEvents.hasOwnProperty(time)) {
var timestamp = Number(time) || 0;
if (now - timestamp > msToConsiderOld) {
var eventName = this.manualEvents[time];
delete this.manualEvents[time];
delete this.manualEventsParams[eventName];
}
}
}
};
// used in tests
AvoDeduplicator.prototype._clearEvents = function () {
this.avoFunctionsEvents = {};
this.manualEvents = {};
this.avoFunctionsEventsParams = {};
this.manualEventsParams = {};
};
return AvoDeduplicator;
}());
exports.AvoDeduplicator = AvoDeduplicator;