avo-inspector
Version:
[](https://badge.fury.io/js/avo-inspector)
105 lines (104 loc) • 4.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AvoSessionTracker = void 0;
var AvoGuid_1 = require("./AvoGuid");
var AvoInspector_1 = require("./AvoInspector");
var AvoSessionTracker = /** @class */ (function () {
function AvoSessionTracker(avoBatcher) {
this._lastSessionTimestamp = null;
this._sessionLengthMillis = 5 * 60 * 1000;
this.avoBatcher = avoBatcher;
}
Object.defineProperty(AvoSessionTracker, "sessionId", {
get: function () {
if (AvoSessionTracker._sessionId === null) {
if (!AvoInspector_1.AvoInspector.avoStorage.isInitialized()) {
return "unknown";
}
var maybeSessionId = null;
try {
maybeSessionId = AvoInspector_1.AvoInspector.avoStorage.getItem(AvoSessionTracker.idCacheKey);
}
catch (e) {
console.error("Avo Inspector: something went wrong. Please report to support@avo.app.", e);
}
if (maybeSessionId === null || maybeSessionId === undefined) {
AvoSessionTracker._sessionId = this.updateSessionId();
}
else {
AvoSessionTracker._sessionId = maybeSessionId;
}
}
return AvoSessionTracker._sessionId;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AvoSessionTracker.prototype, "lastSessionTimestamp", {
get: function () {
if (this._lastSessionTimestamp === null ||
this._lastSessionTimestamp === 0) {
var maybeLastSessionTimestamp = AvoInspector_1.AvoInspector.avoStorage.getItem(AvoSessionTracker.lastSessionTimestampKey);
if (maybeLastSessionTimestamp !== null &&
maybeLastSessionTimestamp !== undefined) {
this._lastSessionTimestamp = maybeLastSessionTimestamp;
if (isNaN(this._lastSessionTimestamp)) {
this._lastSessionTimestamp = 0;
}
}
else {
this._lastSessionTimestamp = 0;
}
}
return this._lastSessionTimestamp;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AvoSessionTracker.prototype, "sessionLengthMillis", {
get: function () {
return this._sessionLengthMillis;
},
enumerable: false,
configurable: true
});
AvoSessionTracker.prototype.startOrProlongSession = function (atTime) {
var _this = this;
AvoInspector_1.AvoInspector.avoStorage.runAfterInit(function () {
var timeSinceLastSession = atTime - _this.lastSessionTimestamp;
if (timeSinceLastSession > _this._sessionLengthMillis) {
AvoSessionTracker.updateSessionId();
_this.avoBatcher.handleSessionStarted();
}
_this._lastSessionTimestamp = atTime;
AvoInspector_1.AvoInspector.avoStorage.setItem(AvoSessionTracker.lastSessionTimestampKey, _this._lastSessionTimestamp);
});
};
AvoSessionTracker.updateSessionId = function () {
AvoSessionTracker._sessionId = AvoGuid_1.default.newGuid();
try {
AvoInspector_1.AvoInspector.avoStorage.setItem(AvoSessionTracker.idCacheKey, AvoSessionTracker.sessionId);
}
catch (e) {
console.error("Avo Inspector: something went wrong. Please report to support@avo.app.", e);
}
return AvoSessionTracker._sessionId;
};
Object.defineProperty(AvoSessionTracker, "lastSessionTimestampKey", {
get: function () {
return "AvoInspectorSessionTimestamp";
},
enumerable: false,
configurable: true
});
Object.defineProperty(AvoSessionTracker, "idCacheKey", {
get: function () {
return "AvoInspectorSessionId";
},
enumerable: false,
configurable: true
});
AvoSessionTracker._sessionId = null;
return AvoSessionTracker;
}());
exports.AvoSessionTracker = AvoSessionTracker;